How to send a mail with an attachment in Drupal8

Sometimes we require emails to be sent with a file attachment, for example, to upload a resume in case of applying for a job or to mail the files provided by the website. By default, drupal doesn’t support mail attachments so we need to write a new module. In this article, we will explore how to use swift mailer module to send the mail with an attachment.

It is a two step process:

  • Installing and configuring the Swift Mailer and Mail System modules.
  • Implementing hook_mail function.


Step 1: Installing and configuring the required modules

The Swift Mailer module extends the basic e-mail sending functionality provided by Drupal by transferring all e-mail handling to its library. The Swift Mailer module depends on the Mail System module. So, we need to install Mail System and Swift Mailer modules, then we can set Drupal default mail system to swift mailer in /admin/config/system/mailsystem  as shown below:

Note: Swift Mailer module requires the Swift Mailer PHP library. The preferred way to install the module is via Composer:

$ composer require swiftmailer/swiftmailer

After the installation, we need to configure it in /admin/config/swiftmailer/transport as follows:

We can select any transport type as per our mail server type. In our case, we are using PHP mailer.

Step 2: Implementation of hook_mail

After successfully installing the required modules we need to implement hook_mail() in order to define one or more email templates. Then, we use mail manager to send emails using one of these templates.

* Implements hook_mail().
function <module_name>_mail($key, &$message, $params) {
 switch ($key) {
   case 'the_mail_key':
     $message['from'] = \Drupal::config('')->get('mail');
     $message['subject'] = t('Mail Subject');
     $message['body'][] = $params['message'];
     $message['params']['files'] = $params['attachments'];

Here, the variables can be described as:

  • $key: An identifier of the mail.
  • $message: An array to be filled in. Elements in this array include id, subject, to, body, from, headers.
  • $params: Array of data that we need to send to the email which is passed from the mail manager.


Now, we need to create a custom function for triggering the mail using mail manager plugin.

function foo_bar(){
     $mailManager = \Drupal::service('plugin.manager.mail');
     $module = '<module_name>';
     $key = 'the_mail_key';
     $to = \Drupal::currentUser()->getEmail(); // Or ''
     $params['message'] = 'Email with an attachment';
     //Attaching a file to the email
     $file  = new stdClass();
     $file->uri = 'sites/default/files/file_name.pdf'; // File path
     $file->filename = 'file_name.pdf'; //File name
     $file->filemime = 'application/pdf'; //File mime type
     $params['attachments'][] = $file;
     $langcode = \Drupal::currentUser()->getPreferredLangcode();
     $send = true;
     $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

We need the following information:

  • $module: The module name to invoke hook_mail().
  • $key: The key to identify which email is being sent.
  • $to: The mail address or addresses where the message will be sent.
  • $langcode: The language code used to compose the email.
  • $params: Array of parameters which are used to build the email.
  • $params[‘attachments’] : The email attachment.
  • $send : If the value is set to true, $result calls an implementation of \Drupal\Core\Mail\MailInterface->mail() to deliver the message and stores the result in $message[‘result’].


After successfully completing the above steps you can check your email inbox for the attachment. This is how we send emails with attachment programmatically in Drupal8.


About The Author

Leave a Reply