Mail in Drupal 8: the built-in system and useful modules

Parent Feed: 

The dream of many website owners is to have email sending opportunities on their websites. Of course, it’s possible with Drupal 8, because it has infinite powers. We’ve already discussed how your Drupal 8 website can “speak” multiple languages, cooperate with any third-party systems, connect to all imaginable social media, make your images perfect on any device, engage your users with super interactive menus, win their hearts with high loading speed, let them publish content on the fly, and what not! It’s now time for a closer look at the options for a perfect mail sending setup.

Drupal 8’s mail sending opportunities

The default mail system in Drupal 8

Drupal 8 has a built-in mail system — MailManager, which also serves as a plugin manager. In addition to basic core mail plugins, developers can also declare their own plugins that implement two main methods — the format() and the send(). There are important hooks to use when working with the MailManager. The main ones are the hook_mail() for sending emails and the hook_mail_alter() for editing the existing setup.

In addition to the default mail system, there also are contributed Drupal modules that add in some important things missing in core, or connect various third-party email sending services to your Drupal website.

Some great contributed Drupal 8 modules for mail sending

The Mail System module

Definitely, here is the most important one among contributed Drupal 8 module in this area — the Mail System module. It fills in the most noticeable gap of Drupal default mail system — namely, provides an administrative interface and an API for working with Drupal mail. This module is also successfully used in combination with other modules.

The Mail System module lets you configure the default mail system by selecting:

  • the standard plugin for email formatting before sending it
  • the standard plugin for sending an email after formatting it
  • the theme that will render the emails

In addition, you can make module-specific settings by selecting:

  • the module
  • the key for identifying specific mails
  • the formatter plugin
  • the sender plugin

the Mail System module in Drupal 8

Integration modules with the example of SendGrid

You could also rely on some popular third-party email service. For instance, consider connecting your Drupal 8 website to a service for transactional and marketing emails — SendGrid, which is trusted by Uber, Spotify, Glassdoor, Airbnb, Yelp and other large companies.

The SendGrid module for Drupal will help you with the integration, after which you will be able to send emails from your Drupal 8 website quickly, reliably and with flexible options. Your emails will be sent via SendGrid cloud-based delivery system with the help of their strong RESTful API.

We have chosen this service as an example, because the Drupal module for it has had a fresh stable version in June. Others include Mailgun, Mandrill, Elastic Email, Sparkpost, PeytzMail, as well as tools like the Swift Mailer library, and so on. All these Drupal third-party integration modules are now in different levels of stability.

The Views Send module

Meet the module that lets you send emails using the power of Views, which is now part of Drupal 8 core. It allows for personalized mass mailing from a View, where each row generates one email.

Using tokens with the Views fields, you can fully personalize your emails. They will have a subject, a body, a recipient's name, email address, and more taken from the View.

The Contact Emails module

Here is another module that adds advanced features to Drupal 8 core — namely, to the core module Contact, which allows users to send email to other users or site administrators via contact forms.

The Contact Emails module brings in such options as sending emails to multiple recipients, using text formats, tokens, and more. You can send as many emails as you wish with different messages.

The Simple Mail module

As the name suggests, the Simple Mail Drupal module makes mail delivery extremely simple, which is great for simple use cases.

With it, you can also queue your emails and manage the queues with the help of another useful module — the Queue UI, which provides a handy user interface for that.

The Reroute Email module

In many cases — for example, when you do some testing — you do not want the users of the site to receive your emails. So it is useful to be able to reroute all outgoing emails to another address. The Reroute email Drupal 8 module will take care of these scenarios.

The HTML Mail module

The HTML Mail module for Drupal 8 adds some theming to your emails. Namely, it lets you theme your email messages in the same way as you theme your website.

To recap

These is just a glimpse at useful modules and opportunities for mail sending in Drupal 8. In addition, there is a very simple way of providing a perfect mail sending system on your website in accordance with all your wishes. Send a short message here to just one recipient — our Drupal 8 team! The rest of the magic will be in our hands.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web