/**
  * @return Message
  */
 public static function getValid()
 {
     $message = new Message();
     $message->setFrom(MailWrapperTestBootstrap::$from);
     $message->addTo(MailWrapperTestBootstrap::$toAddresses[0]);
     $message->addTo(MailWrapperTestBootstrap::$toAddresses[1]);
     $message->addCC(MailWrapperTestBootstrap::$ccAddresses[0]);
     $message->addCC(MailWrapperTestBootstrap::$ccAddresses[1]);
     $message->addBCC(MailWrapperTestBootstrap::$bccAddresses[0]);
     $message->addBCC(MailWrapperTestBootstrap::$bccAddresses[1]);
     $message->addReplyTo(MailWrapperTestBootstrap::$alternate);
     $message->setSubject(MailWrapperTestBootstrap::$subject);
     $message->setBody(MailWrapperTestBootstrap::$contentText);
     return $message;
 }
Example #2
0
 public function send(Email $email)
 {
     $message = new Message();
     $config = $this->config;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($this->params['fromName'])) {
             $fromName = $this->params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($this->params['fromAddress'])) {
             $fromAddress = $this->params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($this->params['fromName'])) {
             $fromName = $this->params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $parts = array();
     if ($email->get('isHtml')) {
         $bodyPart = new MimePart($email->getBodyForSending());
         $bodyPart->type = 'text/html';
         $bodyPart->charset = 'utf-8';
     } else {
         if ($email->get('bodyPlain')) {
             $bodyPart = new MimePart($email->get('bodyPlain'));
         } else {
             $bodyPart = new MimePart($email->get('body'));
         }
         $bodyPart->type = 'text/plain';
         $bodyPart->charset = 'utf-8';
     }
     $parts[] = $bodyPart;
     $aCollection = $email->get('attachments');
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $aCollection = $email->getInlineAttachments();
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $body->setParts($parts);
     $message->setBody($body);
     try {
         $this->transport->send($message);
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }
Example #3
0
 public function send(Email $email, $params = array(), &$message = null, $attachmetList = [])
 {
     if (!$message) {
         $message = new Message();
     }
     $config = $this->config;
     $params = $this->params + $params;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($params['fromAddress'])) {
             $fromAddress = $params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     if (!$email->get('from')) {
         $email->set('from', $fromAddress);
     }
     if (!empty($params['replyToAddress'])) {
         $replyToName = null;
         if (!empty($params['replyToName'])) {
             $replyToName = $params['replyToName'];
         }
         $message->setReplyTo($params['replyToAddress'], $replyToName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $value = $email->get('replyTo');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addReplyTo(trim($address));
             }
         }
     }
     $attachmentPartList = array();
     $attachmentCollection = $email->get('attachments');
     $attachmentInlineCollection = $email->getInlineAttachments();
     foreach ($attachmetList as $attachment) {
         $attachmentCollection[] = $attachment;
     }
     if (!empty($attachmentCollection)) {
         foreach ($attachmentCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     if (!empty($attachmentInlineCollection)) {
         foreach ($attachmentInlineCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $textPart = new MimePart($email->getBodyPlainForSending());
     $textPart->type = 'text/plain';
     $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $textPart->charset = 'utf-8';
     if ($email->get('isHtml')) {
         $htmlPart = new MimePart($email->getBodyForSending());
         $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
         $htmlPart->type = 'text/html';
         $htmlPart->charset = 'utf-8';
     }
     if (!empty($attachmentPartList)) {
         $messageType = 'multipart/related';
         if ($email->get('isHtml')) {
             $content = new MimeMessage();
             $content->addPart($textPart);
             $content->addPart($htmlPart);
             $messageType = 'multipart/mixed';
             $contentPart = new MimePart($content->generateMessage());
             $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"';
             $body->addPart($contentPart);
         } else {
             $body->addPart($textPart);
         }
         foreach ($attachmentPartList as $attachmentPart) {
             $body->addPart($attachmentPart);
         }
     } else {
         if ($email->get('isHtml')) {
             $body->setParts(array($textPart, $htmlPart));
             $messageType = 'multipart/alternative';
         } else {
             $body = $email->getBodyPlainForSending();
             $messageType = 'text/plain';
         }
     }
     $message->setBody($body);
     if ($messageType == 'text/plain') {
         if ($message->getHeaders()->has('content-type')) {
             $message->getHeaders()->removeHeader('content-type');
         }
         $message->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
     } else {
         if (!$message->getHeaders()->has('content-type')) {
             $contentTypeHeader = new \Zend\Mail\Header\ContentType();
             $message->getHeaders()->addHeader($contentTypeHeader);
         }
         $message->getHeaders()->get('content-type')->setType($messageType);
     }
     $message->setEncoding('UTF-8');
     try {
         $rand = mt_rand(1000, 9999);
         if ($email->get('parentType') && $email->get('parentId')) {
             $messageId = '' . $email->get('parentType') . '/' . $email->get('parentId') . '/' . time() . '/' . $rand . '@espo';
         } else {
             $messageId = '' . md5($email->get('name')) . '/' . time() . '/' . $rand . '@espo';
         }
         if ($email->get('isSystem')) {
             $messageId .= '-system';
         }
         $messageIdHeader = new \Zend\Mail\Header\MessageId();
         $messageIdHeader->setId($messageId);
         $message->getHeaders()->addHeader($messageIdHeader);
         $this->transport->send($message);
         $email->set('messageId', '<' . $messageId . '>');
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }