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;
 }
 /**
  * @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;
 }
Exemple #5
-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());
 }