/** * @param Swift_Events_SendEvent $evt */ public function beforeSendPerformed(\Swift_Events_SendEvent $evt) { $message = $evt->getMessage(); $converter = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles(); $converter->setEncoding($message->getCharset()); $converter->setUseInlineStylesBlock(); $converter->setCleanup(); if ($message->getContentType() === 'text/html' || $message->getContentType() === 'multipart/alternative' && $message->getBody()) { $converter->setHTML($message->getBody()); $message->setBody($converter->convert()); } foreach ($message->getChildren() as $part) { if (strpos($part->getContentType(), 'text/html') === 0) { $converter->setHTML($part->getBody()); $part->setBody($converter->convert()); } } }
/** * @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 static function send_email_zend($email, $betreff, $text_plain, $text_html = null, $mail_tag = null) { $mail = new Zend\Mail\Message(); $mail->setFrom(Yii::app()->params["adminEmail"], Yii::app()->params["adminEmailName"]); $mail->addTo($email, $email); $mail->setSubject($betreff); $mail->setEncoding("UTF-8"); if ($text_html !== null) { $converter = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($text_html); $converter->setStripOriginalStyleTags(false); $converter->setUseInlineStylesBlock(true); $converter->setEncoding("UTF-8"); $converter->setExcludeMediaQueries(true); $converter->setCleanup(false); $text_html = $converter->convert(); $text_part = new Zend\Mime\Part($text_plain); $text_part->type = "text/plain"; $text_part->charset = "UTF-8"; $html_part = new Zend\Mime\Part($text_html); $html_part->type = "text/html"; $html_part->charset = "UTF-8"; $mimem = new Zend\Mime\Message(); $mimem->setParts([$text_part, $html_part]); $mail->setBody($mimem); $mail->getHeaders()->get('content-type')->setType('multipart/alternative'); } else { $mail->setBody($text_plain); $headers = $mail->getHeaders(); $headers->removeHeader('Content-Type'); $headers->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8'); } $transport = new Zend\Mail\Transport\Sendmail(); $transport->send($mail); }