SpinSpire logo

mimemail attachments - order matters

I wrote a module that generates PDF and attaches it to an email using hook_mail_alter(). It took me a while to figure out that I cannot add the attachment simply as a string in memory. I had to write it to a temp file first. And then my hook_mail_alter() implementation ...

/**
 * Hook to allow attaching PDF file.
 */
function mymodule_mail_alter(&$message) {
   // generate the PDF file in $filepath
    $file = array(
      'filename' => 'tix.pdf',
      'filemime' => 'application/pdf',
      'filepath' => $filepath,
    );

    // add the attachment by appending to the $message['params']['attachments'] array
    $message['params']['attachments'][] = $file;
  }
}

On my own machine the above was working perfectly and attachment was going out with the email. But on my hosting provider, it didn't work for some reason. I struggled with it for a day. And finally, I just decided to disable to the 'mimemail' module (this module is required for attachments to work). Obviously, attachments didn't work because they can't work without this module. But just as soon as I re-enabled it - attachments started working!

I suspect it had something to do with the order in which modules are enabled and therefore the order in which certain hooks are invoked. I still don't know the exact reasoning behind it. But the moral of the story is - THE ORDER IN WHICH MODULES ARE ENABLED MATTERS.