public function testStreamEncoding() { $testfile = realpath(__FILE__); $original = file_get_contents($testfile); // Test Base64 $fp = fopen($testfile, 'rb'); $this->assertTrue(is_resource($fp)); $part = new Mime\Part($fp); $part->encoding = Mime\Mime::ENCODING_BASE64; $fp2 = $part->getEncodedStream(); $this->assertTrue(is_resource($fp2)); $encoded = stream_get_contents($fp2); fclose($fp); $this->assertEquals(base64_decode($encoded), $original); // test QuotedPrintable $fp = fopen($testfile, 'rb'); $this->assertTrue(is_resource($fp)); $part = new Mime\Part($fp); $part->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $fp2 = $part->getEncodedStream(); $this->assertTrue(is_resource($fp2)); $encoded = stream_get_contents($fp2); fclose($fp); $this->assertEquals(quoted_printable_decode($encoded), $original); }
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; }
/** * @param null|'text/html'|'text' $type * * @return Email */ public function getMessage($type = null) { do { if (false !== strpos($this->current()->contenttype, 'text/html')) { $currentType = 'text/html'; } else { $currentType = 'text'; } } while ($type && $type != $currentType && $this->countParts() > $this->key() && !$this->next()); $message = new Email(); $message->setSubject($this->subject); $message->setTo($this->to); $message->setFrom($this->from); $part = new Part($this->current()); $body = (string) $part->getContent(); if ('text/html' == $currentType) { $body = quoted_printable_decode($body); } $message->setBody($body); return $message; }
public function pdfAction() { $query = $this->getServiceLocator()->get('wrapperQuery'); $pressModel = new \CMS\Model\Press($query); $newsId = $this->params('newsId'); $newsDetails = $pressModel->getMoreDetails($newsId)[0]; $dompdf = new \DOMPDF(); $this->renderer = $this->getServiceLocator()->get('ViewRenderer'); $content = $this->renderer->render('cms/press/pdf', array('tmp_name' => $newsDetails->picture['tmp_name'], 'created' => $newsDetails->created, 'title' => $newsDetails->title, 'body' => $newsDetails->body, 'summary' => $newsDetails->summary, 'author' => $newsDetails->author)); // make a header as html $html = new MimePart($content); $html->type = "text/html"; $body = new MimeMessage("Hello"); $body->setParts(array($html)); // add html to convert it to pdf $dompdf->load_html($html->getContent()); $dompdf->set_paper("letter", "portrait"); $dompdf->render(); //download popup $dompdf->stream($newsDetails->title . '.pdf'); }
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); }
/** * Upload an attachment to Elastic Email so it can be reused when an email is sent * * @link http://elasticemail.com/api-documentation/attachments-upload * @param Part $attachment * @return int The attachment id */ public function uploadAttachment(Part $attachment) { $request = $this->prepareHttpClient('/attachments/upload', array('file' => $attachment->filename))->setMethod(HttpRequest::METHOD_PUT)->setRawBody($attachment->getRawContent())->getRequest(); // Elastic Email handles the content type of the message itself. Based on the extension of // the file, Elastic Email determines the content type. The attachment must be uploaded to // the server with always the application/x-www-form-urlencoded content type. // // More information: http://support.elasticemail.com/discussions/questions/1486-how-to-set-content-type-of-an-attachment $request->getHeaders()->addHeaderLine('Content-Type', 'application/x-www-form-urlencoded')->addHeaderLine('Content-Length', strlen($attachment->getRawContent())); $response = $this->client->send($request); return $this->parseResponse($response); }
/** * @link https://github.com/zendframework/zf2/issues/5428 * @group 5428 */ public function testContentEncodingWithStreamReadTwiceINaRow() { $testfile = realpath(__FILE__); $original = file_get_contents($testfile); $fp = fopen($testfile, 'rb'); $part = new Mime\Part($fp); $part->encoding = Mime\Mime::ENCODING_BASE64; $contentEncodedFirstTime = $part->getContent(); $contentEncodedSecondTime = $part->getContent(); $this->assertEquals($contentEncodedFirstTime, $contentEncodedSecondTime); fclose($fp); $fp = fopen($testfile, 'rb'); $part = new Mime\Part($fp); $part->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $contentEncodedFirstTime = $part->getContent(); $contentEncodedSecondTime = $part->getContent(); $this->assertEquals($contentEncodedFirstTime, $contentEncodedSecondTime); fclose($fp); }