Sending E-mails with Attachments in Drupal 7

Update Nov-14-2011
The commenter gollyg has posted an updated version of the code that allows for multiple attachments to be added. Thanks gollyg! https://github.com/gollyg/Attachment-Email

After some Googling I came across a few options:

  1. Use Zend Framework mail lib.
  2. Use PECL mail.
  3. Write a one-off class based on numerous examples available.

I ended up going with option 3. I really like the Zend framework but couldn’t justify including 20mb in library files just to send e-mails. The class I ended up basing functionality off of was (IMHO) a cleaner implementation than the PECL lib. The implementation is super clean, sending emails with attachments can be done with just a few lines of code:


require_once('attachment_email.php');
$to="ish.williams1949@gmail.com";
$from="ngmaloney@gmail.com";
$subject="Check out this report!";
$message="Yo! Check out this report!";
$attachment = array(
  'uri' => '~/documents/report.pdf',
  'filename' => 'report.pdf',
);
$email = new AttachmentEmail($to, $from, $subject, $message, $attachment);
$email->send();

The class file can be found here. It was based on an example I found which is credited in the README file. The README file also contains usage documentation. It is currently being bundled with another module we are working on but I may make it a contrib API. More to come on the module it is being bundled with...

9 comments

by jones (not verified) on Mon, 01/17/2011 - 10:33

by Patrick (not verified) on Wed, 03/23/2011 - 20:57
Works excellently. Sending an email is rather complex in D6/7, and trying to get attachments going in a custom module at the moment in Drupal 7 is insanely difficult.

by admin on Wed, 03/23/2011 - 21:25
Glad it could help. I had the same problems so figured i'd document so others wouldn't.

by Miyukaze (not verified) on Wed, 07/06/2011 - 11:35
Hi, Thank you very much for that piece of code =) I'm searching how to automatically send the PDF of my simplenews newsletter as attachment when I send my newsletter to subscribers but as you said, Mime Mail is still in early dev and does not support attachment yet. Do you have any advices for me? I'm working on adding your code in Mime Mail module for hours and i'm becoming totally insane T_T (I'm pretty novice with Drupal) Sorry for my bad english! And thanks again! ---- Miyu'

by admin on Wed, 07/06/2011 - 11:46
Miyu, You'll need to write some custom code. I would create a custom module that contains 2 functions: 1. a hook_form_alter that adds an additional submit hook to your newsletter form (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#submit_property). 2. A custom submit hook that includes the AttachmentEmail class. This is where you'd put the logic for sending the PDF.

by Miyukaze (not verified) on Wed, 07/06/2011 - 11:53
Wow thanks! You're quick! I'll try to think about this tomorrow, for now: end of my internship day ^__^ Thank you again, maybe i'll come back for some other advices :) Miyu'

by gollyg (not verified) on Thu, 11/10/2011 - 01:56
Thanks for posting this. I had 'assumed' this would be handled easily by Drupal 7 when the client asked for it! This was a bit of a life saver. My requirements were for multiple files, so I had to mod the code a bit. It is forked on github at https://github.com/gollyg/Attachment-Email Thanks again.

by darrylmabini (not verified) on Fri, 11/11/2011 - 08:51
Is this gonna work with multiple file attachments? Darryl

by darrylmabini (not verified) on Mon, 11/14/2011 - 03:49
Thank you gollyg...

Post new comment

By submitting this form, you accept the Mollom privacy policy.
© 2010 Nicholas G. Maloney. Drupal theme by Kiwi Themes.