Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Send Emails

No security coverage Under active development 14 sites

This module provides a user interface for creating and editing email templates, and a service to send emails programmatically. It allows non-technical users to manage email content and offers flexibility for developers to integrate email sending into custom workflows.

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]

Ask your assistant about Send Emails

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

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
7
Tracked since
Jun 2023
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Release Timeline

Releases

Version Type Core PHP Release date
2.0.0-beta2 Pre-release 8–11 Dec 28, 2024
2.0.0-beta1 Pre-release 8–11 Oct 30, 2024
2.0.0-alpha8 Pre-release 8–10 Dec 17, 2023
2.0.0-alpha7 Pre-release 8–10 Jul 10, 2023
2.0.0-alpha6 Pre-release 8–10 Jun 6, 2023
2.0.0-alpha5 Pre-release 8–10 Jun 5, 2023
2.0.0-alpha4 Pre-release 8–10 Jun 5, 2023