Created
October 28, 2019 10:53
-
-
Save tsh-code/d886ad02ebadceb76d621029014000a4 to your computer and use it in GitHub Desktop.
Zend Mail complex example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Zend\Mail; | |
use Zend\Mime\Message as MimeMessage; | |
use Zend\Mime\Mime; | |
use Zend\Mime\Part as MimePart; | |
$transport = new Mail\Transport\Smtp(); | |
$options = new Mail\Transport\SmtpOptions([ | |
'host' => '192.168.99.100', | |
'port' => 5025, | |
]); | |
$transport->setOptions($options); | |
$text = new MimePart('This is text content of e-mail'); | |
$text->type = Mime::TYPE_TEXT; | |
$text->charset = 'utf-8'; | |
$text->encoding = Mime::ENCODING_QUOTEDPRINTABLE; | |
$html = new MimePart(file_get_contents(__DIR__ . '/../template/mail.html')); | |
$html->type = Mime::TYPE_HTML; | |
$html->charset = 'utf-8'; | |
$html->encoding = Mime::ENCODING_QUOTEDPRINTABLE; | |
$content = new MimeMessage(); | |
$content->setParts([$text, $html]); | |
$contentPart = new MimePart($content->generateMessage()); | |
$contentPart->type = Mime::MULTIPART_ALTERNATIVE; | |
$contentPart->setBoundary($content->getMime()->boundary()); | |
$image = new MimePart(fopen(__DIR__ . '/../screen.png', 'r')); | |
$image->type = 'image/png'; | |
$image->filename = 'image-file-name.png'; | |
$image->disposition = Mime::DISPOSITION_INLINE; | |
$image->encoding = Mime::ENCODING_BASE64; | |
$body = new MimeMessage(); | |
$body->setParts([$contentPart, $image]); | |
$message = new Mail\Message(); | |
$message->setBody($body); | |
$message->setFrom('Freeaqingme@example.org', 'Dolf'); | |
$message->addTo('matthew@example.com', 'Matthew'); | |
$message->setSubject('multipart e-mail'); | |
$message->getHeaders()->get('content-type')->setType(Mime::MULTIPART_RELATED); | |
$transport->send($message); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment