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); }
public function sendEmail($from, $to, $cc, $subject, $body, $attachmentFilename) { $message = new Message(); $message->setFrom($from); $message->setTo($to); $message->setCc($cc); $message->setSubject($subject); $mimeMessage = new \Zend\Mime\Message(); $part = new \Zend\Mime\Part($body); $part->setType(Mime::TYPE_TEXT); $part->setCharset('UTF-8'); $mimeMessage->addPart($part); $part = new \Zend\Mime\Part('<p>' . $body . '<p>'); $part->setType(Mime::TYPE_HTML); $part->setCharset('UTF-8'); $mimeMessage->addPart($part); $part = new \Zend\Mime\Part($body); $part->setType(Mime::TYPE_OCTETSTREAM); $part->setEncoding(Mime::ENCODING_BASE64); $part->setFileName($attachmentFilename); $part->setDisposition(Mime::DISPOSITION_ATTACHMENT); $mimeMessage->addPart($part); $message->setBody($mimeMessage); $this->transport->send($message); $this->debugSection('ZendMailer', $subject . ' ' . $from . ' -> ' . $to); }
/** * Requires Fileinfo. */ private function addAttachments() { if (class_exists('Finfo', false)) { $finfo = new \Finfo(FILEINFO_MIME_TYPE); } else { $finfo = false; } foreach ($this->mail->attachments as $filename => $attachment) { if (!is_array($attachment)) { throw new Exception\RuntimeException(sprintf("Attachments must be array, %s passed", gettype($attachment))); } elseif (!is_string($attachment['content']) && (!is_resource($attachment['content']) || !get_resource_type($attachment['content']) == 'stream')) { throw new Exception\RuntimeException(sprintf("Attachment content must be string or stream, %s passed", gettype($attachment['content']))); } $type = null; if (empty($attachment['mime_type']) && $finfo) { if (is_resource($attachment['content'])) { $type = $finfo->buffer(stream_get_contents($attachment['content'])); rewind($attachment['content']); } else { $type = $finfo->buffer($attachment['content']); } } $part = new Mime\Part($attachment['content']); if (empty($attachment['encoding'])) { $attachment['encoding'] = Mime\Mime::ENCODING_BASE64; } $part->encoding = $attachment['encoding']; if ($type) { $part->type = $type; } $part->disposition = !empty($attachment['inline']) ? Mime\Mime::DISPOSITION_INLINE : Mime\Mime::DISPOSITION_ATTACHMENT; $this->body->addPart($part); } }
protected function convertBodyToMime($text) { $textPart = new Zend\Mime\Part($text); $textPart->setType(Zend\Mime\Mime::TYPE_TEXT); $newBody = new Zend\Mime\Message(); $newBody->addPart($textPart); $this->mail->setBody($newBody); }
/** * Create a email based on it's theme an params * * @param string $address * @param string $name * @throws Exception * @return \Zend\Mail\Message */ protected function _constructEmail($address, $name) { $content = $this->_template->render(); if ('' == $this->_replyTo) { $this->_replyTo = $this->_template->getTestament()->getReplyTo(); } if ('' == $this->_fromName) { $this->_fromName = $this->_template->getTestament()->getFromName(); } if ('' == $this->_fromAddress) { $this->_fromAddress = $this->_template->getTestament()->getFromAddress(); } if ('' == $this->_subject) { $this->_subject = $this->_template->getTestament()->getFromAddress(); } $contentParts = array(); $partText = new Part($content->getText()); $partText->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $partText->type = Mime::TYPE_TEXT; $contentParts[] = $partText; $partHtml = new Part($content->getHtml()); $partHtml->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $partHtml->type = Mime::TYPE_HTML; $partHtml->charset = 'UTF-8'; $contentParts[] = $partHtml; $alternatives = new \Zend\Mime\Message(); $alternatives->setParts($contentParts); $alternativesPart = new Part($alternatives->generateMessage()); $alternativesPart->type = "multipart/alternative; boundary=\"" . $alternatives->getMime()->boundary() . "\""; $body = new \Zend\Mime\Message(); $body->addPart($alternativesPart); foreach ($this->_attachments as $attachmentSrc) { $attachment = new Part(fopen($attachmentSrc['filelocation'], 'r')); $attachment->filename = $attachmentSrc['filename']; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->type = Mime::DISPOSITION_ATTACHMENT; $attachment->disposition = true; $body->addPart($attachment); } $subject = $this->_subject; foreach ($this->_variables as $name => $variable) { $subject = str_replace('{{:' . $name . ':}}', $variable, $subject); } $message = new Message(); $message->setSubject($subject); $message->setFrom($this->_fromAddress, $this->_fromName); if ($this->_replyTo) { $message->setReplyTo($this->_replyTo); } $message->setBody($body); $message->setTo($address, $name); $message->setEncoding("UTF-8"); return $message; }
protected function convertMailMessage($mail) { if (!$mail instanceof \Zend_Mail) { throw new \InvalidArgumentException('The message should be an instance of \\Zend_Mail'); } //convert zend_mail1 to zend\mail\message // \Zend_Debug::dump($mail->getFrom()); // \Zend_Debug::dump(get_class_methods($mail)); // \Zend_Debug::dump($mail->getHeader('To')); // \Zend_Debug::dump($mail->getHeaders()); $headers = new \Zend\Mail\Headers(); $_headers = []; foreach ($mail->getHeaders() as $headerName => $values) { foreach ($values as $key => $value) { if ($key !== 'append') { $_headers[$headerName][$key] = $value; } } } $headers->addHeaders($_headers); $headersEncoding = $mail->getHeaderEncoding(); $headers->setEncoding($headersEncoding); $_message = new \Zend\Mail\Message(); $_message->setHeaders($headers); $body = new \Zend\Mime\Message(); $charset = $mail->getCharset(); $text = $mail->getBodyText(); if (!empty($text)) { if ($text instanceof \Zend_Mime_Part) { $part = new \Zend\Mime\Part($text->getContent()); $part->encoding = $text->encoding; $part->type = $text->type; $part->charset = $text->charset; } elseif (is_string($text)) { $part = new \Zend\Mime\Part($text); $part->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; $part->type = \Zend\Mime\Mime::TYPE_TEXT; $part->charset = $charset; } $body->addPart($part); } $html = $mail->getBodyHtml(); if (!empty($html)) { if ($html instanceof \Zend_Mime_Part) { $part = new \Zend\Mime\Part($html->getContent()); $part->encoding = $html->encoding; $part->type = $html->type; $part->charset = $html->charset; } elseif (is_string($html)) { $part = new \Zend\Mime\Part($html); $part->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; $part->type = \Zend\Mime\Mime::TYPE_TEXT; $part->charset = $charset; } $body->addPart($part); } //@todo $mail->getParts() copy attachments $_message->setBody($body); // \Zend_Debug::dump($_message); // die; return $_message; }
/** * Send an email to any email address * * @param mixed $from Email address or string: "name <email>" * @param mixed $to Email address or string: "name <email>" * @param string $subject The subject of the message * @param string $body The message body * @param array $params Optional parameters * @return bool * @throws NotificationException */ function notifications_html_handler_send_email($from, $to, $subject, $body, array $params = null) { $options = array('to' => $to, 'from' => $from, 'subject' => $subject, 'body' => $body, 'params' => $params, 'headers' => array("Content-Type" => "text/html; charset=UTF-8; format=flowed", "MIME-Version" => "1.0", "Content-Transfer-Encoding" => "8bit")); // $mail_params is passed as both params and return value. The former is for backwards // compatibility. The latter is so handlers can now alter the contents/headers of // the email by returning the array $options = elgg_trigger_plugin_hook('email', 'system', $options, $options); if (!is_array($options)) { // don't need null check: Handlers can't set a hook value to null! return (bool) $options; } try { if (empty($options['from'])) { $msg = "Missing a required parameter, '" . 'from' . "'"; throw new \NotificationException($msg); } if (empty($options['to'])) { $msg = "Missing a required parameter, '" . 'to' . "'"; throw new \NotificationException($msg); } $options['to'] = \Elgg\Mail\Address::fromString($options['to']); $options['from'] = \Elgg\Mail\Address::fromString($options['from']); $options['subject'] = elgg_strip_tags($options['subject']); $options['subject'] = html_entity_decode($options['subject'], ENT_QUOTES, 'UTF-8'); // Sanitise subject by stripping line endings $options['subject'] = preg_replace("/(\r\n|\r|\n)/", " ", $options['subject']); $options['subject'] = elgg_get_excerpt(trim($options['subject'], 80)); $message = new \Zend\Mail\Message(); foreach ($options['headers'] as $headerName => $headerValue) { $message->getHeaders()->addHeaderLine($headerName, $headerValue); } $message->setEncoding('UTF-8'); $message->addFrom($options['from']); $message->addTo($options['to']); $message->setSubject($options['subject']); $body = new Zend\Mime\Message(); $html = new \Zend\Mime\Part($options['body']); $html->type = "text/html"; $body->addPart($html); $files = elgg_extract('attachments', $options['params']); if (!empty($files) && is_array($files)) { foreach ($files as $file) { if (!$file instanceof \ElggFile) { continue; } $attachment = new \Zend\Mime\Part(fopen($file->getFilenameOnFilestore(), 'r')); $attachment->type = $file->getMimeType() ?: $file->detectMimeType(); $attachment->filename = $file->originalfilename ?: basename($file->getFilename()); $attachment->disposition = Zend\Mime\Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Zend\Mime\Mime::ENCODING_BASE64; $body->addPart($attachment); } } $message->setBody($body); $transport = notifications_html_handler_get_transport(); if (!$transport instanceof Zend\Mail\Transport\TransportInterface) { throw new \NotificationException("Invalid Email transport"); } $transport->send($message); } catch (\Exception $e) { elgg_log($e->getMessage(), 'ERROR'); return false; } return true; }
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); }
<?php require_once 'Zend/Loader/StandardAutoloader.php'; $loader = new Zend\Loader\StandardAutoloader(); $loader->register(); $text = new Zend\Mime\Part('Plain Text'); $text->encoding = Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; $text->type = Zend\Mime\Mime::TYPE_TEXT; $text->charset = 'UTF-8'; $html = new Zend\Mime\Part('<b>HTML</b>'); $html->encoding = Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; $html->type = Zend\Mime\Mime::TYPE_HTML; $html->charset = 'UTF-8'; $message = new Zend\Mime\Message(); $message->addPart($text); $message->addPart($html); $mail = new Zend\Mail\Message('UTF-8'); $mail->setFrom('*****@*****.**'); $mail->setSubject('ZF2 Multipart Mail'); $mail->setBody($message); $mail->addTo('*****@*****.**'); $transport = new Zend\Mail\Transport\Smtp(); $transport->send($mail);
public function handle(EventInterface $event) { $user = $event->getTarget(); $extractor = new PlainExtractor($this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'), '.'); $data = $event->getParam('data'); if (!is_array($data)) { $data = $extractor->extract($data); } $data = $data + $extractor->extract($user, 'user') + $extractor->extract($user->getUserInfo(), 'userinfo'); /** * @var \Mailer\Model\NotificationModel $notifyModel */ $notifyModel = $this->getServiceLocator()->get('Mailer/Model/Notification'); $notificationTpl = $notifyModel->findBySlug($this->convertNameToSlug($event->getName())); if (null == $notificationTpl) { $this->getLog()->crit('Notification template "' . $this->convertNameToSlug($event->getName()) . '" not found', array('template_vars' => implode(',', array_keys($data)))); throw new \Mailer\Model\Exception\NotificationTemplateNotFound('Notification template "' . $this->convertNameToSlug($event->getName()) . '" not found'); } $translation = $notifyModel->getNotificationTranslation($notificationTpl); $messageOptions = $this->getMailSettings(); $mail = new \Mailer\Mail\Message(); $mail->setEncoding($messageOptions['encoding']); if (null != $notificationTpl->getEmailFrom()) { $mail->setFrom($notificationTpl->getEmailFrom(), $translation->getNameFrom()); $mail->setSender($notificationTpl->getEmailFrom(), $translation->getNameFrom()); } else { $mail->setFrom($messageOptions['defaultFrom'], $messageOptions['defaultFromName']); $mail->setSender($messageOptions['defaultFrom'], $messageOptions['defaultFromName']); } $mail->setTo($user->getEmail(), $user->getUserInfo()->getFullName()); $mail->setSubject($translation->compileSubject($data)); $body = new \Zend\Mime\Message(); if (null != $translation->getNotificationHtml()) { $part = new \Zend\Mime\Part($translation->compileNotificationHtml($data)); $part->type = "text/html"; $part->charset = $messageOptions['encoding']; $part->language = $translation->getLanguageId(); $body->addPart($part); } if (null != $translation->getNotificationText()) { $part = new \Zend\Mime\Part($translation->compileNotificationText($data)); $part->type = "text/plain"; $part->charset = $messageOptions['encoding']; $part->language = $translation->getLanguageId(); $body->addPart($part); } $mail->setBody($body); $this->getTransport()->send($mail); }