How to configure and use Swift Mailer in Drupal 8

June 18, 2021

Tags: Technologies, IT Staff Augmentation

swift mailer

 

One of the "disadvantages" of using Drupal 8 for a programmer experienced in D7 or accustomed to this technology is that many of the favorites contributed modules in D7 are in the process of migrating to D8, some are barely in their alpha or beta versions and others have not even begun to be migrated. This leaves us with only a few options of stable modules and the alternative of launching you in an adventure with the first versions of another group of modules as is the case of Swift Mailer.

 

Swift Mailer extends the basic email delivery functionality provided by Drupal, by delegating all email management to the Swift Mailer library. This allows your site to take advantage of numerous functions offered by the library, such as:

 

  • Send emails directly through an SMTP server of your choice, a locally installed MTA such as sendmail, or the mail functionality provided by PHP.
  • Send emails in HTML (MIME) format.
  • Add attachments to emails.
  • Add images to the contents of emails.

 

The module also allows you to customize the image of an email to reflect the general appearance of your site; Which is one of the objectives of this article.

 

swift mailer

 

To start using Swift Mailer first you must check that you have the mailsystem module installed and enabled, if you do not, you will have to install it and enable it:

 

Swift Mailer depends on the mailsystem module. This module is responsible for making the Swift Mailer module available for Drupal.

 

Once the module has been installed and enabled, then you must install and enable the swiftmailer module:

 

In addition, you will need to download and install the Swift Mailer library to let this module work. The preferred way to install this library is via Composer in the project root directory (eg docroot):

 

On this occasion, I also installed and configured MailCatcher; But this is not a strictly necessary step. MailCatcher runs a super simple SMTP server that captures any message sent to show it on an interface. Your page is quite complete and details the steps to follow for its installation and configuration.

 

The next step would be to configure the Mail System (/admin/config/system/mailsystem) through the admin UI:

 

To finally configure the Swift Mailer (/admin/config/swiftmailer/transport) also through the admin UI, in this case, with the configuration corresponding to the MailCatcher:

 

To create a custom theme and to be able to add an email template with your own design you must create the theme directory in the following path: /docroot/themes/custom/

 

Then you have to create a file in the theme directory with the following name format:

 

.info.yml And put in it, the required content. Finally, you must clear the cache (drush cr) and if everything was configured correctly, you must go to the admin panel to the Appearance option (/admin/appearance) and there should appear the theme created as "not installed". The last step would be to install it to be available as a theme on your system. In order to create the personalized e-mail template, a folder with names of templates and a file called swiftmailer.html.twig should be added inside the theme directory.

 

drupal 8

 

You should make a copy of the contributed module template with the same name located in docroot/modules/contrib/swiftmailer/templates and start your customization from there.

 

The css style should be located in the same file and if the design has pictures, it is good practice to place them in a directory within the topic that bears images names. The way to reference the image in your code would look something like this:

 

And of course, you already have an HTML template for your website using Swift Mailer in Drupal 8. Making use of alpha versions for D8 of a module that you may be familiar with can sometimes cause some dissatisfaction or insecurity since anything like incompatibility between your current version of D8 and the initial version of the module can make nothing work; But the advantage when you take the risk, is that everything can turn out well and make publications like this one, where "contributing" not only stays in the software but transcends to another level by helping other developers in the community.

 

We recommend you on video

 

Let's work together!