mimemail attachments - order matters

mimemail attachments - order matters

Submitted by Jitesh Doshi on Sat, 11/05/2011 - 15:25

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.

Jitesh Doshi

Profile picture for user Jitesh Doshi
Managing Partner & CTO
  • A seasoned technology entrepreneur and enthusiast
  • A regular speaker at industry conferences and universities
  • Host and organizer of technology user groups
  • Active in management of non-profit organizations serving the local community
  • Leader and contributor for multiple open-source projects
  • Expert in cloud, application integration, web and mobile technologies
  • Author of open-source projects, including on Drupal.org - Popular Tags and PRLP.
  • Developed several highly successful software platforms and frameworks for clients