Esempio n. 1
0
 /**
  * @param string $subject
  * @param string $plain
  * @param string $html
  * @param string $messageId
  *
  * @return \Zend\Mail\Message
  */
 public function createMessage($subject, $plain, $html, $messageId)
 {
     $mail = $this->getMessageClass();
     $mail->setFrom(\Yii::$app->params['mailFromEmail'], \Yii::$app->params['mailFromName']);
     $mail->setSubject($subject);
     $mail->setEncoding('UTF-8');
     $mId = new \Zend\Mail\Header\MessageId();
     $mId->setId($messageId);
     $mail->getHeaders()->addHeader($mId);
     if ($html == '') {
         $mail->setBody($plain);
         $content = new \Zend\Mail\Header\ContentType();
         $content->setType('text/plain');
         $content->addParameter('charset', 'UTF-8');
         $mail->getHeaders()->addHeader($content);
     } else {
         $html = '<!DOCTYPE html><html>
         <head><meta charset="utf-8"><title>' . Html::encode($subject) . '</title>
         </head><body>' . $html . '</body></html>';
         $converter = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($html);
         $converter->setStripOriginalStyleTags(true);
         $converter->setUseInlineStylesBlock(true);
         $converter->setEncoding('UTF-8');
         $converter->setCleanup(false);
         $converter->setExcludeMediaQueries(true);
         $contentHtml = $converter->convert();
         $contentHtml = preg_replace("/ data\\-[a-z0-9_-]+=\"[^\"]*\"/siu", "", $contentHtml);
         $textPart = new \Zend\Mime\Part($plain);
         $textPart->type = 'text/plain';
         $textPart->charset = 'UTF-8';
         $htmlPart = new \Zend\Mime\Part($contentHtml);
         $htmlPart->type = 'text/html';
         $htmlPart->charset = 'UTF-8';
         $mimem = new \Zend\Mime\Message();
         $mimem->setParts([$textPart, $htmlPart]);
         $mail->setBody($mimem);
         /** @var ContentType $contentType */
         $contentType = $mail->getHeaders()->get('content-type');
         $contentType->setType('multipart/alternative');
     }
     return $mail;
 }
Esempio n. 2
0
 function setHeader($name, $value)
 {
     if ($name === 'Message-ID') {
         $this->mail->getHeaders()->addHeader(Zend\Mail\Header\MessageId::fromString('Message-ID: ' . trim($value, "<>")));
     } else {
         $this->mail->getHeaders()->addHeaderLine($name, $value);
     }
 }
Esempio n. 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();
 }