This is a ZF2 Module that gives you a simple way to configure one or multiple mail services.
It supports all transports shipped with ZF2, e.g. any transport that implements the Zend\Mail\Transport\TransportInterface
.
It also has a transport for Mandrill. If you wish to use it install also mandrill/mandrill
(versions 1.0.*) library.
Add ripaclub/zf2-mailman
to your composer.json
.
{
"require": {
"ripaclub/zf2-mailman": "~0.3.2"
}
}
Configure a transport in your configuration file.
'mailman' => [
'MailMan\Gmail' => [
'default_sender' => 'my-name-is-methos@gmail.com',
'transport' => [
'type' => 'smtp',
'options' => [
'host' => 'smtp.gmail.com',
'port' => '587',
'connection_class' => 'login',
'connection_config' => [
'ssl' => 'tls',
'username' => 'my-name-is-methos@gmail.com',
'password' => 'MYSECRETPASSWORD',
]
]
],
],
],
Do not forget to add MailMan module to you application.config.php
file.
'modules' => [
// ...
'MailMan',
'Application',
],
Text only message
Then we send a text only message.
$message = new \MailMan\Message();
$message->addTextPart('Test email');
$message->setSubject('My name is methos');
$message->addFrom('my-name-is-methos@gmail.com', 'Methos');
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var \MailMan\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);
Message with attachment
Do you want to send an email message with an attachment from filesystem?
$message = new \MailMan\Message();
$message->addAttachment('/path/to/an/attachment.png');
$message->setBody('Test email');
$message->setSubject('My name is methos');
$message->addFrom('my-name-is-methos@gmail.com', 'Methos');
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var \MailMan\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);
Message using a template
$content = new ViewModel();
$content->setTemplate('email/example.phtml');
$content->setVariable('name', 'RipaClub');
$message = new \MailMan\Message();
$message->setSubject('Example email');
$message->addHtmlPart($this->getServiceLocator()->get('ViewRenderer')->render($content));
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var $mailService \MailMan\Service\MailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);
The content of email/example.phtml
file will be:
<h2>Hi <?= $name; ?>,</h2>
This is an example email with template.
To use the Mandrill transport add "mandrill/mandrill"
to your composer.json
.
Then configure it.
'mailman' => [
'MailMan\Mandrill' => [
'default_sender' => 'test@mail.com',
'transport' => [
'type' => 'mandrill',
'options' => [
'api_key' => 'MYSECRETMANDRILLKEY',
'sub_account' => 'my-optional-subaccount-if-any'
],
],
],
]
In this example we use the SMTP transport (shipped by ZF2).
'mailman' => [
'MailMan\SMTP' => [
'default_sender' => 'my-name-is-methos@gmail.com',
'transport' => [
'type' => 'smtp',
'options' => [
'host' => 'smtp.gmail.com',
'port' => '587',
'connection_class' => 'login',
'connection_config' => [
'ssl' => 'tls',
'username' => 'my-name-is-methos@gmail.com',
'password' => 'MYSECRETPASSWORD',
]
]
],
],
],
In this example we use the Sendmail transport (shipped by ZF2).
'mailman' => [
'MailMan\Sendmail' => [
'default_sender' => 'my-name-is-methos@yourdomain.com',
'transport' => [
'type' => 'sendmail'
],
],
],