Sending automated emails with PHP, Swiftmailer and Twig

I’m the one of hosts of a Coding Dojo in my city called Katayunos. Katayunos is the mix of the word Kata (coding kata) and “Desayuno” (breakfast in Spanish). A group of brave programmers meet together one Saturday morning and after having breakfast we pick one coding kata and we practise TDD and pair programming. It’s something difficult to explain to non-geek people (why the hell we wake up early one Saturday morning to do this) but if you are reading this post probably it sounds good:).

My work as host is basically pick the place and encourage people to join to the Coding Dojo. One way of doing this (besides twitter buzz) is take my address book and send one bulk email to all of them inviting to join us. I don’t like this kind of mails. They look like spam, so I prefer to send a personalized email. This email has a common part (the place location, the hour, the event description, …) and the personalized part. I can do it manually, the list isn’t so huge, but definitely that’s not cool. Because of that I have done a little script to perform this operation. I can do a simple PHP script but we are speaking about announcing a event about TDD, SOLID and things like that, so I must use the “right way”. Let’s start.

I manage my list of contacts within a spreadsheet. In this spreadsheet I have the name, the email and a one paragraph with the personalized part to each one of my contact. I can easily export this spreadsheet to a csv document like this:

[sourcecode]
Peter Parker, spiderman@gmail.com, "Lorem ipsum dolor sit amet, …"
Clark Kent, superman@gmail.com, "consectetur adipisicing elit, …"
Juan López Fernández, superlopez@gmail.com, "sed do eiusmod tempor incididunt .."
[/sourcecode]

So first of all I need to parse this file.

[sourcecode language=”php”]
class Parser
{
private $data;

public function createFromCsvFile($path)
{
$handle = fopen($path, "r");
while (($data = fgetcsv($handle)) !== false) {
$this->data[] = [
‘name’ => trim($data[0]),
’email’ => trim($data[1]),
‘body’ => isset($data[2]) ? trim($data[2]) : null,
];
}
}

public function getData()
{
return $this->data;
}
}
[/sourcecode]

Easy. Now I want to send this parsed array by email. Because of that I will include Swiftmailer in my composer.json file.

My email will also be one template and one personalized part. We will use Twig to manage the template.

[sourcecode language=”bash”]
"require": {
"swiftmailer/swiftmailer": "v5.0.2",
"twig/twig": "v1.13.2",
}
[/sourcecode]

Now we will create a class to wrap the needed code to send emails

[sourcecode language=”php”]
class Mailer
{
private $swiftMailer;
private $swiftMessage;

function __construct(Swift_Mailer $swiftMailer, Swift_Message $swiftMessage)
{
$this->swiftMailer = $swiftMailer;
$this->swiftMessage = $swiftMessage;
}

public function sendMessage($to, $body)
{
$this->swiftMessage->setTo($to);
$this->swiftMessage->setBody(strip_tags($body));
$this->swiftMessage->addPart($body, ‘text/html’);

return $this->swiftMailer->send($this->swiftMessage);
}
}
[/sourcecode]

Our Mailer class sends mails. Our Parser class parses one csv file. Now we need something to join those two classes: the Spammer class. Spammer class will take one parsed array and it will send one by one the mails using Mailer class.

[sourcecode language=”php”]
class Spammer
{
private $twig;
private $mailer;

function __construct(Twig_Environment $twig, Mailer $mailer)
{
$this->twig = $twig;
$this->mailer = $mailer;
}

public function sendEmails($data)
{
foreach ($data as $item) {
$to = $item[’email’];
$this->mailer->sendMessage($to, $this->twig->render(‘mail.twig’, $item));
}
}
}
[/sourcecode]

Ok with this three classes I can easily send my emails. This script is a console script and we also want pretty console colours and this kind of stuff. symfony/console to the rescue. But I’ve a problem now. I want to write one message when one mail is sent and another one when something wrong happens. If I want to do that I need to change my Spammer class. But my Spammer class does’t know anything about my console Command. If I inject the console command into my Spammer class I will violate the Demeter law, and that’s a sin. What can we do? Easy: The mediator pattern. We can write one implementation of mediator pattern but we also can use symfony/event-dispatcher, a well done implementation of this pattern. We change our Spammer class to:

[sourcecode language=”php”]
use Symfony\Component\EventDispatcher\EventDispatcher;

