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);
     }
 }
 /**
  * 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');
 }