Skip to content

Mail Module for ZF2 with Mandrill Transport

License

Notifications You must be signed in to change notification settings

visa4/zf2-mail-module

Repository files navigation

ZF2MailModule

Build Status Latest Stable Version Latest Unstable Version License

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 so for instance any transport that implements the Zend\Mail\Transport\TransportInterface.

It also has a transport for Mandrill

Installation

Add fntlnz/zf2-mail-module to your composer.json

{
   "require": {
       "fntlnz/zf2-mail-module": "v0.1.0"
   }
}

Usage

Configure a transport in your configuration file

'mail_module' => [
    'MailModule\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',
                 ]
             ]
        ],
    ],
],

Text only message

/** @var \MailModule\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailModule\Gmail');
$mailService->setBody('Test email');
$mailService->getMessage()->setSubject('My name is methos');
$mailService->getMessage()->addFrom('my-name-is-methos@gmail.com', 'Methos');
$mailService->getMessage()->addTo('fontanalorenz@gmail.com', 'Lorenzo');
$mailService->send();

Message with attachment

/** @var \MailModule\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailModule\Gmail');
$mailService->addAttachment('/path/to/an/attachment.png');
$mailService->setBody('Test email');
$mailService->getMessage()->setSubject('My name is methos');
$mailService->getMessage()->addFrom('my-name-is-methos@gmail.com', 'Methos');
$mailService->getMessage()->addTo('fontanalorenz@gmail.com', 'Lorenzo');
$mailService->send();

Message using a template

/** @var $mailService \MailModule\Service\MailService */
$mailService = $this->getServiceLocator()->get('MailModule\Gmail');
$content = new ViewModel();
$content->setTemplate('email/example.phtml');
$content->setVariable('name', 'Lorenzo');
$mailService->getMessage()->setSubject('Example email');
$mailService->setBody($this->getServiceLocator()->get('ViewRenderer')->render($content));
$mailService->getMessage()->addTo('fontanalorenz@gmail.com', 'Lorenzo');
$mailService->send();

email/example.phtml

<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

'mail_module' => [
    'MailModule\Mandrill' => [
        'default_sender' => 'test@mail.com',
        'transport' => [
            'type' => 'mandrill',
            'options' => [
                'apikey' => 'MYSECRETMANDRILLKEY',
                'sub_account' => 'my-optional-subaccount-if-any'
            ],
        ],
    ],
]

SMTP

'mail_module' => [
    'MailModule\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',
                 ]
             ]
        ],
    ],
],

About

Mail Module for ZF2 with Mandrill Transport

Resources

License

Stars

Watchers

Forks

Packages

No packages published