Attach documents (e.g. Terms and conditions) to Drupal Commerce Message order confirmations

Drupal Commerce Message

Attaching documents like PDFs to Drupal eMails is quite simple. For our specific case we needed to attach PDF documents to Drupal Commerce Message order confirmations.

This can easily be done using the hook below. Simply modify the names and path to the document to attach. That's it!

  1. function MY_MODULE_mail_alter(&$message) {
  2.     if ($message['id'] == 'message_notify_commerce_order_order_confirmation') {
  3.         $file = file_create_url('public://media/my-document-to-attach.pdf');
  4.         $attachment = array(
  5.             'filecontent' => file_get_contents($file),
  6.             'filename' => 'test.pdf',
  7.             'filemime' => 'application/pdf',
  8.         );
  9.  
  10.         $message['params']['attachments'][] = $attachment;
  11.     }
  12. }

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.

More information about formatting options

Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.