0

I am creating a form that needs to send an email upon form submission. I want to use the Easy Email module to send HTML emails. I want to send an email from my module, using an Easy Email template. How can I send an email from my custom module using an Easy Email template?

2

Sending an email from a custom module using an Easy Email template requires the following steps:

  1. Create a new Easy Email template
  2. Implement [MODULE].emails.yml
  3. Tell the system to use the template from step 1 when sending the mail from your module
  4. Send the mail from your module
  5. (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.

  1. Navigate to Admin -> Structure -> Email Templates and click 'Add Email Template'.
  2. Enter a name for the template, and make note of the machine name (the email template key). Save and proceed
  3. In the template, set the following field values:
    • From Name: [site:name]
    • From Address: [site:mail]
  4. Save.
  5. Find the template you just created in the list, and under 'Operations', click 'manage fields'
  6. Add an entity reference field to the Group entity and save
  7. 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:

    <p>[easy_email:recipient_uid:0:entity:display-name],</p>
    
    <p>The group <a href="[easy_email:field_group:entity:url]">[easy_email:field_group:entity:title]</a> has submitted an application for something. Please review it.</p>
    
    <p>&nbsp;</p>
    
    <p>Thank you,</p>
    
    <p>--&nbsp; [site:name] team</p>
    

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.

  1. Navigate to Admin -> Structure -> Email templates
  2. Click the 'Overrides' tab (note - requires the Easy Email Override module to be enabled)
  3. Click 'Add Email Override'
  4. 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 &$form, FormStateInterface $form_state) {
  $group = $form_state->get('group_entity');

  // Do some submission stuff (not shown).

  // Get the UIDs of all users with the site admin role.
  $uids = $this->entityTypeManager->getStorage('user')->getQuery()
    ->condition('status', 1)
    ->condition('roles', 'site_admin')
    ->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')->mail(
      // The module machine name:
      'example',
      // The email machine name (use the same one declared in [MODULE].emails.yml)
      'group_something_application_received',
      $admin->getEmail(),
      $admin->getPreferredAdminLangcode(),
      // Populate the parameters declared in [MODULE].emails.yml:
      [
        'account' => $admin,
        'group' => $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:

  1. Template configuration for the easy email template
  2. 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.