/** * @param Message $message * @return Message */ public function render(Message $message) { $viewModel = new ViewModel($this->variables); $viewModel->setTemplate($this->template); $helperPluginManager = $this->renderer->getHelperPluginManager(); /** @var HeadTitle $helper */ $helper = $helperPluginManager->get('HeadTitle'); // replace headTitle $headTitle = new HeadTitle(); $headTitle->setAutoEscape(false); $helperPluginManager->setAllowOverride(true); $helperPluginManager->setService('HeadTitle', $headTitle); if (!$message->getBody()) { $message->setBody(new MimeMessage()); } $text = new Part($this->renderer->render($viewModel)); $text->charset = 'UTF-8'; $text->boundary = $message->getBody()->getMime()->boundary(); $text->encoding = Mime::ENCODING_BASE64; $text->type = Mime::TYPE_HTML; $message->getBody()->addPart($text); $message->setSubject($headTitle->renderTitle()); // hack for ZF $message->setBody($message->getBody()); // restore original helper $helperPluginManager->setService('HeadTitle', $helper); return $message; }
/** * Inject files to message * * @param Message $message * @param array $files Paths to files */ public function injectFiles(Message $message, $files) { foreach ($files as $file) { $attachment = new Part(fopen($file, 'r')); $attachment->type = mime_content_type($file); $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $attachment->filename = basename($file); $attachment->boundary = $message->getBody()->getMime()->boundary(); $message->getBody()->addPart($attachment); } // hack for ZF $message->setBody($message->getBody()); }
/** * Send a mail message * * @param Mail\Message $message * @return array */ public function send(Mail\Message $message) { $this->getMandrillClient(); $body = $message->getBody(); $attachments = []; switch (true) { case $body instanceof Message: $bodyHtml = $this->getHtmlPart($body); $bodyText = $this->getTextPart($body); $attachments = $this->getAttachments($body); break; case is_string($body): $bodyHtml = $body; $bodyText = $message->getBodyText(); break; case is_object($body): $bodyHtml = $body->__toString(); $bodyText = $message->getBodyText(); break; default: throw new Exception\InvalidArgumentException(sprintf('"%s" expectes a body that is a string, an object or a Zend\\Mime\\Message; received "%s"', __METHOD__, is_object($body) ? get_class($body) : gettype($body))); break; } $message = ['html' => $bodyHtml, 'text' => $bodyText, 'subject' => $message->getSubject(), 'from_email' => $message->getFrom()->current()->getEmail(), 'from_name' => $message->getFrom()->current()->getName(), 'to' => array_merge($this->mapAddressListToArray($message->getTo(), 'to'), $this->mapAddressListToArray($message->getCc(), 'cc'), $this->mapAddressListToArray($message->getBcc(), 'bcc')), 'headers' => $message->getHeaders()->toArray(), 'subaccount' => $this->options->getSubAccount(), 'attachments' => $attachments]; return $this->mandrillClient->messages->send($message); }
public function send(SmtpTransport $transport = null, Message $message, array $attachments = array()) { if ($transport == null) { $transport = new Sendmail(); } $content = $message->getBody(); $parts = $attachments; $parts = array(); $bodyMessage = new \Zend\Mime\Message(); $multiPartContentMessage = new \Zend\Mime\Message(); $text = new Part(html_entity_decode(strip_tags(str_replace("<br />", "\n", $content)))); $text->type = "text/plain"; $text->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $text->charset = 'UTF-8'; $multiPartContentMessage->addPart($text); $html = new Part($content); $html->type = Mime::TYPE_HTML; $html->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $html->charset = 'utf-8'; $multiPartContentMessage->addPart($html); $multiPartContentMimePart = new Part($multiPartContentMessage->generateMessage()); $multiPartContentMimePart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $multiPartContentMessage->getMime()->boundary() . '"'; $bodyMessage->addPart($multiPartContentMimePart); foreach ($attachments as $attachment) { $bodyMessage->addPart($attachment); } $message->setBody($bodyMessage); $message->setEncoding("UTF-8"); $transport->send($message); }
function addAttachment($data, $filename, $mimetype) { $body = $this->mail->getBody(); if (!$body instanceof \Zend\Mime\Message) { $this->convertBodyToMime($body); $body = $this->mail->getBody(); } $attachment = new Zend\Mime\Part($data); $attachment->setFileName($filename); $attachment->setType($mimetype); $attachment->setEncoding(Zend\Mime\Mime::ENCODING_BASE64); $attachment->setDisposition(Zend\Mime\Mime::DISPOSITION_INLINE); $body->addPart($attachment); }
/** * Attaches files to the message if any */ private function attachFiles() { if (count($this->attachments) === 0) { return; } // Get old message parts $mimeMessage = $this->message->getBody(); if (is_string($mimeMessage)) { $originalBodyPart = new Mime\Part($mimeMessage); $originalBodyPart->type = $mimeMessage != strip_tags($mimeMessage) ? Mime\Mime::TYPE_HTML : Mime\Mime::TYPE_TEXT; // A Mime\Part body will be wraped into a Mime\Message, ensuring we handle a Mime\Message after this point $this->setBody($originalBodyPart); $mimeMessage = $this->message->getBody(); } $oldParts = $mimeMessage->getParts(); // Generate a new Mime\Part for each attachment $attachmentParts = []; $info = new \finfo(FILEINFO_MIME_TYPE); foreach ($this->attachments as $key => $attachment) { if (!is_file($attachment)) { continue; // If checked file is not valid, continue to the next } // If the key is a string, use it as the attachment name $basename = is_string($key) ? $key : basename($attachment); $part = new Mime\Part(fopen($attachment, 'r')); $part->id = $basename; $part->filename = $basename; $part->type = $info->file($attachment); $part->encoding = Mime\Mime::ENCODING_BASE64; $part->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; $attachmentParts[] = $part; } $body = new Mime\Message(); $body->setParts(array_merge($oldParts, $attachmentParts)); $this->message->setBody($body); }
public function testMaySetNullBody() { $this->message->setBody(null); $this->assertNull($this->message->getBody()); }
/** * Extract all attachments from a message * * Attachments are detected in the Mime message where * the type of the mime part is not text/plain or * text/html. * * @param Message $message * @return \Zend\Mime\Part[]|array */ protected function extractAttachments(Message $message) { $body = $message->getBody(); // If body is not a MimeMessage object, then the body is just the text version if (is_string($body) || !$body instanceof MimeMessage) { return array(); } $filter = array('text/plain', 'text/html'); $attachments = array(); foreach ($body->getParts() as $part) { if (!in_array($part->type, $filter) || $part->disposition === Mime::DISPOSITION_ATTACHMENT) { $attachments[] = $part; } } return $attachments; }
/** * @param MailMessage $message */ public function __construct(MailMessage $message) { $this->message = $message; $this->textBody = $message->getBody(); }
/** * Grab From Email * * Return the matches of a regex against the raw email * * @return void * @author Koen Punt **/ protected function grabMatchesFromEmail(Message $email, $regex) { preg_match($regex, $email->getBody(), $matches); $this->assertNotEmpty($matches, "No matches found for {$regex}"); return $matches; }
public function send(Message $message, array $attachments = array()) { if ($this->getConfiguration()->getSendAllMailsToBcc() !== null) { $message->addBcc($this->getConfiguration()->getSendAllMailsToBcc()); } if ($this->getConfiguration()->getSendAllMailsTo() != null) { $message->setTo($this->getConfiguration()->getSendAllMailsTo()); } $content = $message->getBody(); $bodyMessage = new \Zend\Mime\Message(); $multiPartContentMessage = new \Zend\Mime\Message(); $text = new Part(strip_tags($content)); $text->type = Mime::TYPE_TEXT; $text->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $multiPartContentMessage->addPart($text); $html = new Part($content); $html->type = Mime::TYPE_HTML; $html->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $html->charset = 'utf-8'; $multiPartContentMessage->addPart($html); $multiPartContentMimePart = new Part($multiPartContentMessage->generateMessage()); $multiPartContentMimePart->charset = 'UTF-8'; $multiPartContentMimePart->type = 'multipart/alternative'; $multiPartContentMimePart->boundary = $multiPartContentMessage->getMime()->boundary(); $bodyMessage->addPart($multiPartContentMimePart); foreach ($attachments as $attachment) { $bodyMessage->addPart($attachment); } $message->setBody($bodyMessage); $message->setEncoding("UTF-8"); $this->transport->send($message); }
/** * Get Message body (sha1) hash. * * @param Message $message * @return string */ private function getBodyHash(Message $message) { return base64_encode(pack("H*", sha1($message->getBody()))); }
/** * Create `Mandrill message` from `ZF message`. * * @param ZendMessage $zfMessage * * @return Message */ public static function convertZFMail(ZendMessage $zfMessage) { $mandrillMessage = new self(); // GET HTML MIME PART $messageHtmlPart = $messageTextPart = null; $mimeParts = $zfMessage->getBody()->getParts(); foreach ($mimeParts as $mimePart) { switch ($mimePart->getType()) { case ZendMime::TYPE_HTML: $messageHtmlPart = $mimePart->getContent(); break; case ZendMime::TYPE_TEXT: $messageTextPart = $mimePart->getContent(); break; } } // text part if ($messageTextPart !== null) { $mandrillMessage->text = $messageTextPart; } // html part if ($messageHtmlPart !== null) { $mandrillMessage->html = $messageHtmlPart; } // subject if ($zfMessage->getSubject() !== null) { $mandrillMessage->subject = $zfMessage->getSubject(); } // from if ($zfMessage->getFrom()->count()) { $mandrillMessage->from_email = $zfMessage->getFrom()->current()->getEmail(); $mandrillMessage->from_name = $zfMessage->getFrom()->current()->getName(); } // reply-to if ($zfMessage->getReplyTo()->count()) { $mandrillMessage->headers = ['Reply-To' => $zfMessage->getReplyTo()->current()->getEmail()]; } // to if ($zfMessage->getTo()->count()) { foreach ($zfMessage->getTo() as $to) { $mandrillMessage->addRecipient(new Recipient($to->getEmail(), null, Recipient::RECIPIENT_TYPE_TO)); } } // cc if ($zfMessage->getCc()->count()) { foreach ($zfMessage->getCc() as $cc) { $mandrillMessage->addRecipient(new Recipient($cc->getEmail(), null, Recipient::RECIPIENT_TYPE_CC)); } } // bcc if ($zfMessage->getBcc()->count()) { foreach ($zfMessage->getBcc() as $bcc) { $mandrillMessage->addRecipient(new Recipient($bcc->getEmail(), null, Recipient::RECIPIENT_TYPE_BCC)); } } return $mandrillMessage; }