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' => 'attached_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.
Project information
- Maintenance fixes only
Considered feature-complete by its maintainers. - Module categories: Integrations
- 334 sites report using this module
- Created by Gogowitsch on , updated
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
Releases
Development version: 8.x-1.x-dev updated 17 Mar 2024 at 20:24 UTC