The theory of the Mail API

Like before, let's first cover this API from a theoretical point of view. It's important to understand the architecture before diving into examples.

Sending emails programmatically in Drupal is a two-part job. The first thing we will need to do is define something of a template for the email in our module. This is not a template in the traditional sense, but rather a procedural data wrapper to the email you want to send. It's referred to in our code as the key or message ID, but I believe that template is a better word to describe it, and it works by--you guessed it--implementing a hook.

The second thing that we will need to do is use the Drupal mail manager to send an email using one of the defined templates and ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.