Exemple #1
0
 function send()
 {
     //第一步: 设置相关的headers
     //1.设置邮件的Content-Type,要不然网站的消息内容里面的html标签会被当做纯文本处理
     $smtpHeaderContentType = new SmtpHeaderContentType();
     $smtpHeaderContentType->setType('text/html');
     //2.设置编码集并添加Content-Type
     $headers = new SmtpHeaders();
     $headers->setEncoding('utf-8');
     $headers->addHeader($smtpHeaderContentType);
     //第二步:设置消息的相关
     $message = new SmtpMessage();
     $message->setHeaders($headers);
     $message->addTo($this->mailTo)->addFrom($this->mailFrom)->setSubject($this->subject)->setBody($this->body);
     //邮件的内容
     //第三步:设置Smtp的相关链接参数
     $smtpOptions = new SmtpOptions();
     $smtpOptions->setHost($this->host)->setPort($this->port)->setConnectionClass('login')->setConnectionConfig(array('username' => $this->username, 'password' => $this->password, 'ssl' => 'ssl'));
     //第四步:加载配置,发送消息
     $smtpTransport = new SmtpTransport();
     $smtpTransport->setOptions($smtpOptions);
     //加载配置
     $smtpTransport->send($message);
     //发送消息
 }
Exemple #2
0
 public function testProvidingParametersIntroducesHeaderFolding()
 {
     $header = new ContentType();
     $header->setType('application/x-unit-test');
     $header->addParameter('charset', 'us-ascii');
     $string = $header->toString();
     $this->assertContains("Content-Type: application/x-unit-test;\r\n", $string);
     $this->assertContains(";\r\n charset=\"us-ascii\"", $string);
 }
Exemple #3
0
 /**
  * Get a blank email message object.
  *
  * @return Message
  */
 public function getNewMessage()
 {
     $message = new Message();
     $message->setEncoding('UTF-8');
     $headers = $message->getHeaders();
     $ctype = new ContentType();
     $ctype->setType('text/plain');
     $ctype->addParameter('charset', 'UTF-8');
     $headers->addHeader($ctype);
     return $message;
 }
 public function __construct(array $options = array())
 {
     // @TODO make this multipart
     parent::__construct($options);
     $this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/html; charset=UTF-8'));
     $this->setEncoding('UTF-8');
     $this->variables = new ViewVariables();
 }
 /**
  * @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;
 }
 public function __construct(array $options = array())
 {
     parent::__construct($options);
     $this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/plain; charset=UTF-8'));
     $this->setEncoding('UTF-8');
 }
Exemple #7
0
 public function testExtractsExtraInformationFromContentType()
 {
     $contentTypeHeader = ContentType::fromString('Content-Type: multipart/alternative; boundary="Apple-Mail=_1B852F10-F9C6-463D-AADD-CD503A5428DD"');
     $params = $contentTypeHeader->getParameters();
     $this->assertEquals($params, array('boundary' => 'Apple-Mail=_1B852F10-F9C6-463D-AADD-CD503A5428DD'));
 }
Exemple #8
0
 /**
  * @param       $subject
  * @param       $html
  * @param       $plain
  * @param array $options
  * @param array $receivers
  * @param array $attachments
  *
  * @return Mail\Message
  */
 protected function _assembleMailMessage($subject, $html, $plain, array $options, array $receivers, array $attachments = [])
 {
     $config = $this->_rendererConfig;
     $message = new Mail\Message();
     $message->setEncoding('UTF-8');
     // Sender
     if (isset($options['use_default_sender']) && $options['use_default_sender'] === true) {
         $fromEmail = isset($config['mail_from_email']) ? $config['mail_from_email'] : null;
         $fromName = isset($config['mail_from_name']) ? $config['mail_from_name'] : null;
     } else {
         $fromEmail = isset($options['sender_mail']) ? $options['sender_mail'] : null;
         $fromName = isset($options['sender_name']) ? $options['sender_name'] : null;
     }
     $addressListFrom = new Mail\AddressList();
     $addressListFrom->add(new Mail\Address($fromEmail, isset($fromName) ? $fromName : null));
     $message->setFrom($addressListFrom);
     // Receiver
     $addressListTo = new Mail\AddressList();
     foreach ($receivers as $receiver) {
         $recEmail = isset($config['email_receiver_override']) ? $config['email_receiver_override'] : $receiver[0];
         $recName = isset($receiver[1]) ? $receiver[1] : null;
         $addressListTo->add(new Mail\Address($recEmail, $recName));
     }
     $message->setTo($addressListTo);
     if (isset($config['mail_bcc']) && is_array($config['mail_bcc'])) {
         foreach ($config['mail_bcc'] as $bccAddress) {
             // Avoid sending email copy to "to" AND bcc
             $found = false;
             foreach ($message->getTo() as $to) {
                 if ($to->getEmail() == $bccAddress) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $addressListBcc = new Mail\AddressList();
                 $addressListBcc->add(new Mail\Address($bccAddress));
                 $message->addBcc($addressListBcc);
             }
         }
     }
     if (isset($config['subject_prefix']) && $config['subject_prefix'] != '') {
         $subject = $config['subject_prefix'] . $subject;
     }
     $message->setSubject($subject);
     if (empty($attachments)) {
         $message->setBody($html);
         $contentType = Mail\Header\ContentType::fromString('Content-Type: text/html; charset=utf-8');
         $message->getHeaders()->addHeader($contentType);
         $contentTransferEncoding = Mail\Header\ContentTransferEncoding::fromString('Content-Transfer-Encoding: 8bit');
         $message->getHeaders()->addHeader($contentTransferEncoding);
     } else {
         $html = new Mime\Part($html);
         $html->type = Mime\Mime::TYPE_HTML;
         $html->charset = 'UTF-8';
         $html->disposition = Mime\Mime::DISPOSITION_INLINE;
         $html->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE;
         $body = new Mime\Message();
         $body->addPart($html);
         foreach ($attachments as $attachmentData) {
             if (isset($attachmentData['file_path']) && is_readable($attachmentData['file_path'])) {
                 $attachmentData['file_data'] = file_get_contents($attachmentData['file_path']);
             }
             if (isset($attachmentData['file_data']) && !empty($attachmentData['file_data'])) {
                 $attachment = new Mime\Part($attachmentData['file_data']);
                 $attachment->type = $attachmentData['mime_type'];
                 $attachment->filename = $attachmentData['name'];
                 $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
                 $attachment->encoding = Mime\Mime::ENCODING_BASE64;
                 $body->addPart($attachment);
             }
         }
         $message->setBody($body);
     }
     return $message;
 }
Exemple #9
-1
 /**
  * @group #2728
  *
  * Tests setting different MIME types
  */
 public function testSetContentType()
 {
     $header = new ContentType();
     $header->setType('application/vnd.ms-excel');
     $this->assertEquals('Content-Type: application/vnd.ms-excel', $header->toString());
     $header->setType('application/rss+xml');
     $this->assertEquals('Content-Type: application/rss+xml', $header->toString());
     $header->setType('video/mp4');
     $this->assertEquals('Content-Type: video/mp4', $header->toString());
     $header->setType('message/rfc822');
     $this->assertEquals('Content-Type: message/rfc822', $header->toString());
 }