--- name: drupal-mail description: Drupal mail system configuration for local testing (Mailpit) and production (Mailchimp Transactional). Covers HTML email rendering, Content-Type headers, sender/formatter plugins, and troubleshooting. Use when testing emails locally, debugging email issues, or configuring mail delivery. --- # Drupal Mail System Configuration ## Quick Reference ### Local Testing (DDEV Mailpit) **Working configuration for HTML emails in Mailpit:** ```yaml # mailsystem.settings defaults: sender: php_mail formatter: mailchimp_transactional_mail ``` **Enable/disable notifications:** ```bash # Check if notifications are disabled ddev drush state:get my_notifications.disabled # Enable notifications (for local testing) ddev drush state:set my_notifications.disabled 0 # Disable notifications (to stop local emails) ddev drush state:set my_notifications.disabled 1 ``` **View Mailpit:** - URL: https://.ddev.site:8026 - Or run: `ddev mailpit` ### Production Configuration **Mailchimp Transactional (Mandrill) for sending:** ```yaml # mailsystem.settings defaults: sender: mailchimp_transactional_mail formatter: mailchimp_transactional_mail ``` ## Mail System Architecture ### Key Concepts 1. **Sender**: The plugin that actually sends the email (SMTP, PHP mail(), API) 2. **Formatter**: The plugin that formats the email body (converts HTML, wraps content) 3. **Content-Type Header**: Must be set to `text/html` for HTML rendering ### Plugin Combinations | Environment | Sender | Formatter | HTML Works? | Notes | |------------|--------|-----------|-------------|-------| | Local (Mailpit) | `php_mail` | `mailchimp_transactional_mail` | Yes | Recommended for local testing | | Local (Mailpit) | `php_mail` | `php_mail` | No | Strips HTML to plain text | | Production | `mailchimp_transactional_mail` | `mailchimp_transactional_mail` | Yes | Uses Mandrill API | | Production | `symfony_mailer` | `symfony_mailer` | Yes | Requires SMTP config | ## HTML Email Requirements ### Content-Type Header **Critical**: Module must set Content-Type header in `hook_mail_alter()`: ```php /** * Implements hook_mail_alter(). */ function my_notifications_mail_alter(&$message) { if ($message['module'] === 'my_notifications') { $message['headers']['Content-Type'] = 'text/html; charset=UTF-8'; } } ``` Without this, HTML renders as plain text even with correct formatter. ### Message Template Format **Config entity format** (`message.template.*.yml`): ```yaml text: - value: |- Subject line with [tokens]

HTML body content

format: full_html ``` **Key points:** - First line = email subject (extracted, stripped of HTML) - Rest = HTML email body - Use inline styles (email clients strip `