Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 28, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/d886ad02ebadceb76d621029014000a4 to your computer and use it in GitHub Desktop.
Save tsh-code/d886ad02ebadceb76d621029014000a4 to your computer and use it in GitHub Desktop.
Zend Mail complex example
<?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