Replace Swift Mailer with Symfony Mailer for HTML email

Developer Desk

When you're theming a Drupal Commerce site, you shouldn't forget to style the emails your store sends. The primary use case is the checkout completion email that includes the customer's receipt, but there may be others depending on your feature set and customizations (e.g. dunning emails for recurring subscriptions).

Commerce Core provides a basic, table based template that you'll want to either replace or at least ensure is styled by your theme to reflect your branding. (Find it in the order module's templates directory, commerce-order-receipt.html.twig.) However, without adding the capacity to properly send HTML email from your site, it's going to look like a jumbled mess in your customer's inbox.

We've long recommended Swift Mailer for formatting and sending HTML emails from Drupal Commerce. Symfony announced the project's deprecation late last year, recommending folks switch to using the 3 year old Symfony Mailer instead after they brought it up to full feature parity with the Swift Mailer library.

Fortunately, there's a module for that!

Symfony Mailer settings form

Drupal's Symfony Mailer module integrates the library and provides a "backwards compatibility" module that ensures the module can serve as a drop-in replacement for the old Mail System module the Swift Mailer module depended on. Making the switch in my local testing was as simple as:

  1. Uninstall the Swift Mailer module.
  2. Uninstall the Mail System module.
  3. Add the Symfony Mailer module to my project (using Composer).
  4. Install the Symfony Mailer and Symfony Mailer Back-compatibility modules.
  5. Define a transport type. (I develop locally with ddev, so I added an SMTP transport using 127.0.0.1 as the host, port 1025, and no username / password.)
  6. Make that your default transport.

And that's it! After taking the above steps, my email receipts showed up as properly formatted HTML emails. If you're also developing locally using ddev, remember you can review all email sent by your site in MailHog on port 8026 (i.e. by navigating to https://example.ddev.site:8026).

The Symfony Mailer module offers a variety of "policy" options you may need to review depending on how you've customized email on your site. You can use these policy options to alter properties of different emails, such as setting a custom reply-to address for your receipt emails.

The module is still in active development with only an alpha release, so make sure to test it fully in a development environment before deploying to production. Leave a comment with any tips you glean from your own implementation!

Comments

Submitted by jb (not verified) on Tue, 04/05/2022 - 08:03

thanks for explaining Symfony Mailer. This project is not covered by the security advisory policy. (on the module page)
you don`t have any security concerns using it?

Submitted by Ryan Szrama on Mon, 04/18/2022 - 19:13

In reply to by jb (not verified)

Not particularly. The fact it isn't covered is primarily due to it not having a full release yet. The security team works on behalf of projects that have full tagged releases. However, in this case, the meat of the project - the actual mailer library itself - is a Symfony maintained project that has been stable for a long time. That's not to say you shouldn't proceed with caution, but we don't see any particular risk in using the module at present.

(Bear in mind that even projects "covered" by the policy can still have security issues; it's just that there's a process by which the security team oversees disclosure and communication about vulnerabilities. Great question, though!)

Add new comment