
Introduction
Sending email is a crucial requirement for many Drupal websites. Whether it's user registration confirmations, password resets, contact form submissions, or custom notifications, Drupal provides a flexible system for sending emails programmatically. This article details how to leverage Drupal's Mail Manager to send emails effectively in Drupal 8, 9, and 10.
Understanding Drupal's Mail System
Drupal's mail system is built around the \Drupal\Core\Mail\MailManagerInterface
. This interface provides a standardized way to format and send emails. It handles the complexities of email formatting, queuing (if configured), and delivery. Key to this process are:
- Mail Manager: The central service for sending emails.
- Hook_mail(): Allows modules to define email templates. Though less common these days, understanding it can be helpful when debugging legacy code.
- Render Arrays: Drupal encourages the use of render arrays to construct the email body, ensuring proper theming and escaping.
Sending a Simple Email
Here's a step-by-step guide to sending a basic email from a custom Drupal module:
-
Create a Custom Module: If you don't already have one, create a custom Drupal module. For example, let's call it `my_module`.
-
Implement the Sending Logic: Within your module, create a service or a function (depending on your preferred approach) to handle the email sending. Below is example code that could live inside your module, or in a custom service.
use Drupal\Core\Mail\MailManagerInterface; use Drupal\Core\Render\Markup; /** * Sends a test email. * * @param string $to The recipient's email address. * @param string $subject The email subject. * @param string $body The email body. * * @return bool TRUE if the email was sent successfully, FALSE otherwise. */ function my_module_send_email(string $to, string $subject, string $body): bool { $mailManager = \Drupal::service('plugin.manager.mail'); $module = 'my_module'; $key = 'my_email'; // A unique key for your email. $params = [ 'message' => $body, ]; $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); $send = true; // Set to FALSE to only format, not send. $result = $mailManager->mail($module, $key, $to, $langcode, $params, null, $send); if ($result['result'] === true) { \Drupal::messenger()->addMessage('Email sent successfully!'); return true; } else { \Drupal::messenger()->addError('There was a problem sending your email: ' . $result['result']); return false; } } //Example usage (inside a form submit handler or other logic) $to = 'recipient@example.com'; $subject = 'Test Email'; $body = '
This is a test email sent programmatically from Drupal.
Important: Please confirm receipt.'; $sent = my_module_send_email($to, $subject, $body); -
Call the Function: You can now call the
my_module_send_email()
function from within a form submit handler, event subscriber, or any other place in your code where you need to send an email.
Formatting Email Content
It's important to properly format your email content to avoid security vulnerabilities and ensure consistent rendering. Drupal encourages the use of render arrays. You can use Markup::create()
to avoid XSS issues if your html is already sanitised.
Advanced Options
- Attachments: The MailManager allows for sending attachments by including an 'attachments' key in the
$params
array. - Queuing: To improve performance, consider using Drupal's queue system to process emails asynchronously.
- HTML Emails: Ensure your mail configuration is set to allow HTML emails. Also be sure to set the 'Content-type' header as 'text/html' inside the $params.
Key Takeaway: Sending emails programmatically in Drupal is straightforward using the MailManager. By understanding the key concepts and following best practices for formatting and sending email, you can effectively integrate email functionality into your Drupal applications.