/**
  * Should sendgrid user settings be used to send email.
  * @return boolean
  */
 protected function shouldCustomUserSettingsBeUsed()
 {
     if ($this->emailAccount == null) {
         return false;
     }
     $itemData = EmailMessageUtil::getCampaignOrAutoresponderDataByEmailMessage($this->emailMessage);
     if ($itemData != null) {
         $useAutoresponderOrCampaignOwnerMailSettings = (bool) ZurmoConfigurationUtil::getByModuleName('MarketingModule', 'UseAutoresponderOrCampaignOwnerMailSettings');
         if ($this->emailAccount->useCustomOutboundSettings == EmailMessageUtil::OUTBOUND_PERSONAL_SMTP_SETTINGS && $this->emailAccount->outboundHost != '' && $this->emailAccount->outboundUsername != '' && $this->emailAccount->outboundPassword != '' && $useAutoresponderOrCampaignOwnerMailSettings === true) {
             $this->updateMailerDetailsForEmailMessage('smtp', 'personal');
             return true;
         }
         return false;
     } elseif ($this->emailAccount->useCustomOutboundSettings == EmailMessageUtil::OUTBOUND_PERSONAL_SMTP_SETTINGS && $this->emailAccount->outboundHost != '' && $this->emailAccount->outboundUsername != '' && $this->emailAccount->outboundPassword != '') {
         $this->updateMailerDetailsForEmailMessage('smtp', 'personal');
         return true;
     }
     return false;
 }
 /**
  * Send email.
  */
 public function sendEmail()
 {
     $emailMessage = $this->emailMessage;
     if ($this->emailAccount == null) {
         $apiUser = Yii::app()->sendGridEmailHelper->apiUsername;
         $apiPassword = Yii::app()->sendGridEmailHelper->apiPassword;
     } else {
         $apiUser = $this->emailAccount->apiUsername;
         $apiPassword = ZurmoPasswordSecurityUtil::decrypt($this->emailAccount->apiPassword);
     }
     $itemData = EmailMessageUtil::getCampaignOrAutoresponderDataByEmailMessage($this->emailMessage);
     $sendgrid = new SendGrid($apiUser, $apiPassword, array("turn_off_ssl_verification" => true));
     $email = new SendGrid\Email();
     $email->setFrom($this->fromUserEmailData['address'])->setFromName($this->fromUserEmailData['name'])->setSubject($emailMessage->subject)->setText($emailMessage->content->textContent)->setHtml($emailMessage->content->htmlContent)->addUniqueArg("zurmoToken", md5(ZURMO_TOKEN))->addHeader('X-Sent-Using', 'SendGrid-API')->addHeader('X-Transport', 'web');
     //Check if campaign and if yes, associate to email.
     if ($itemData != null) {
         list($itemId, $itemClass, $personId) = $itemData;
         $email->addUniqueArg("itemId", $itemId);
         $email->addUniqueArg("itemClass", $itemClass);
         $email->addUniqueArg("personId", $personId);
     }
     foreach ($this->toAddresses as $emailAddress => $name) {
         $email->addTo($emailAddress, $name);
     }
     foreach ($this->ccAddresses as $emailAddress => $name) {
         $email->addCc($emailAddress);
     }
     foreach ($this->bccAddresses as $emailAddress => $name) {
         $email->addBcc($emailAddress);
     }
     //Attachments
     $attachmentsData = array();
     $tempAttachmentPath = Yii::app()->getRuntimePath() . DIRECTORY_SEPARATOR . 'emailAttachments';
     if (!file_exists($tempAttachmentPath)) {
         mkdir($tempAttachmentPath);
     }
     if (!empty($emailMessage->files)) {
         foreach ($emailMessage->files as $file) {
             $fileName = tempnam($tempAttachmentPath, 'zurmo_');
             $fp = fopen($fileName, 'wb');
             fwrite($fp, $file->fileContent->content);
             fclose($fp);
             $email->addAttachment($fileName, $file->name);
             $attachmentsData[] = $fileName;
         }
     }
     $emailMessage->sendAttempts = $emailMessage->sendAttempts + 1;
     $response = $sendgrid->send($email);
     if ($response->message == 'success') {
         //Here we need to check if
         $emailMessage->error = null;
         $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_SENT);
         $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     } elseif ($response->message == 'error') {
         $content = Zurmo::t('EmailMessagesModule', 'Response from Server') . "\n";
         foreach ($response->errors as $error) {
             $content .= $error;
         }
         $emailMessageSendError = new EmailMessageSendError();
         $data = array();
         $data['message'] = $content;
         $emailMessageSendError->serializedData = serialize($data);
         $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_OUTBOX_ERROR);
         $emailMessage->error = $emailMessageSendError;
     }
     if (count($attachmentsData) > 0) {
         foreach ($attachmentsData as $path) {
             unlink($path);
         }
     }
     $saved = $emailMessage->save(false);
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
 }