ripaclub / zf2-mailman
ZF2 module to manage email delivery
Requires
- php: >=5.5
- zendframework/zend-mail: ~2.4
- zendframework/zend-mime: ~2.4
- zendframework/zend-servicemanager: ~2.4
- zendframework/zend-stdlib: ~2.4
Requires (Dev)
- mandrill/mandrill: ~1.0.0
- phpunit/phpunit: ~4.2
- satooshi/php-coveralls: dev-master
- zendframework/zend-mvc: ~2.4
Suggests
- mandrill/mandrill: Allows usage of the Mandrill transport
This package is not auto-updated.
Last update: 2024-11-09 16:16:55 UTC
README
What is this?
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.
Installation
Add ripaclub/zf2-mailman
to your composer.json
.
{
"require": {
"ripaclub/zf2-mailman": "~0.3.2"
}
}
Usage
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.
Transports configuration examples
Mandrill
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' ], ], ], ]
SMTP
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', ] ] ], ], ],
Sendmail
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' ], ], ],