Using SwiftMailer in Fork CMS

Since our 3.9 release, all mail communication runs through the Swiftmailerbundle. This is the officially supported way of sending mails through Symfony. It's  based on the widely used and well documented library Swiftmailer.

Using SwiftMailer is beneficial to everyone. Due to the size of its user base, Swiftmailer has a low bug count and is a polished product. It also contains a test suite that covers almost all code. This means we have more functionality with less bugs, that's awesome! For the Fork CMS development team, it also means less code to maintain.

How does it work?

The swiftmailerbundle registers a mailer service in the dependency injection container. This way, we can just use $this->get('mailer') in controllers or \Frontend\Core\Engine\Model::get('mailer') in other classes to get our mailer object. This probably looks familiar, since we already had our custom mailer service registered in the container.

The big change is in building the mails. Previously, we had an addEmail method that took waaaaay too much parameters and did everything from parsing the mail, providing fallbacks for e-mail addresses, etc. With the SwiftmailerBundle, it has changed to a send method that accepts a \Swift_Message object. We have created a custom Fork Message object that can parse our templates too, to make the usage in Fork CMS a little more convenient.

// create a message object and set all the needed properties
$message = \Common\Mailer\Message::newInstance($subject)
    ->setFrom(array($fromEmail => $fromName))
    ->setTo(array($toEmail => $toName))
    ->setReplyTo(array($replyToEmail => $replyToName))
    ->parseHtml($template, $variables, $addUTM)
    ->setPlainText($plainText)
    ->addAttachments($attachments)
;

// send it trough the mailer service
$this->get('mailer')->send($message);

Easy, isn't it? All the standard Fork CMS modules are also updated to this code, so this could also help you to write your own e-mail sending code.

Is our backend mail configuration still used?

If you're asking this question, you've probably used the SwiftmailerBundle in Symfony and you know that it is configured with a static configuration in your parameters.yml file. We also have this configuration in our app/config/config.yml file. It looks like this:

swiftmailer:
    transport: "mail"

This means we instantiate the swiftmailer service and will use the php mail transport to send e-mails (which is also the default in Fork). When we selected smtp in the backend, we'd like to use this though. This is done using another service: the mailer_configurator. This service is actually an event handler and it's called right after Fork receives a request. This event handler fetches the mail settings from the database and replaces the mail transport with a configured version, thus containing our configuration from the backend.

It could be interesting for all Fork CMS developers to note that all the changes needed to upgrade to the newest version are always integrated in the UPGRADE_GUIDE files in our repository.

If you want to use a gmail account to send your emails trough smtp, it might be needed to unlock some settings to be able to this. You can visit these links and try again after enabling them: https://accounts.google.com/b/0/DisplayUnlockCaptchahttps://www.google.com/settings/security/lesssecureapps. Good luck!