Soflomo\Mail is yet another mail facade for Zend\Mail

There are numerous e-mail modules available on modules.zendframework.com, but all fail the goal to be reusable for a medium-wide range of use cases. Therefore is today Soflomo\Mail v0.2.0 released: a facade module that helps users to execute three steps in a single method call:

  1. Compose an e-mail message object (with address fields, subject and so on)
  2. Render view templates for the body text (both in html and/or text)
  3. Send the e-mail with a given transport

At Soflomo we rely heavily on e-mails. In many cases e-mails are sent and with Zend Framework 2, this process usually leads to the following boilerplate code:

<?php
namespace MyApp\Service;

use Zend\View\Renderer\RendererInterface;
use Zend\Mail\Message;
use Zend\Mail\Transport\TransportInterface;
use Zend\Mime\Part    as MimePart;
use Zend\Mime\Message as MimeMessage;

use MyApp\Entity\User;

class PasswordService
{
    protected $renderer;
    protected $transport;

    public function __construct(
        RendererInterface $renderer,
        TransportInterface $transport
    ) {
        $this->renderer  = $renderer;
        $this->transport = $transport;
    }

    public function sendPasswordReminder(User $user)
    {
        $message = new Message;
        $message->setSubject('Password reminder');
        $message->setTo($user->getEmailAddress());

        $html = $this->renderer('email/password', array(
            'user' => $user,
        ));

        $part = new MimePart($html);
        $part->type = 'text/html';

        $body = new MimeMessage;
        $body->setParts(array($part));
        $message->setBody($body);

        $this->transport->send($message);
    }
}

Rinse and repeat for every other message you send. This is the reason why all the different mail facade modules exist. However, they often use hardcoded the SMTP protocol, have problems in using custom Message instances and are simply not reusable. Now, with Soflomo\Mail the service is much easier to write, read and maintain:

<?php
namespace MyApp\Service;

use Soflomo\Mail\Service\MailService;
use MyApp\Entity\User;

class PasswordService
{
    protected $service;

    public function __construct(MailService $service)
    {
        $this->service  = $service;
    }

    public function sendPasswordReminder(User $user)
    {
        // Message variables
        $options = array(
            'to'       => $user->getEmailAddres(),
            'template' => 'email/password',
            'subject'  => 'Password reminder'
        );

        // Template variables
        $variables = array(
            'user'     => $user
        )
        $this->service->send($options, $variables);
    }
}

The core concept of Soflomo\Mail is:

  1. It should work out of the box with Zend\Mail\Transport objects
  2. Configuration over code (no php wiring required for basic usage)
  3. Ability to use your own Transport service, for example with SlmMail if you want to use 3rd part email providers

All these topics are integrated in v0.2.0. Tests are not written yet, but should shortly arrive for the coming minor. Simply require soflomo/mail and start using it!