1

I'm sending order confirmation messages via the module message-notify. As mail system i'm using MimeMail. Is there a possibility to add an attachment to the email with the key: commerce_order_order_confirmation?

I tried to add the attachment in hook_mail()

function mymodule_mail($key, &$message, $params) {

    if(isset($key)) {
        if($key == 'commerce_order_order_confirmation') {
            $attachment = array(
                'filecontent' => file_get_contents(DRUPAL_ROOT . '/sites/default/files/pdf/myattachment.pdf'),
                'filename' => 'myattachment.pdf',
                'filemime' => 'application/pdf',
            );

            $message['params']['attachments'][] = $attachment;
        }
    }
}
2

You want to change the email before it is sent. So try this as a starting point (put together with the help of:
1. your post
2. https://drupal.stackexchange.com/a/98559/34326
3. https://www.drupal.org/node/2057703#comment-8343071):

function mymodule_mail_alter(&$message) {


    if ($message['id'] == 'message_notify_commerce_order_order_confirmation') {
        $file = file_create_url(file_build_uri('path-to-file-relative-to-your-files-folder.pdf'));
        $attachment = array(
            'filecontent' => file_get_contents($file),
            'filename' => 'test.pdf',
            'filemime' => 'application/pdf',
        );

        $message['params']['attachments'][] = $attachment;
    }
}

The file should be located in sites/default/files or whatever applies to your site. You should also change filemime to what matches your file.

I am getting errors "realpath() expects parameter 1 to be a valid path" and "is_file() expects parameter 1 to be a valid path", but the email with the attachment is sent anyway, which is why I am posting this.
The errors seem to be related to the 'filepath' parameter which is mentioned in the 3rd link, because depending on what I define, the results vary: eg. 'filepath' => $file,, which gives me no attachment and no error or 'filepath' => $file->uri,, which gives me an additional error about a "non-object" and no attachment.

http://passivemanagement.net/blog/2014/01/let-drupal-send-email-attachment-using-mimemail-module uses a different markup for the file, but right now I can't figure out, how to adapt it to the task at hand.

  • This lead to the correct way. I managed with 'filepath' => '/path/to/file.pdf' instead of 'filecontent' => .... – freakimkaefig Mar 15 '16 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.