How do I send an email from a module using an Easy Email module email template?
Sending an email from a custom module using an Easy Email template requires the following steps:


Create a new Easy Email template
Implement [MODULE].emails.yml
Tell the system to use the template from step 1 when sending the mail from your module
Send the mail from your module
(Optional) - Export and save configuration to your module


This example will show how to send an email to admins with the role site_admin when a group (using the Groups module) has applied for a review of some kind. The email requires the Group entity, to provide the group name and a link to the group.

1) Create a new Easy Email template

The email in this example will require a new template, with an entity reference field on the template for the Group entity that the email will refer to.


Navigate to Admin -&gt; Structure -&gt; Email Templates and click 'Add Email Template'.
Enter a name for the template, and make note of the machine name (the email template key). Save and proceed
In the template, set the following field values:


From Name: [site:name]
From Address: [site:mail]

Save.
Find the template you just created in the list, and under 'Operations', click 'manage fields'
Add an entity reference field to the Group entity and save
Back on the email templates listing page, edit the email. Group fields are available as tokens in the template body due to the entity reference to the group. The recipient of the mail is also available, and [easy_email:recipient_uid:0:entity:display-name] will provide the name of the recipient. Example email:

&lt;p&gt;[easy_email:recipient_uid:0:entity:display-name],&lt;/p&gt;

&lt;p&gt;The group &lt;a href="[easy_email:field_group:entity:url]"&gt;[easy_email:field_group:entity:title]&lt;/a&gt; has submitted an application for something. Please review it.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Thank you,&lt;/p&gt;

&lt;p&gt;--&amp;nbsp; [site:name] team&lt;/p&gt;



2. Implement [MODULE].emails.yml

Next, [MODULE].emails.yml needs to be implemented, so that the Easy Email module knows to override the email.

Note that this will require an arbitrary key for the email, that will be used to send the email from the module in step 4. In this example, the email key is group_something_application_received.

# This is the ID of the email override. I have keyed it by
# [ENTITY TYPE].[MAIL KEY].
group.something_application_received:
  # Re-use of the same ID above.
  id: 'group.something_application_received'
  # A label to be shown on the email override interface.
  label: 'Group: Custom domain application received'
  # This should be replaced with the machine name of the module.
  module: 'example'
  # The email key. This will be used by the module to send the email.
  key: 'group_something_application_received'
  # An array of parameters. At a minimum, account will be required for
  # the recipient of the email. In this example, Group is also required
  # to fill in the group values of the template.
  params:
    account:
      type: 'entity:user'
      label: 'Account'
    group:
      type: 'entity:group'
      label: 'Group'


3) Tell the system to use the template from step 1 when sending the mail from your module

The next thing is to link the email the module will send to the template created in step 1.


Navigate to Admin -&gt; Structure -&gt; Email templates
Click the 'Overrides' tab (note - requires the Easy Email Override module to be enabled)
Click 'Add Email Override'
Give it a label. Under 'Email to override', select the value entered for 'label' in `[MODULE].emails.yml'. Under 'Easy email template', select the template created in step 1. Save.


4) Send the mail from your module

Finally, emails can be sent using the system default email manager. The Easy Email manager will handle the rest of it. In this example, the email is being sent from the submission handler of a form that stored a Group object in the form state.

/**
 * {@inheritdoc}
 */
public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  $group = $form_state-&gt;get('group_entity');

  // Do some submission stuff (not shown).

  // Get the UIDs of all users with the site admin role.
  $uids = $this-&gt;entityTypeManager-&gt;getStorage('user')-&gt;getQuery()
    -&gt;condition('status', 1)
    -&gt;condition('roles', 'site_admin')
    -&gt;execute();

  // Load the admins.
  $admins = User::loadMultiple($uids);
  // Loop through the admins, and send the email:
  foreach ($admins as $admin) {

    // Send an email to the admin using the mail manager.
    \Drupal::service('plugin.manager.mail')-&gt;mail(
      // The module machine name:
      'example',
      // The email machine name (use the same one declared in [MODULE].emails.yml)
      'group_something_application_received',
      $admin-&gt;getEmail(),
      $admin-&gt;getPreferredAdminLangcode(),
      // Populate the parameters declared in [MODULE].emails.yml:
      [
        'account' =&gt; $admin,
        'group' =&gt; $group,
      ]
    );
  }
}


5) (Optional) - Export and save configuration to your module

This step is optional insofar as the module will send mail after completing step 4. However, if the module is to be used in multiple systems, default configuration should be exported from the system and saved to the [MODULE]/config/install directory. The configuration exported should be:


Template configuration for the easy email template
Override configuration for the email override


Note that if you wanted your module to easy-email compatible, but not dependent, you can put the configuration in [MODULE]/config/optional, and do not declare the Easy Email module a dependency. This way your configuration will be used if the Easy Email module is already installed on the system, but will not require it. If you do take this path, you'll also need to implement hook_mail() in your module and react to the key group_something_application_received to create the email text that will be sent when the Easy Email module is not installed.