Drupal is a registered trademark of Dries Buytaert
Release: Drupal 10.3.6 Update released for Drupal core (10.3.6)! Release: Drupal 11.0.5 Update released for Drupal core (11.0.5)! Release: Drupal 10.2.9 Update released for Drupal core (10.2.9)! Release: Drupal 10.2.10 Update released for Drupal core (10.2.10)! Release: Drupal 10.3.7 Update released for Drupal core (10.3.7)! Release: Drupal 11.0.6 Update released for Drupal core (11.0.6)! Release: Drupal 10.3.8 Update released for Drupal core (10.3.8)! Release: Drupal 11.0.7 Update released for Drupal core (11.0.7)! Module Revived: Translation Management Tool 8.x-1.19 Module tmgmt updated after 7 months of inactivity (8.x-1.19). Usage Milestone: Account field split Module account_field_split crossed 10,000 active installs.

Send Emails

No security coverage
View on drupal.org

Requires developing experience

Have you ever wanted to send users an email, but didn't want to use Rules?

And wouldn't it be nice to have a user interface to be able to edit the said email? A user interface you could show non-technical users, instead of you updating it all the time?

Introducting Send Emails, a light-weight module that provides a UI to create & update emails and a service to send the emails:

\Drupal::service('send_emails.mail')->notifyUsersByRole( $notificationRole, $emailTemplate);

You can also include custom TWIG variables using:

$autoLoginLinkUrl = ''; // If an empty string, will use the "Url for Auto Login Link" in the User Interface
$replyToEmail = ''; // If an empty string, will use the "Reply To Email" in the User Interface
$twigVariables = []; // Additional Twig variables that can be accessed in the email template
\Drupal::service('send_emails.mail')->notifyUser($user, $emailTemplate, $autoLoginLinkUrl, $replyToEmail, $twigVariables);

See ./src/EmailApi.php for more functions and documentation

Example

Here's how I use it in a custom module:

  1. Install the module using the normal process (see documentation)
  2. Create a new email
  3. Go to Configuation > Send Emails Configuation: /admin/config/send_emails/emails#edit-emails-definitions
  4. Define a new email using:

    director_private_notes | Send an email when a node "director_agendas" is created or updated and the "field_send_emails_notify" checkbox is checked
  5. Click "Save"
  6. The page should now have a new email that you can edit
  7. Add the below code to a custom module
  8. Flush the caches
function YOURMODULE_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
  switch ($entity->bundle()) {
    case 'director_agendas':
      $notificationRole = 'testing';
      $emailTemplate = 'director_private_notes';
      
      if ($entity->hasField('field_send_emails_notify') && $entity->field_send_emails_notify->value == TRUE) {

          $result = \Drupal::service('send_emails.mail')->notifyUsersByRole( $notificationRole, $emailTemplate);

          $entity->set('field_send_emails_notify', FALSE);

          $lastNotificationTime =  \Drupal::service('date.formatter')->format($entity->changed->value, 'last_notified');
        
          $entity->set('field_send_emails_last_sent', 'Sent ' . $lastNotificationTime);

          \Drupal::messenger()->addMessage(
              t('%role have been notified', 
              ['%role' => ucfirst($notificationRole)])
          );

      }
      break;
  }
}

Documentation

Email Definitions

Email Fields:

  • Subject: Supports TWIG variables (see below). The subject of the email.
  • Reply To Email: the email used when replying to the email (Drupal forces using the site's email as the from address)
  • Body: Supports TWIG variables (see below). The HTML body of the email.
  • Url for Auto Login Link: the url used in to the {{ auto_login_link }} TWIG variable. Allows the user to access the url wile being automatically logged in.

TWIG Variables:

  • {{ name }} (e.g. username): the username of the user the email is being sent to or the name provided
  • {{ auto_login_link }}: a URL link string that automatically logs in the user
  • {{ site_name }}: the name of the site (defined in Basic Settings)
  • {{ site_front }}: url to the site's front page
  • {{ misc.time-raw }}: the UNIX timestamp
  • {{ misc.userEntity }}: the Drupal user entity (if it exists)
  • You can edit email definitions with only the subject and body by going to: /admin/config/send_emails/emails/minimal/[email_template_machine_name]
  • You can manually send an email to users with a role by:
  1. Enabling the "Send Emails - Manual" submodule
  2. Going to: /admin/config/send_emails/manual/[email_template_machine_name]

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Oct 2024
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Releases

Version Type Core Release date
2.0.0-beta2 Pre-release Dec 28, 2024
2.0.0-beta1 Pre-release Oct 30, 2024