Tuesday 24 May 2016

Drupal 7 - issue sending Attachments with hook_mail

If you're having issue with sending Attachments then here's how I solved the issue.  There are other ways but this is how I solved the issue when it wasn't working.


Install these modules 
  1.  Mail System - https://www.drupal.org/project/mailsystem
  2.  Swift Mailer - https://www.drupal.org/project/swiftmailer
You'll also need to install the Swift Mailer library this is done through Composer
 composer require swiftmailer/swiftmailer

Enable the above modules. 
Then you'll need to add the following to your $message array.
 


//File to Attach - this uses Swift Mail to attach the file.
      $file_to_attach = new stdClass();
      $file_to_attach->uri = 'sites/default/files/report.csv';
      $file_to_attach->filename = 'report.csv';
      $file_to_attach->filemime = 'text/csv';

      $message['params']['files'][] = $file_to_attach;

No comments: