public function sendHTML($to, $from, $subject, $html, $attachments = false, $headers = false, $plain = false, $inlineImages = false) { if ($inlineImages) { user_error('The SES mailer does not support inlining images', E_USER_NOTICE); } $htmlPart = new Mime\Part($html); $htmlPart->type = Mime\Mime::TYPE_HTML; $htmlPart->charset = 'utf-8'; $htmlPart->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $plainPart = new Mime\Part($plain ?: \Convert::xml2raw($html)); $plainPart->type = Mime\Mime::TYPE_TEXT; $plainPart->charset = 'utf-8'; $plainPart->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $alternatives = new Mime\Message(); $alternatives->setParts(array($plainPart, $htmlPart)); $alternativesPart = new Mime\Part($alternatives->generateMessage()); $alternativesPart->type = Mime\Mime::MULTIPART_ALTERNATIVE; $alternativesPart->boundary = $alternatives->getMime()->boundary(); $body = new Mime\Message(); $body->addPart($alternativesPart); if ($attachments) { $this->addAttachments($body, $attachments); } $this->sendMessage($to, $from, $subject, $body, $headers); }
/** * sendMail * @param string $subject * @param array $aMail */ public function sendMail($subject = '', $aMail = array()) { $message = new Message(); $message->setEncoding("UTF-8"); $message->setFrom($this->aMailConf['mailFromMail'], $this->aMailConf['mailFromName']); foreach ($aMail as $mail) { //destinatários $message->addTo($mail); } //assunto $message->setSubject($subject); //cria o corpo da mensagem $body = new MimeMessage(); $contentPart = new MimePart($this->content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $this->content->getMime()->boundary() . '"'; $messageType = 'multipart/related'; //adiciona o html e o txt $body->addPart($contentPart); foreach ($this->aAttachments as $attachment) { $body->addPart($attachment); } //monta o corpo $message->setBody($body); $message->getHeaders()->get('content-type')->setType($messageType); //enviar try { $this->transport->send($message); } catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { return $e; } return true; }
/** * Creates a multi-part message body with plain text and HTML (from Markdown) * * @param string $text * @return \Zend\Mime\Message */ public function createMultiPartBody($text) { $plain = new MimePart($text); $plain->type = "text/plain"; $markdown = new Markdown(); $html = new MimePart($markdown->transform($text)); $html->type = "text/html"; $alternatives = new MimeMessage(); $alternatives->setParts(array($plain, $html)); $alternatives->type = "multipart/alternative"; $part = new MimePart($alternatives->generateMessage()); $part->type = "multipart/alternative;\n boundary=\"" . $alternatives->getMime()->boundary() . "\""; $body = new MimeMessage(); $body->addPart($part); return $body; }
/** * Set body with optional attachments * * @param \Zend\Mime\Message|strin $body * @param array $attachments * @return type */ public function setBody($body, $html = null, array $attachments = []) { if (!is_object($body) && (is_string($html) || $attachments)) { $parts = []; if (!is_null($body)) { $text = new Mime\Part($body); $text->type = Mime\Mime::TYPE_TEXT; $text->charset = $this->encoding ?: 'utf-8'; $text->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $parts[] = $text; } if (!is_null($html)) { $html = new Mime\Part($html); $html->type = Mime\Mime::TYPE_HTML; $html->charset = $this->encoding ?: 'utf-8'; $html->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $parts[] = $html; $type = 'multipart/alternative'; if ($attachments) { $content = new Mime\Message(); $content->setParts($parts); $part = new Mime\Part($content->generateMessage()); $part->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; $parts = [$part]; $type = 'multipart/related'; } } foreach ($attachments as $path) { $resource = fopen($path, 'r'); $attachment = new Mime\Part($resource); $attachment->type = mime_content_type($path); $attachment->filename = basename($path); $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Mime\Mime::ENCODING_BASE64; $parts[] = $attachment; } $body = new Mime\Message(); $body->setParts($parts); } parent::setBody($body); if (isset($type)) { $this->getHeaders()->get('content-type')->setType($type); } return $this; }
public function testGenerate() { $msg = new Mime\Message(); // No Parts $p1 = new Mime\Part('This is a test'); $p2 = new Mime\Part('This is another test'); $msg->addPart($p1); $msg->addPart($p2); $res = $msg->generateMessage(); $mime = $msg->getMime(); $boundary = $mime->boundary(); $p1 = strpos($res, $boundary); // $boundary must appear once for every mime part $this->assertTrue($p1 !== false); if ($p1) { $p2 = strpos($res, $boundary, $p1 + strlen($boundary)); $this->assertTrue($p2 !== false); } // check if the two test messages appear: $this->assertTrue(strpos($res, 'This is a test') !== false); $this->assertTrue(strpos($res, 'This is another test') !== false); // ... more in ZMailTest }
/** * @param $body * @param null $mimeType * @return array */ public function getMessageBody($body, $mimeType = null) { // Make sure we have a string. if ($body instanceof ViewModel) { $body = $this->getView()->render($body); $detectedMimeType = Mime::TYPE_HTML; } elseif (null === $body) { $detectedMimeType = Mime::TYPE_TEXT; $body = ''; } if (null !== ($layout = $this->getLayout())) { $layout->setVariables(array('content' => $body)); $detectedMimeType = Mime::TYPE_HTML; $body = $this->parseTemplate($layout); } if (null === $mimeType && !isset($detectedMimeType)) { $mimeType = preg_match("/<[^<]+>/", $body) ? Mime::TYPE_HTML : Mime::TYPE_TEXT; } elseif (null === $mimeType) { $mimeType = $detectedMimeType; } $htmlPart = new MimePart($body); $htmlPart->type = $mimeType; $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; if (null !== ($charset = $this->getOption('charset'))) { $htmlPart->charset = $charset; } if ($mimeType === Mime::TYPE_HTML) { $text = $this->renderTextBody($body); $textPart = new MimePart($text); $textPart->type = Mime::TYPE_TEXT; $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; if ($this->getOption('charset')) { $textPart->charset = $this->getOption('charset'); } } $bodyMessage = new MimeMessage(); if (count($this->attachments) > 0) { $content = new MimeMessage(); if (isset($textPart)) { $content->addPart($textPart); } if (isset($content)) { $content->addPart($htmlPart); } $contentPart = new MimePart($content->generateMessage()); $contentPart->type = 'multipart/alternative; boundary="' . $content->getMime()->boundary() . '"'; $bodyMessage->addPart($contentPart); $messageType = Mime::MULTIPART_RELATED; foreach ($this->attachments as $attachment) { $bodyMessage->addPart($attachment); } } else { if (isset($textPart)) { $bodyMessage->addPart($textPart); } if (isset($htmlPart)) { $bodyMessage->addPart($htmlPart); } $messageType = Mime::MULTIPART_ALTERNATIVE; } return ['body' => $bodyMessage, 'type' => $messageType]; }
function sendEmail($to, $subject, $html, $text, $attachments = null) { // HTML part $htmlPart = new Mime\Part($html); $htmlPart->setEncoding(Mime\Mime::ENCODING_QUOTEDPRINTABLE); $htmlPart->setType(Mime\Mime::TYPE_HTML); // Plain text part $textPart = new Mime\Part($text); $textPart->setEncoding(Mime\Mime::ENCODING_QUOTEDPRINTABLE); $textPart->setType(Mime\Mime::TYPE_TEXT); $body = new Mime\Message(); if ($attachments) { // With attachments, we need a multipart/related email. First part // is itself a multipart/alternative message $content = new Mime\Message(); $content->addPart($textPart); $content->addPart($htmlPart); $contentPart = new Mime\Part($content->generateMessage()); $contentPart->setType(Mime\Mime::MULTIPART_ALTERNATIVE); $contentPart->setBoundary($content->getMime()->boundary()); $body->addPart($contentPart); $messageType = Mime\Mime::MULTIPART_RELATED; // Add each attachment foreach ($attachments as $thisAttachment) { $attachment = new Mime\Part($thisAttachment['content']); $attachment->filename = $thisAttachment['filename']; $attachment->type = Mime\Mime::TYPE_OCTETSTREAM; $attachment->encoding = Mime\Mime::ENCODING_BASE64; $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; $body->addPart($attachment); } } else { // No attachments, just add the two textual parts to the body $body->setParts([$textPart, $htmlPart]); $messageType = Mime\Mime::MULTIPART_ALTERNATIVE; } // attach the body to the message and set the content-type $message = new Message(); $message->addTo($to); $message->setEncoding($this->encoding); $message->addFrom($this->fromEmail, $this->fromName); $message->setSubject($subject); $message->setBody($body); $message->getHeaders()->get('content-type')->setType($messageType); $this->getTransport()->send($message); }
public function sendEmail($template = 'message', $emailOptions = array()) { $emailOptions = array_merge($this->config, $emailOptions); $content = $this->renderEmail($template, $emailOptions); if ($emailOptions['debug']) { $displays = array(); foreach ($emailOptions as $key => $value) { if ($key !== 'message' && $key != 'attachments') { if (!is_object($value)) { $displays[] = $key . ': <strong>' . (is_array($value) ? implode(',', $value) : $value) . '</strong>'; } else { $displays[] = $key . ': <strong>OBJ</strong>'; } } } echo '<div style=" background-color: #444; padding: 50px; box-shadow: 1px 1px 10px rgba(0,0,0,0.8) inset; "> <div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); padding: 15px; " > ' . implode(' ; ', $displays) . ' </div> <div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); " >' . $content . '</div>' . (isset($emailOptions['attachments']) && $emailOptions['attachments'] ? '<div style=" width:80%; margin:10px auto; background-color:#ffffff; box-shadow:1px 2px 5px rgba(0,0,0,0.5); padding: 15px; " >Mit Anhang</div>' : '') . '</div>'; } $attachments = isset($emailOptions['attachments']) && $emailOptions['attachments'] && is_array($emailOptions['attachments']) ? $emailOptions['attachments'] : array(); $message = new Message(); $message->addTo($emailOptions['to']); $message->addFrom($emailOptions['from']); $message->setSubject($emailOptions['subject']); if ($emailOptions['bcc']) { $message->addBcc($emailOptions['bcc']); } if ($emailOptions['cc']) { $message->addCc($emailOptions['cc']); } if ($this->html) { // HTML part $htmlPart = new MimePart($content); $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $htmlPart->type = "text/html; charset=UTF-8"; } // Plain text part $textPart = new MimePart(strip_tags($content)); $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $textPart->type = "text/plain; charset=UTF-8"; $body = new MimeMessage(); if ($attachments) { // With attachments, we need a multipart/related email. First part // is itself a multipart/alternative message $content = new MimeMessage(); $content->addPart($textPart); if ($this->html) { $content->addPart($htmlPart); } $contentPart = new MimePart($content->generateMessage()); $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; $body->addPart($contentPart); $messageType = 'multipart/related'; // Add each attachment foreach ($attachments as $thisAttachment) { $attachment = new MimePart($thisAttachment['buffer']); $attachment->filename = $thisAttachment['filename']; $attachment->type = Mime::TYPE_OCTETSTREAM; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body->addPart($attachment); } } else { // No attachments, just add the two textual parts to the body if ($this->html) { $body->setParts(array($textPart, $htmlPart)); $messageType = 'multipart/alternative'; } else { $body->setParts(array($textPart)); $messageType = 'text/plain'; } } // attach the body to the message and set the content-type $message->setBody($body); $message->getHeaders()->get('content-type')->setType($messageType); $message->setEncoding('UTF-8'); if ($emailOptions['send']) { if (isset($emailOptions['smtp']) && $emailOptions['smtp'] == 'google') { $transport = new SmtpTransport(); $options = new SmtpOptions(array('name' => 'casamail.com', 'host' => 'smtp.gmail.com', 'port' => 465, 'connection_class' => 'login', 'connection_config' => array('username' => $emailOptions['smtp_username'], 'password' => $emailOptions['smtp_password'], 'ssl' => 'ssl'))); $transport->setOptions($options); } else { $transport = new SendmailTransport(); } try { $transport->send($message); } catch (\Exception $e) { if (!get_class($transport) == 'Sendmail') { //try with postfix $transport = new SendmailTransport(); $transport->send($message); } } } else { echo '<h1>E-Mail <strong>NOT</strong> sent</h1>'; } return $content; }
/** * Sends the email. * * @param bool $resetData Whether or not to reset the service to its default values. * * @throws \Exception * * @return bool */ public function sendMail($resetData = true) { $_from[] = $this->from; $fromStr = $this->generateEmailStrings($_from); $toStr = $this->generateEmailStrings($this->to); $replyStr = $this->generateEmailStrings($this->replyto); $zendMailData = self::getMailerObject(); $mail = $zendMailData['mail']; $transport = $zendMailData['transport']; if (is_array($this->from) && count($this->from)) { if ($this->from[0] != '') { $from = $this->from; } } if (!isset($from)) { $from = array(Config::get('concrete.email.default.address'), Config::get('concrete.email.default.name')); $fromStr = Config::get('concrete.email.default.address'); } // The currently included Zend library has a bug in setReplyTo that // adds the Reply-To address as a recipient of the email. We must // set the Reply-To before any header with addresses and then clear // all recipients so that a copy is not sent to the Reply-To address. if (is_array($this->replyto)) { foreach ($this->replyto as $reply) { $mail->setReplyTo($reply[0], $reply[1]); } } $mail->setFrom($from[0], $from[1]); $mail->setSubject($this->subject); foreach ($this->to as $to) { $mail->addTo($to[0], $to[1]); } if (is_array($this->cc) && count($this->cc)) { foreach ($this->cc as $cc) { $mail->addCc($cc[0], $cc[1]); } } if (is_array($this->bcc) && count($this->bcc)) { foreach ($this->bcc as $bcc) { $mail->addBcc($bcc[0], $bcc[1]); } } $headers = $mail->getHeaders(); if ($headers->has('messageid')) { $messageIdHeader = $headers->get('messageid'); } else { $messageIdHeader = new \Zend\Mail\Header\MessageId(); $headers->addHeader($messageIdHeader); } $headers->addHeaders($this->headers); $messageIdHeader->setId(); $body = new MimeMessage(); if ($this->body !== false && $this->bodyHTML !== false) { $alternatives = new MimeMessage(); $text = new MimePart($this->body); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $alternatives->addPart($text); $html = new MimePart($this->bodyHTML); $html->type = 'text/html'; $html->charset = APP_CHARSET; $alternatives->addPart($html); $alternativesPath = new MimePart($alternatives->generateMessage()); $alternativesPath->type = 'multipart/alternative;' . Mime::LINEEND . ' boundary="' . $alternatives->getMime()->boundary() . '"'; $body->addPart($alternativesPath); } elseif ($this->body !== false) { $text = new MimePart($this->body); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $body->addPart($text); } elseif ($this->bodyHTML !== false) { $html = new MimePart($this->bodyHTML); $html->type = 'text/html'; $html->charset = APP_CHARSET; $body->addPart($html); } foreach ($this->attachments as $att) { $body->addPart($att); } if (count($body->getParts()) === 0) { $text = new MimePart(''); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $body->addPart($text); } $mail->setBody($body); $sent = false; try { if (Config::get('concrete.email.enabled')) { $transport->send($mail); } $sent = true; } catch (Exception $e) { if ($this->getTesting()) { throw $e; } $l = new GroupLogger(LOG_TYPE_EXCEPTIONS, Logger::CRITICAL); $l->write(t('Mail Exception Occurred. Unable to send mail: ') . $e->getMessage()); $l->write($e->getTraceAsString()); if (Config::get('concrete.log.emails')) { $l->write(t('Template Used') . ': ' . $this->template); $l->write(t('To') . ': ' . $toStr); $l->write(t('From') . ': ' . $fromStr); if (isset($this->replyto)) { $l->write(t('Reply-To') . ': ' . $replyStr); } $l->write(t('Subject') . ': ' . $this->subject); $l->write(t('Body') . ': ' . $this->body); } $l->close(); } // add email to log if (Config::get('concrete.log.emails') && !$this->getTesting()) { $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); if (Config::get('concrete.email.enabled')) { $l->write('**' . t('EMAILS ARE ENABLED. THIS EMAIL WAS SENT TO mail()') . '**'); } else { $l->write('**' . t('EMAILS ARE DISABLED. THIS EMAIL WAS LOGGED BUT NOT SENT') . '**'); } $l->write(t('Template Used') . ': ' . $this->template); $l->write(t('Mail Details: %s', $mail->toString())); $l->close(); } // clear data if applicable if ($resetData) { $this->reset(); } return $sent; }
/** * Send Mail to User * * @param integer $id rentals id * @param array $data input data * @return RentalsEntityInterface */ public function sendMail($address, $subject, $content_text, $content_html, $rentals_data, $mail_settings) { $message = new Message(); $message->addTo($address)->setEncoding("UTF-8")->addFrom($mail_settings['email_sender'])->addBcc($mail_settings['email_receiver'])->setSubject($subject); // Set UTF-8 charset $headers = $message->getHeaders(); $headers->removeHeader('Content-Type'); $headers->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8'); $uid = "daslastenradat"; // setting this to an existing uid updates event, a new uid adds event $summary = 'Lastenrad ausleihen'; $tstart = date('Ymd\\THis', strtotime($rentals_data['from'])); $tend = date('Ymd\\THis', strtotime($rentals_data['to'])); $tstamp = gmdate("Ymd\\THis\\Z"); $ical = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nMETHOD:PUBLISH\nDTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:{$tstart}\nDTEND;TZID=Europe/Vienna;VALUE=DATE-TIME:{$tend}\nLOCATION:Verleihstation - siehe Website \nSEQUENCE:0\nUID:{$uid}\nDTSTAMP:{$tstamp}\nSUMMARY:{$summary}\nPRIORITY:5\nX-MICROSOFT-CDO-IMPORTANCE:1\nCLASS:PUBLIC\nEND:VEVENT\nEND:VCALENDAR"; $at = new MimePart($ical); $at->type = 'text/calendar'; $at->disposition = Mime::DISPOSITION_INLINE; $at->encoding = Mime::ENCODING_8BIT; $at->filename = 'termin.ics'; //Content-Type: text/calendar; method=REQUEST //Content-Transfer-Encoding: Base64 //Content-Disposition: attachment; filename=iCal-20140915-070904.ics //$message->addAttachment($at); $content = new MimeMessage(); $htmlPart = new MimePart($content_html); $htmlPart->type = 'text/html'; $textPart = new MimePart($content_text); $textPart->type = 'text/plain'; $content->setParts(array($textPart, $htmlPart)); $contentPart = new MimePart($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; $attachment = new MimePart(fopen('C:\\projekte\\lastenrad\\public\\downloads\\nutzungsbedingungen_freies_lastenrad.pdf', 'r')); $attachment->type = 'application/pdf'; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body = new MimeMessage(); $body->setParts(array($contentPart, $attachment, $at)); $message->setBody($body); //$transport->setOptions($options); $this->transport->send($message); return true; }
/** * Sends a mixed message via email * @param string[] $message * @param string[] $templates * @param string[] $variables * @return $this */ public function sendMixedMessage(array $message, array $templates, array $variables = []) { //create the message $mailMessage = $this->createMessage($message); //render the templates $contentMimeMessage = new MimeMessage(); foreach ($templates as $mimeType => $template) { //render the template $viewContent = $this->renderTemplate($template, $variables); //add the template to the message $mimePart = new MimePart($viewContent); $mimePart->type = $mimeType; $contentMimeMessage->addPart($mimePart); } //combine the alternative content into a single mime part if ($contentMimeMessage->isMultiPart()) { $contentMimePart = new MimePart($contentMimeMessage->generateMessage()); $contentMimePart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $contentMimeMessage->getMime()->boundary() . '"'; $contentMimeParts = [$contentMimePart]; } else { $contentMimeParts = $contentMimeMessage->getParts(); } //order the content before any attachments $finalMimeMessage = new MimeMessage(); $finalMimeMessage->setParts(array_merge($contentMimeParts, $mailMessage->getBody()->getParts())); $mailMessage->setBody($finalMimeMessage); //let the client choose which part to display if ($mailMessage->getBody()->isMultiPart()) { $mailMessage->getHeaders()->get('content-type')->setType('multipart/mixed'); } //send the message return $this->sendMessage($mailMessage); }
public function sendorderAction() { $pdfView = new ViewModel(); $pdfView->setTerminal(true)->setTemplate('Application/calculator/sendorder.phtml')->setVariables(array('fetchResult' => '789')); $html = $this->getServiceLocator()->get('viewpdfrenderer')->getHtmlRenderer()->render($pdfView); $eng = $this->getServiceLocator()->get('viewpdfrenderer')->getEngine(); $eng->load_html($html); $eng->render(); $pdfCode = $eng->output(); //file_put_contents($_SERVER['DOCUMENT_ROOT'].'/data/pdf/ac.pdf',$pdfCode); $content = new MimeMessage(); $htmlPart = new MimePart("<html><body><p>Sorry,</p><p>I'm going to be late today!Короче, опоздаю..</p></body></html>"); $htmlPart->type = 'text/html'; $textPart = new MimePart("Sorry, I'm going to be late today!"); $textPart->type = 'text/plain'; $content->setParts(array($textPart, $htmlPart)); $contentPart = new MimePart($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; //$attachment = new MimePart(fopen($_SERVER['DOCUMENT_ROOT'].'/data/pdf/ac.pdf', 'r')); $attachment = new MimePart($pdfCode); $attachment->type = 'application/pdf'; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body = new MimeMessage(); $body->setParts(array($contentPart, $attachment)); $message = new Message(); $message->setEncoding('utf-8')->addTo('*****@*****.**')->addFrom('*****@*****.**')->setSubject('will be late')->setBody($body); $transportConfig = new SmtpOptions(array('host' => 'smtp.gmail.com', 'connection_class' => 'login', 'connection_config' => array('ssl' => 'tls', 'username' => '*****@*****.**', 'password' => 'vikabibika0987654321'), 'port' => 587)); $transport = new SmtpTransport(); $options = new SmtpOptions($transportConfig); $transport->setOptions($options); $transport->send($message); //$this->redirect()->toRoute('zfcadmin/admin_manufacturers'); }
public function send(Email $email, $params = array(), &$message = null, $attachmetList = []) { if (!$message) { $message = new Message(); } $config = $this->config; $params = $this->params + $params; if ($email->get('from')) { $fromName = null; if (!empty($params['fromName'])) { $fromName = $params['fromName']; } else { $fromName = $config->get('outboundEmailFromName'); } $message->addFrom(trim($email->get('from')), $fromName); } else { if (!empty($params['fromAddress'])) { $fromAddress = $params['fromAddress']; } else { if (!$config->get('outboundEmailFromAddress')) { throw new Error('outboundEmailFromAddress is not specified in config.'); } $fromAddress = $config->get('outboundEmailFromAddress'); } if (!empty($params['fromName'])) { $fromName = $params['fromName']; } else { $fromName = $config->get('outboundEmailFromName'); } $message->addFrom($fromAddress, $fromName); } if (!$email->get('from')) { $email->set('from', $fromAddress); } if (!empty($params['replyToAddress'])) { $replyToName = null; if (!empty($params['replyToName'])) { $replyToName = $params['replyToName']; } $message->setReplyTo($params['replyToAddress'], $replyToName); } $value = $email->get('to'); if ($value) { $arr = explode(';', $value); if (is_array($arr)) { foreach ($arr as $address) { $message->addTo(trim($address)); } } } $value = $email->get('cc'); if ($value) { $arr = explode(';', $value); if (is_array($arr)) { foreach ($arr as $address) { $message->addCC(trim($address)); } } } $value = $email->get('bcc'); if ($value) { $arr = explode(';', $value); if (is_array($arr)) { foreach ($arr as $address) { $message->addBCC(trim($address)); } } } $value = $email->get('replyTo'); if ($value) { $arr = explode(';', $value); if (is_array($arr)) { foreach ($arr as $address) { $message->addReplyTo(trim($address)); } } } $attachmentPartList = array(); $attachmentCollection = $email->get('attachments'); $attachmentInlineCollection = $email->getInlineAttachments(); foreach ($attachmetList as $attachment) { $attachmentCollection[] = $attachment; } if (!empty($attachmentCollection)) { foreach ($attachmentCollection as $a) { $fileName = 'data/upload/' . $a->id; $attachment = new MimePart(file_get_contents($fileName)); $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->filename = $a->get('name'); if ($a->get('type')) { $attachment->type = $a->get('type'); } $attachmentPartList[] = $attachment; } } if (!empty($attachmentInlineCollection)) { foreach ($attachmentInlineCollection as $a) { $fileName = 'data/upload/' . $a->id; $attachment = new MimePart(file_get_contents($fileName)); $attachment->disposition = Mime::DISPOSITION_INLINE; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->id = $a->id; if ($a->get('type')) { $attachment->type = $a->get('type'); } $attachmentPartList[] = $attachment; } } $message->setSubject($email->get('name')); $body = new MimeMessage(); $textPart = new MimePart($email->getBodyPlainForSending()); $textPart->type = 'text/plain'; $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $textPart->charset = 'utf-8'; if ($email->get('isHtml')) { $htmlPart = new MimePart($email->getBodyForSending()); $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $htmlPart->type = 'text/html'; $htmlPart->charset = 'utf-8'; } if (!empty($attachmentPartList)) { $messageType = 'multipart/related'; if ($email->get('isHtml')) { $content = new MimeMessage(); $content->addPart($textPart); $content->addPart($htmlPart); $messageType = 'multipart/mixed'; $contentPart = new MimePart($content->generateMessage()); $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; $body->addPart($contentPart); } else { $body->addPart($textPart); } foreach ($attachmentPartList as $attachmentPart) { $body->addPart($attachmentPart); } } else { if ($email->get('isHtml')) { $body->setParts(array($textPart, $htmlPart)); $messageType = 'multipart/alternative'; } else { $body = $email->getBodyPlainForSending(); $messageType = 'text/plain'; } } $message->setBody($body); if ($messageType == 'text/plain') { if ($message->getHeaders()->has('content-type')) { $message->getHeaders()->removeHeader('content-type'); } $message->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8'); } else { if (!$message->getHeaders()->has('content-type')) { $contentTypeHeader = new \Zend\Mail\Header\ContentType(); $message->getHeaders()->addHeader($contentTypeHeader); } $message->getHeaders()->get('content-type')->setType($messageType); } $message->setEncoding('UTF-8'); try { $rand = mt_rand(1000, 9999); if ($email->get('parentType') && $email->get('parentId')) { $messageId = '' . $email->get('parentType') . '/' . $email->get('parentId') . '/' . time() . '/' . $rand . '@espo'; } else { $messageId = '' . md5($email->get('name')) . '/' . time() . '/' . $rand . '@espo'; } if ($email->get('isSystem')) { $messageId .= '-system'; } $messageIdHeader = new \Zend\Mail\Header\MessageId(); $messageIdHeader->setId($messageId); $message->getHeaders()->addHeader($messageIdHeader); $this->transport->send($message); $email->set('messageId', '<' . $messageId . '>'); $email->set('status', 'Sent'); $email->set('dateSent', date("Y-m-d H:i:s")); } catch (\Exception $e) { throw new Error($e->getMessage(), 500); } $this->useGlobal(); }
public function mailtestAction() { $emailService = new Service\EmailFactory(); $emailService->setFrom('*****@*****.**'); $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $user = $em->getRepository("ErsBase\\Entity\\User")->findOneBy(array('email' => '*****@*****.**')); $user = new Entity\User(); $user->setEmail('*****@*****.**'); $emailService->addTo($user); $emailService->setSubject('Testmail'); $emailService->setHtmlMessage('<h1>Testmail</h1>'); #$emailService->setTextMessage('Testmail'); $emailService->addAttachment(getcwd() . '/public/img/EJC2015_Terms_and_Services.pdf'); $emailService->addAttachment(getcwd() . '/public/img/logo.jpg'); $emailService->addAttachment(getcwd() . '/public/img/ejc_logo.png'); $emailService->send(); return true; $content = new Mime\Message(); $textContent = 'This is the text of the email.'; $textPart = new Mime\Part($textContent); $textPart->type = 'text/plain'; $htmlMarkup = '<html><body><h1>This is the text of the email.</h1></body></html>'; $htmlPart = new Mime\Part($htmlMarkup); $htmlPart->type = 'text/html'; $content->setParts(array($textPart, $htmlPart)); $contentPart = new Mime\Part($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; $pathToImage = getcwd() . '/public/img/logo.jpg'; $attachment = new Mime\Part(fopen($pathToImage, 'r')); #$attachment->type = 'application/pdf'; $attachment->type = 'image/jpeg'; $attachment->filename = 'logo.jpg'; $attachment->encoding = Mime\Mime::ENCODING_BASE64; $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; $body = new Mime\Message(); $body->setParts(array($contentPart, $attachment)); $message = new Message(); $message->setEncoding('utf-8'); $message->addTo('*****@*****.**'); $message->addFrom('*****@*****.**'); $message->setSubject('Testmail'); $message->setBody($body); $transport = new Transport\Sendmail(); $transport->send($message); }
public function send() { # differ the following scenarios: # - plain text email # - plain text email with 1 or n attachments # - html email with text alternative # - html email with text alternative and 1 or n attachments $content = new Mime\Message(); if (!$this->isHtmlMessage()) { $content->addPart($this->getTextMessage()); } else { $content->addPart($this->getTextMessage()); $content->addPart($this->getHtmlMessage()); } if (count($this->getAttachments()) == 0) { $body = $content; } else { $contentPart = new Mime\Part($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; $body = new Mime\Message(); $body->addPart($contentPart); foreach ($this->getAttachments() as $att) { $body->addPart($att); } } $message = new Mail\Message(); $message->setEncoding('utf-8'); $message->setBody($body); if ($this->isHtmlMessage()) { if (count($this->getAttachments()) == 0) { $message->getHeaders()->get('content-type')->addParameter('charset', 'utf-8')->setType('multipart/alternative'); } else { $message->getHeaders()->get('content-type')->addParameter('charset', 'utf-8')->setType('multipart/mixed'); // Important to get all attachments into this email. } } else { $message->getHeaders()->get('content-type')->addParameter('charset', 'utf-8')->setType('text/plain'); } foreach ($this->getTo() as $user) { $message->addTo($user->getEmail()); } foreach ($this->getCc() as $user) { $message->addCc($user->getEmail()); } foreach ($this->getBcc() as $user) { $message->addBcc($user->getEmail()); } $message->addFrom($this->getFrom()); $message->setSubject($this->getSubject()); $transport = new Mail\Transport\Sendmail(); $transport->send($message); }