Drupal is a registered trademark of Dries Buytaert
Menu Per Role 8.x-1.9 Minor update available for module menu_per_role (8.x-1.9). AI (Artificial Intelligence) 1.5.0-alpha2 New alpha version released for module ai (1.5.0-alpha2). AI (Artificial Intelligence) 1.5.0-alpha1 First alpha version released for module ai (1.5.0-alpha1). Easy Encryption 1.0.4 Minor update available for module easy_encryption (1.0.4). Soccerbet 1.1.15 Minor update available for module soccerbet (1.1.15). United Nations Maps & Geoservices 3.0.8 Minor update available for module unep_maps (3.0.8). Burndown 1.0.71 Minor update available for module burndown (1.0.71). Media Embed Code 1.0.2 Minor update available for module media_embed_code (1.0.2). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs. Cha-ching donation register 1.0.18 Module chaching updated after 6 months of inactivity (1.0.18).

This module allows developers to programmatically attach files to emails sent from Drupal. It intercepts emails and adds attachments based on specific parameters provided in the email settings.

This is a module for developers who want to attach files programmatically to emails sent from Drupal 8+. This module doesn't send emails but rather intercepts emails from other modules.

If an 'attachment' or an 'attachments' key is present in the $params array, it will be converted into an actual MIME attachment. Otherwise, this module doesn't touch the mail.

This module has no configuration options.

Single attachment

Example code to put into your module:

      $params = [
        'attachment' => [
          'filecontent' => 
            'Your attachment contents. ' .
            'You could use file_get_contents() instead of ' .
            'this string.',
          'filename' => 'filename_how_' .
            'recipients_see_it.txt',
          'filemime' => 'text/plain',
        ],
        // further params as needed by your hook_mail
      ];
      $mail_manager = \Drupal::service('plugin.manager.mail');
      $result = $mail_manager->mail($your_module, $your_key, $to, $lang, $params);

Multiple attachments

If you need to attach more than one file, use the array key 'attachments' instead, and nest your files in an array:

      $params = [
        'attachments' => [
          [
            'filecontent' => 
              'Your attachment contents. ' .
              'You could use file_get_contents() instead of this string.',
            'filename' => 'attached_filename_how_recipients_see_it.txt',
            'filemime' => 'text/plain',
          ],
          [
            'filecontent' => file_get_contents('temporary://test.pdf'),
            'filename' => 'Invoice.pdf',
            'filemime' => 'application/pdf',
          ],
        ],
        // further params as needed by your hook_mail
      ];
      $mail_manager = \Drupal::service('plugin.manager.mail');
      $result = $mail_manager->mail($your_module, $your_key, $to, $lang, $params);

Files can be binary, too.

The 'filemime' key is optional. If it is missing then Drupal's ExtensionMimeTypeGuesser will be used.

The 'filecontent' key is optional, if 'filename' points to an existing file on the webserver.

You can also use the mail_alter hook to insert attachments. The following example is for a hypothetical module called “mymodule”:

// attach a file to submission emails sent by the webform module
function mymodule_mail_alter(array &$message): void {
  if (isset($message['module']) && $message['module'] == 'webform') {
      $message['params']['attachments'][] = [
        'filecontent' => MymoduleElement::getFileString($message['params']['webform_submission']),
        'filename' => t('QR code') . '.png',
        'filemime' => 'image/png',
      ];
  }
}

For working examples, see the email_attachment_demo sub-module and the webform_qr_code_element contrib module.

Supporting this module

To say “thank you” or financially support development of this module, you can send a few Euros via PayPal: [email protected]

Activity

Total releases
1
First release
Jul 2026
Latest release
21 hours ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Release date
8.x-1.6 Stable Jul 19, 2026