function onBeforeWrite() { if ($this->MessageID && !$this->MandrillMessageID) { if ($MM = MandrillMessage::get()->filter(array('MandrillID' => $this->MessageID))) { $this->MandrillMessageID = $MM->First()->ID; } } }
public function actionSend() { $name = isset($_POST['to_name']) ? $_POST['to_name'] : ""; $email = isset($_POST['to_email']) ? $_POST['to_email'] : ""; $body = isset($_POST['body']) ? $_POST['body'] : ""; $subject = isset($_POST['subject']) ? $_POST['subject'] : ""; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { header('Content-type: application/json'); echo json_encode(['message' => 'invalid email']); return; } if (!$body) { header('Content-type: application/json'); echo json_encode(['message' => 'email body must be passed']); return; } $message = new MandrillMessage(); if (isset(Yii::app()->params['mandrill']['test_run']) && Yii::app()->params['mandrill']['test_run']) { $message->enableTest(); } $message->setFrom(Yii::app()->params['email']['sender_email'], Yii::app()->params['email']['sender_name']); $message->addTo($email, $name); $message->setHtmlBody($body); $message->setSubject($subject); if ($message->send()) { header('Content-type: application/json'); echo json_encode($message->attributes); } }
public function actionMessageDetails($id) { $model = MandrillMessage::model()->findByPk($id); if (!$model) { throw new CHttpException(404, 'Mandrill Message [id: ' . $id . '] was not found'); } $dataProviderModel = new MandrillEmail('search'); $dataProviderModel->messageId = $model->id; if (isset($_GET['MandrillEmail']) && $_GET['MandrillEmail']) { $dataProviderModel->attributes = $_GET['MandrillEmail']; } $this->render('messageDetails', compact('model', 'dataProviderModel')); }
/** * Returns a GridField of messages * @return CMSForm */ public function ListForm() { $fields = new FieldList(); $gridFieldConfig = GridFieldConfig::create()->addComponents(new GridFieldToolbarHeader(), new GridFieldSortableHeader(), new GridFieldDataColumns(), new GridFieldFooter()); $gridField = new GridField('SearchResults', _t('MandrillAdmin.SearchResults', 'Search Results'), $this->Messages(), $gridFieldConfig); $columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); $columns->setDisplayFields(array('date' => _t('MandrillAdmin.MessageDate', 'Date'), 'state' => _t('MandrillAdmin.MessageStatus', 'Status'), 'sender' => _t('MandrillAdmin.MessageSender', 'Sender'), 'email' => _t('MandrillAdmin.MessageEmail', 'Email'), 'subject' => _t('MandrillAdmin.MessageSubject', 'Subject'), 'opens' => _t('MandrillAdmin.MessageOpens', 'Opens'), 'clicks' => _t('MandrillAdmin.MessageClicks', 'Clicks'))); $columns->setFieldFormatting(array('subject' => function ($value, &$item) { return sprintf('<a href="%s" class="cms-panel-link" data-pjax-target="Content">%s</a>', Convert::raw2xml($item->Link), $value); }, 'state' => function ($value, &$item) { $color = MandrillMessage::getColorForState($value); return sprintf('<span style="color:%s">%s</span>', $color, $value); })); $gridField->addExtraClass('all-messages-gridfield'); $fields->push($gridField); $actions = new FieldList(); $form = CMSForm::create($this, "ListForm", $fields, $actions)->setHTMLID('Form_ListForm'); $form->setResponseNegotiator($this->getResponseNegotiator()); return $form; }
/** * Send a multi-part HTML email. * * @return bool */ function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false) { $message = $this->initEmail($to, $from, $subject, $attachedFiles, $customheaders); // set up the body // @todo inlineimages $message['html'] = $htmlContent; if ($plainContent) { $message['text'] = $plainContent; } // send and return try { $mail = $this->initMailer(); $result = $mail->messages->send($message, $this->async, $this->ip_pool, $this->send_at); foreach ($result as $sentmessage) { $MandrillMessage = new MandrillMessage(); $MandrillMessage->MandrillID = $sentmessage['_id']; $MandrillMessage->To = $to; $MandrillMessage->From = $from; $MandrillMessage->Subject = $subject; if (isset($sentmessage['status'])) { $MandrillMessage->MandrillStatus = $sentmessage['status']; } if (isset($sentmessage['reject_reason'])) { $MandrillMessage->MandrillRejectReason = $sentmessage['reject_reason']; } $MandrillMessage->MandrillMessage = json_encode($message); $MandrillMessage->write(); } return true; } catch (Mandrill_Error $e) { // Mandrill errors are thrown as exceptions user_error('A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage()); // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123' } catch (Exception $e) { error_log('Unknown'); return $this->httpError(400, 'An error occurred'); } }
/** * This got messy * * @param $instructionId * @throws CHttpException */ public function actionSendMailshot($instructionId) { $model = new Client('search'); if (isset($_POST['send']) && $_POST['send']) { $instruction = Deal::model()->findByPk($instructionId); $type = MailshotType::model()->findByPk($_POST['MailshotType']); $model->attributes = $_GET['Client']; $dataProvider = $model->search(); $dataProvider->setPagination(['pageSize' => 100]); if (!$type) { throw new CHttpException(404, 'Mailshot Type [name: ' . $_POST['MailshotType'] . '] was not found'); } if (!$instruction) { throw new CHttpException(404, 'Instruction [id: ' . $instructionId . '] was not found'); } $mailshot = new MandrillMailshot(); $mailshot->instructionId = $instruction->dea_id; $mailshot->type = $type->name; $mailshot->save(); if ($type->templatePath && file_exists($type->templatePath)) { ob_start(); include $type->templatePath; $htmlTemplate = ob_get_clean(); } else { $htmlTemplate = $this->execTemplate($type->htmlTemplate); } $textTemplate = $this->execTemplate($type->textTemplate); $mandrillMessagePreset = new MandrillMessage(); $mandrillMessagePreset->setFrom(Yii::app()->params['mailshot']['sender_email'], Yii::app()->params['mailshot']['sender_name']); $mandrillMessagePreset->setHtmlBody($htmlTemplate); $mandrillMessagePreset->setTextBody($textTemplate); $mandrillMessagePreset->setSubject($type->subject); $mandrillMessagePreset->setPreserveRecepients(false); $mandrillMessagePreset->setGlobalMergeVar('MAILSHOT_ID', $mailshot->id); $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_ID', $instruction->dea_id); $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_PRICE', Locale::formatCurrency($instruction->dea_marketprice)); $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_TITLE', $instruction->title); $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_STRAPLINE', $instruction->dea_strapline); $mandrillMessagePreset->setGlobalMergeVar('OFFICE_TITLE', $instruction->branch->office->title); $mandrillMessagePreset->setGlobalMergeVar('OFFICE_NUMBER', $instruction->branch->bra_tel); if (isset($_POST['test']) && $_POST['test']) { $mandrillMessagePreset->enableTest(); } $iterator = new DataProviderIterator($dataProvider); /** @var $mandrillMessage MandrillMessage */ $mandrillMessage = null; $x = 0; $staffEmails = array_fill_keys(Yii::app()->params['mailshot']['alwaysSendTo'], '?'); if (Yii::app()->params['mandrill']['test_run']) { $testEmails = Yii::app()->params['mandrill']['test_emails']; } /** @var $client Client */ foreach ($iterator as $client) { $email = $client->cli_email; if (isset($testEmails)) { if (!$testEmails) { break; } $email = array_shift($testEmails); } if (!$email) { continue; } //we are sending emails to our staff in a different message if (array_key_exists($email, $staffEmails)) { continue; } if ($x % Yii::app()->params['mandrill']['mails_in_message'] === 0) { if ($mandrillMessage) { $mandrillMessage->send(); $mailshot->addMessage($mandrillMessage); } $mandrillMessage = clone $mandrillMessagePreset; } $mandrillMessage->addTo($email, $client->getFullName()); $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_FULLNAME', $client->getFullName()); $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_SALUTATION', $client->cli_salutation); $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_ID', $client->cli_id); $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_EMAIL', $email); $mandrillMessage->setRecepientMetaData($email, 'clientId', $client->cli_id); $x++; } if (!$mandrillMessage->id) { $mandrillMessage->send(); $mailshot->addMessage($mandrillMessage); } /** * doing a trick. will send an email to everyone in staff list to avoid complains */ $staffMessage = clone $mandrillMessagePreset; $clients = Client::model()->findAll('cli_email IN (' . implode(', ', $staffEmails) . ')', array_keys($staffEmails)); foreach ($clients as $client) { $email = $client->cli_email; $staffMessage->addTo($email, $client->getFullName()); $staffMessage->setRecepientMergeVar($email, 'CLIENT_FULLNAME', $client->getFullName()); $staffMessage->setRecepientMergeVar($email, 'CLIENT_SALUTATION', $client->cli_salutation); $staffMessage->setRecepientMergeVar($email, 'CLIENT_ID', $client->cli_id); $staffMessage->setRecepientMergeVar($email, 'CLIENT_EMAIL', $email); $staffMessage->setRecepientMetaData($email, 'clientId', $client->cli_id); } $staffMessage->send(); $mailshot->addMessage($staffMessage); Yii::app()->user->setFlash('mailshot-sent', 'Mailshot successfully sent'); $this->redirect(['instruction/summary', 'id' => $instructionId]); } $this->render('sendMailshot'); }