Module for composing e-mails from templates and sending them
PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
doc
src
test
.gitignore
.scrutinizer.yml
.travis.yml
CHANGES.md
LICENSE
Module.php
README.md
composer.json
phpcs.xml
phpunit.xml.dist

README.md

E-mail module for Zend Framework 2

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads

Introduction

MtMail handles common activities surrounding sending e-mail from applications, mainly creating messages from templates, and sending them through transport adapters.

Features:

  • factory for creating e-mail messages
  • factory for e-mail transport adapters, service for one-line dispatch
  • rendering templates from phtml files, using Zend\View and PhpRenderer
  • rendering templates with layouts
  • plugins for various common tasks: from setting default headers to generating plaintext version of HTML message
  • plugin support via dedicated plugin managers

Installation

Installation is supported via Composer:

  1. Add "mtymek/mt-mail":"1.1.*" to your composer.json file and run php composer.phar update.
  2. Add MtMail to your config/application.config.php file under the modules key.

Creating e-mails

Configuration

By default MtMail doesn't require any extra configuration. By default it will use Zend\View to render templates accessible by your application.

Usage

Creating message from controller:

$mailService = $this->getServiceLocator()->get(\MtMail\Service\Mail::class);

$headers = [
    'to' => 'johndoe@domain.com',
    'from' => 'contact@mywebsite.com',
];
$variables = [
    'userName' => 'John Doe',
];
$message = $mailService->compose($headers, 'application/mail/welcome.phtml', $variables);

This snippet will create a message, compose it with $headers and HTML body rendered from welcome.phtml template (injected with $variables array).

Layouts

In order to give your e-mails common layout, you have to enable "Layout" plugin and tell it where to look for layout template:

return [
    'mt_mail' => [
        'composer_plugins' => [
            'Layout',
        ],
        'layout' => 'application/mail/layout.phtml',
    ],
];

For more info about composing e-mail messages, check the documentation. You can also check documentation for plugins.

Sending e-mails

Configuration

Update your application config:

return [
    'mt_mail' => [
        'transport' => \Zend\Mail\Transport\Smtp::class,
        'transport_options' => [
            'host' => 'some-host.com',
            'connection_class' => 'login',
            'connection_config' => [
                'username' => 'user',
                'password' => 'pass',
                'ssl' => 'tls',
            ],
        ],
    ],
];

Usage


Add following code to your controller:

// create and configure message
$message = new Message();
$message->addTo('johndoe@domain.com');
// ...

// send!
$mailService = $this->getServiceLocator()->get(\MtMail\Service\Mail::class);
$mailService->send($message);

For more info on sending e-mails, check the documentation.