public function actionView($template_id, $group_id, $campaign_id) { $this->layout = '/layouts/vanilla'; $fromEmailDomain = Yii::app()->params['insiderEmailDomain']; $testEmailInvalid = false; $EmailTemplate = EmailTemplate::model()->findByPK($template_id); if (is_null($EmailTemplate)) { throw new CHttpException(404, 'Unable to find template.'); } $CampaignGroup = CampaignGroup::model()->with(array('campaign'))->findByPK($group_id); if (is_null($CampaignGroup)) { throw new CHttpException(404, 'Unable to find campaign group.'); } if (isset($_POST['delete'])) { $path = dirname(Yii::app()->request->scriptFile) . '/templates/' . $EmailTemplate->folder; $this->deleteDir($path); $EmailTemplate->delete(); $this->redirect(array('emailTemplate/create', 'campaign_id' => $campaign_id, 'group_id' => $group_id)); } //send a test email if (isset($_POST['EmailTemplate']['email_test_recipient']) && $_POST['EmailTemplate']['email_test_recipient']) { $insiderEmailDomain = 'abc123.mailgun.org'; $mailgunApiVerify = new MailgunCampaign($fromEmailDomain, Yii::app()->params['mailgun']['public-key']); try { $emails = explode(',', $_POST['EmailTemplate']['email_test_recipient']); foreach ($emails as $key => $email) { $emails[$key] = trim($email); $validEmailResponse = $mailgunApiVerify->validateEmailAddress($emails[$key]); if ($validEmailResponse['is_valid'] != true) { unset($emails[$key]); } } if (sizeof($emails)) { // send it foreach ($emails as $key => $email) { $mailgunApi = new MailgunCampaign($fromEmailDomain, Yii::app()->params['mailgun']['key']); $message = $mailgunApi->newMessage(); $message->setFrom('email@' . $fromEmailDomain, 'Application Name'); $message->addTo($email); $html = $EmailTemplate->exampleEmail; $html = preg_replace("@%recipient.warehouse_id%@", '000', $html); foreach ($CampaignGroup->campaign->outcomes as $Outcome) { if (strlen($Outcome->url)) { $html = preg_replace("@%recipient.outcome_" . $Outcome->id . "%@", $Outcome->url, $html); } } $message->setHtml($html); $message->setSubject($CampaignGroup->subject); $message->send(); } Yii::app()->user->setFlash('success', "Emails sent to " . implode(', ', $emails) . '.'); } else { Yii::app()->user->setFlash('danger', 'No valid email addresses found to send to'); } $this->refresh(); } catch (Exception $e) { var_dump($e); exit; $testEmailInvalid = true; } } //check template for first_name if (stripos($EmailTemplate->html, '%recipient.first_name%') === false) { $EmailTemplate->addNotice('Are you aware that the template.html file does not have a recipient first name tag?: "%recipient.first_name%'); } //check template for last_name if (stripos($EmailTemplate->html, '%recipient.last_name%') === false) { $EmailTemplate->addNotice('Are you aware that the template.html file does not have a recipient last name tag?: "%recipient.last_name%'); } //check we have an email subject if (!strlen($CampaignGroup->subject)) { $EmailTemplate->addNotice('You have not set an email subject for this group.'); } //check for outcomes in the template preg_match_all("/%recipient.outcome_(\\d+)%/", $EmailTemplate->html, $outcomes); if (!sizeof($outcomes[0])) { $EmailTemplate->addNotice('Are you aware there are no outcome tags in the template?'); } else { foreach ($outcomes[0] as $tag) { $EmailTemplate->addNotice('We found the following tag in your template: ' . $tag); } } $this->breadcrumbs = array('Campaigns' => array('campaign/index'), $CampaignGroup->campaign->name => array('campaign/createUpdate', 'id' => $CampaignGroup->campaign_id), 'Group ' . $CampaignGroup->name => array('campaignGroup/update', 'id' => $CampaignGroup->id, 'campaign_id' => $CampaignGroup->campaign_id), 'View Email Template'); $this->render('view', array('EmailTemplate' => $EmailTemplate, 'subject' => $CampaignGroup->subject, 'has_ran' => $CampaignGroup->campaign->hasBeenRun, 'testEmailInvalid' => $testEmailInvalid)); }