public function send($subject, $text, $to, $from = null) { if (!config('mail.enabled')) { return; } $message = new Message(); $message->setSubject($subject); if ($from) { if (is_array($from)) { $message->setFrom($from[0], $from[1]); } else { $message->setFrom($from); } } $message->addTo($to); $content = '<html><head><title>' . $subject . '</title></head><body>' . $text . '</body></html>'; $html = new Mime\Part($content); $html->setType(Mime\Mime::TYPE_HTML); $html->setCharset('utf-8'); $mimeMessage = new Mime\Message(); $mimeMessage->addPart($html); foreach ($this->attachments as $attachment) { $mimeMessage->addPart($attachment); } $message->setBody($mimeMessage); try { $transport = new SmtpTransport(new SmtpOptions(config('mail.options'))); $transport->send($message); } catch (\Exception $e) { throw $e; } return $this; }
/** * @param $templateName * @param array $values * @return Message * @throws \Exception */ public function prepareMessage($templateName, $values = array()) { $viewRenderer = $this->getViewRenderer(); $content = $viewRenderer->render('mail/' . $templateName, $values); $html = new Mime\Part($content); $html->setType('text/html'); $body = new Mime\Message(); $body->addPart($html); $message = new Message(); $message->setBody($body); return $message; }
/** * @param string $html * @param Message $scope * @return Message */ public function createHtmlMessage($html, Message $scope = null) { $part = new Part($html); $part->setType('text/html'); if (!$scope) { $scope = new Message(); } $mime = new MimeMessage(); $mime->setParts([$part]); $scope->setBody($mime); return $scope; }
public function send($alias, $to, $from = null, array $data = null) { if (!config('mail.enabled')) { return; } $template = $this->getTemplate($alias); if (!$template) { throw new CoreException('Cannot find template with alias/id: ' . $alias, 404); } if ($data === null) { $data = array(); } $template->setDescription($this->replaceTemplateContent($template->getDescription(), $data)); $template->setName($this->replaceTemplateContent($template->getName(), $data)); $preffix = server_url(base_url()); $content = preg_replace('#background\\: url\\((.*)\\)#', 'background: url(' . $preffix . '$1)', $template->getDescription()); $content = preg_replace('#\\<img src=\\"(.*)\\"#', '<img src="' . $preffix . '$1"', $content); $template->getDescription($content); $message = new Message(); $message->setSubject($template->getName()); if ($from) { $message->setFrom($from); } $message->addTo($to); $content = $template->getDescription(); $content = '<html><head><title>' . $template->getName() . '</title></head><body>' . $template->getDescription() . '</body></html>'; $html = new Mime\Part($content); $html->setType(Mime\Mime::TYPE_HTML); $html->setCharset('utf-8'); $mimeMessage = new Mime\Message(); $mimeMessage->addPart($html); foreach ($this->attachments as $attachment) { $mimeMessage->addPart($attachment); } $message->setBody($mimeMessage); try { $transport = new SmtpTransport(new SmtpOptions(config('mail.options'))); $transport->send($message); } catch (\Exception $e) { throw $e; } return $this; }
public function backupAction() { $log = $this->getServiceLocator()->get('log'); $log->addInfo('备份数据' . "\t" . $this->getRequest()->getServer('REMOTE_ADDR') . "\t" . $this->getRequest()->getHeaders()->get('User-Agent')->getFieldValue()); $dbconf = $this->getServiceLocator()->get('config')['mysqli']; $dump = new \MySQLDump(new \mysqli($dbconf['host'], $dbconf['username'], $dbconf['password'], $dbconf['dbname'])); $filename = date("Y-m-d_H-i-s") . "-db.sql"; $tmpFile = dirname(__FILE__) . "\\" . $filename; $dump->save($tmpFile); $body = new Message(); $part = new Part(); $part->setType(Mime::TYPE_OCTETSTREAM); $part->setContent(file_get_contents($tmpFile)); $part->setDisposition(Mime::DISPOSITION_ATTACHMENT); $part->setFileName($filename); $part2 = new Part(); $part2->setType(Mime::TYPE_TEXT); $part2->setContent('小秋来发数据了'); $body->addPart($part); $body->addPart($part2); $newmessage = new \Zend\Mail\Message(); $newmessage->addTo($this->getServiceLocator()->get('MailOptions')->getMailTo()); $newmessage->addFrom($this->getServiceLocator()->get('MailOptions')->getMailFrom()); $newmessage->setBody($body); $newmessage->setSubject('备份数据'); $transport = new SmtpTransport(); $options = new SmtpOptions($this->getServiceLocator()->get('config')['mail']); $transport->setOptions($options); try { $transport->send($newmessage); echo 1; } catch (\Exception $e) { echo -1; } exit; }
/** * @param $stringOrView * @return mixed|string */ public function parseTemplate($stringOrView) { if ($stringOrView instanceof ViewModel) { $stringOrView = $this->getView()->render($stringOrView); } // find inline images. $xml = new \DOMDocument(); $xml->loadHTML($stringOrView); $images = $xml->getElementsByTagName('img'); $attachments = []; /* @var DomElement $image */ foreach ($images as $image) { $file = $image->getAttribute('src'); $binary = file_get_contents($file); $mime = $this->mimeByExtension($file); $fileName = pathinfo($file, PATHINFO_BASENAME); $attachment = new MimePart($binary); $attachment->setType($mime); $attachment->setDisposition(Mime::DISPOSITION_ATTACHMENT); $attachment->setEncoding(Mime::ENCODING_BASE64); $attachment->setFileName($fileName); $attachment->setId('cid_' . md5($fileName)); $stringOrView = str_replace($file, 'cid:' . $attachment->getId(), $stringOrView); $attachments[] = $attachment; } $this->attachments = $attachments; return $stringOrView; }
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); }