Exemple #1
0
 protected function send_email($email)
 {
     $send_email = FALSE;
     $sendgrid = new SendGrid(getenv("SENDGRID_USERNAME"), getenv("SENDGRID_PASSWORD"));
     $message = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/></head><body>';
     $message .= '<div style="background-color:white;font-size:14px;font-family:\'Segoe UI\',Arial,Helvetica,sans-serif">';
     $message .= '<table style="background-color:#000000;width:100%;font-weight:bold;vertical-align:middle" cellspacing="1" cellpadding="3" border="0"><tbody><tr><td style="font-size:14px;font-family:\'Segoe UI\',Arial,Helvetica,sans-serif">';
     $message .= '<img border="0" src="' . base_url('assets/images/logo.png') . '" width="82" height="40"></td></tr></tbody></table>';
     foreach ($email['text'] as $line) {
         $message .= '<p style="background-color:white;font-size:14px;font-family:\'Segoe UI\',Arial,Helvetica,sans-serif">' . $line . '</p>';
     }
     $message .= '<hr noshade="" size="1">';
     $message .= '<div style="background-color:white;font-size:14px;font-family:\'Segoe UI\',Arial,Helvetica,sans-serif"><a style="text-decoration:none" href="https://www.facebook.com/chaarbhai" target="_blank">Chaar Bhai</a><span style="color: white;">0</span></div>';
     $message .= '<div style="color: #888888;background-color:white;font-size:14px;font-family:\'Segoe UI\',Arial,Helvetica,sans-serif"><strong>Note</strong>: This message has been sent from an unattended email box.</div>';
     $message .= '</div></body></html>';
     $mail = new SendGrid\Email();
     foreach ($email['recipients'] as $recipient) {
         if (!empty($recipient['email'])) {
             $mail->addTo($recipient['email'], $recipient['name']);
             $send_email = TRUE;
         }
     }
     if (isset($email['attachments'])) {
         foreach ($email['attachments'] as $attachment) {
             $mail->addAttachment($attachment['path'], $attachment['name']);
         }
     }
     $mail->setFrom('*****@*****.**')->setFromName('Chaar Bhai');
     $mail->setSubject($email['subject']);
     $mail->setHtml($message);
     if ($send_email) {
         //$sendgrid->send($mail);
     }
 }
Exemple #2
0
 public function testToWebFormatWithAttachmentCustomFilename()
 {
     $email = new \SendGrid\Email();
     $email->addAttachment('./gif.gif', 'different.jpg');
     $f = pathinfo('./gif.gif');
     $json = $email->toWebFormat();
     $this->assertEquals($json['files']['gif.gif'], $f['dirname'] . '/' . $f['basename']);
 }
 public function testToWebFormatWithAttachmentCustomFilename()
 {
     $email = new SendGrid\Email();
     $email->addAttachment('./gif.gif', 'different.jpg');
     $json = $email->toWebFormat();
     // php 5.5 works differently. @filename has been deprecated for CurlFile in 5.5
     if (class_exists('CurlFile')) {
         $content = new \CurlFile('./gif.gif', 'gif', 'gif');
         $this->assertEquals($json["files[different.jpg]"], $content);
     } else {
         $this->assertEquals($json["files[different.jpg]"], "@./gif.gif");
     }
 }
 /**
  * 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();
     }
 }
 public function testToWebFormatWithAttachmentCustomFilename()
 {
     $email = new SendGrid\Email();
     $email->addAttachment('./gif.gif', 'different.jpg');
     $json = $email->toWebFormat();
     $this->assertEquals($json["files[different.jpg]"], "@./gif.gif");
 }
 /**
  * Set Attachment Files.
  *
  * @param SendGrid\Email $email
  * @param Swift_Mime_Message $message
  */
 protected function setAttachment($email, Swift_Mime_Message $message)
 {
     foreach ($message->getChildren() as $attachment) {
         $tmp_filename = tempnam(sys_get_temp_dir(), 'att');
         file_put_contents($tmp_filename, $attachment->getBody());
         $email->addAttachment($tmp_filename, $attachment->getFilename());
         $this->tmp_files[] = $tmp_filename;
     }
 }