class Spammer
{
private $twig;
private $mailer;
private $dispatcher;

function __construct(Twig_Environment $twig, Mailer $mailer, EventDispatcher $dispatcher)
{
$this->twig = $twig;
$this->mailer = $mailer;
$this->dispatcher = $dispatcher;
}

public function sendEmails($data)
{
foreach ($data as $item) {
$to = $item[’email’];
try {
$this->mailer->sendMessage($to, $this->twig->render(‘mail.twig’, $item));
$this->dispatcher->dispatch(MailEvent::EVENT_MAIL_SENT, new MailEvent\Sent($to));
} catch (\Exception $e) {
$this->dispatcher->dispatch(MailEvent::EVENT_SENT_ERROR, new MailEvent\Error($to, $e));
}
}
}
}
[/sourcecode]

Now can easily build of console command class:

[sourcecode language=”php”]
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class SpamCommand extends Command
{
private $parser;
private $dispatcher;

protected function configure()
{
$this->setName(‘spam:run’)
->setDescription(‘Send Emails’);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Sending mails …");
$this->dispatcher->addListener(MailEvent::EVENT_MAIL_SENT, function (MailEvent\Sent $event) use ($output) {
$output->writeln("<info>Mail sent to</info>: <fg=black;bg=cyan>{$event->getTo()}</fg=black;bg=cyan>");
}
);

$this->dispatcher->addListener(MailEvent::EVENT_SENT_ERROR, function (MailEvent\Error $event) use ($output) {
$output->writeln("<error>Error sending mail to</error>: <fg=black;bg=cyan>{$event->getTo()}</fg=black;bg=cyan> Error: " . $event->getException()->getMessage());
}
);

$this->spammer->sendEmails($this->parser->getData());
$output->writeln("End");
}

public function setSpammer(Spammer $spammer)
{
$this->spammer = $spammer;
}

public function setParser(Parser $parser)
{
$this->parser = $parser;
}

public function setDispatcher(EventDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
}
[/sourcecode]

With all this parts we can build our script. Our classes are decoupled. That’s good but setting up the dependencies properly can be hard. Because of that we will use symfony/dependency-injection. With symfony DIC we can set up our dependency tree within a yaml file:

Our main services.yml
[sourcecode]
imports:
– resource: conf.yml
– resource: mail.yml
– resource: twig.yml

parameters:
base.path: .

services:
parser:
class: Parser
calls:
– [createFromCsvFile, [%mail.list%]]

mailer:
class: Mailer
arguments: [@swift.mailer, @swift.message]

spam.command:
class: SpamCommand
calls:
– [setParser, [@parser]]
– [setDispatcher, [@dispatcher]]
– [setSpammer, [@spammer]]

spammer:
class: Spammer
arguments: [@twig, @mailer, @dispatcher]

dispatcher:
class: Symfony\Component\EventDispatcher\EventDispatcher
[/sourcecode]

I like to separate the configuration files to reuse those files between projects and to make them more readable.

One for twig:

[sourcecode]
parameters:
twig.path: %base.path%/templates
twig.conf:
auto_reload: true

services:
twigLoader:
class: Twig_Loader_Filesystem
arguments: [%twig.path%]

twig:
class: Twig_Environment
arguments: [@twigLoader, %twig.conf%]
[/sourcecode]

another one for swiftmailer:

[sourcecode language=”xml”]
services:
swift.message:
class: Swift_Message
calls:
– [setSubject, [%mail.subject%]]
– [setFrom, [%mail.from.mail%: %mail.from.name%]]

swift.transport:
class: Swift_SmtpTransport
arguments: [%mail.smtp.host%, %mail.smtp.port%, %mail.smtp.encryption%]
calls:
– [setUsername, [%mail.smtp.username%]]
– [setPassword, [%mail.smtp.password%]]

swift.mailer:
class: Swift_Mailer
arguments: [@swift.transport]
[/sourcecode]

and the last one for the configuration parameters:

[sourcecode]
parameters:
mail.do.not.send.mails: false

mail.list: %base.path%/mailList.csv
mail.subject: mail subject
mail.from.name: My Name
mail.from.mail: my_email@mail.com

mail.smtp.username: my_smtp_username
mail.smtp.password: my_smtp_password
mail.smtp.host: smtp.gmail.com
mail.smtp.port: 465
mail.smtp.encryption: ssl
[/sourcecode]

Now we can build our script.

[sourcecode language=”php”]
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . ‘/conf’));
$loader->load(‘services.yml’);

$container->setParameter(‘base.path’, __DIR__);

$application = new Application();
$application->add($container->get(‘spam.command’));
$application->run();
[/sourcecode]n

And that’s all. My colleagues of the next Katayuno will be invited in a “SOLID” way :).
Source code is available in my github account.

BTW: Do you want to organize one Katayuno in your city? It’s very easy. Feel free to contact me for further information.

The reason why singleton is a “problem” with PHPUnit

Singleton is a common design pattern. It’s easy to understand and we can easily implement it with PHP:

[sourcecode language=”php”]
<?php
class Foo
{
static private $instance = NULL;

static public function getInstance()
{
if (self::$instance == NULL) {
self::$instance = new static();
}
return self::$instance;
}
}
[/sourcecode]

Maybe this pattern is not as useful as it is in J2EE world. With PHP everything dies within each request, so we cannot persist our instances between requests (without any persistent mechanism such as databases, memcached or external servers). But at least in PHP we can share the same instance, with this pattern, in our script. No matter were we are, we use Foo::getInstance() and we get our instance. It useful when we work, for example, with database connections and service containers.

If we work with TDD we most probably use PHPUnit. Imagine a simple script to test the instantiation of our Foo class
[sourcecode language=”php”]
class FooTest extends PHPUnit_Framework_TestCase
{
public function testGetInstance()
{
$this->assertInstanceOf(‘Foo’, Foo::getInstance());
}
}
[/sourcecode]

All is green. It works.

Imagine now we want to “improve” our Foo class and we want to create a counter:

[sourcecode language=”php”]
public function testCounter()
{
$foo = Foo::getInstance();
$this->assertEquals(0, $foo->getCounter());
$foo->incrementCounter();
$this->assertEquals(1, $foo->getCounter());
}
[/sourcecode]

Now our test fails, So we change our implementation of the class to pass the test and we add to Foo class

[sourcecode language=”php”]
private $counter = 0;

public function getCounter()
{
return $this->counter;
}

public function incrementCounter()
{
$this->counter++;
}
[/sourcecode]

It’s green again. We are using a singleton pattern to get the instance of the class and we are using TDD, so what’s the problem? I will show you with an example. Imagine we need to improve our Foo class and we want a getter to say if counter is even or not. We add a new test:

[sourcecode language=”php”]
public function testIsOdd()
{
$foo = Foo::getInstance();
$this->assertEquals(FALSE, $foo->counterIsOdd(), ‘0 is even’);
$foo->incrementCounter();
$this->assertEquals(TRUE, $foo->counterIsOdd(), ‘1 is uneven’);
}
[/sourcecode]

And we implement the code to pass the test again:

[sourcecode language=”php”]
public function counterIsOdd()
{
return $this->counter % 2 == 0 ? FALSE : TRUE;
}
[/sourcecode]

But it doesn’t work. Our test is red now. Why? The answer is simple. We are sharing the same instance of Foo class in both tests, so in the first one our counter start with 0 (the default value) but with the second one it start with 1, because the first test has changed the state of the instance (and we are reusing it).

This is a trivial example, but real world problems with this issue are a bit nightmare. Imagine, for example, that we use singleton pattern to get the database connection (a common usage of this pattern). Suddenly we break one test (because we have created a wrong SQL statement). Imagine we have a test suite with 100 assertions. We have broken one assertion (the second one, for example), and our PHPUnit script will say that we have 99 errors (the second one and all the following ones). That happens because our script can’t reuse the database connection (it’s broken in the second test). Too much garbage in the output of the PHPUnit script and that’s not agile.

How to solve it? We have two possible solutions. The first one is avoid singletons as a plague. We have alternatives such as dependency injection, but if we really want to use them (they are not forbidden, indeed) we need to remember to write one static function in our class to reset all our static states and call after each test. PHPUnit allow to execute something at the end of each test with tearDown() function. So we add to our test:

[sourcecode language=”php”]
class FooTest extends PHPUnit_Framework_TestCase
{

public function tearDown()
{
Foo::tearDown();
}
}
[/sourcecode]

And now we implement the tearDown function in our Foo Class
[sourcecode language=”php”]
public static function tearDown()
{
static::$instance = NULL;
}
[/sourcecode]

And all works again. All is green and green is cool, isn’t it? 🙂

Note: Take into account that “problem” is between quotes in the title. That’s beacause singleton is not a “real” problem. We can use it, it isn’t forbiden, but we need to realize the collateral effects that its usage can throw to our code.