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:
- Use Zend Framework mail lib.
- Use PECL mail.
- 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
Thank you, exactly what I needed. I will try this out tomorrow!
Thanks for this post
NP
(No subject)
(No subject)
(No subject)
Added support for multiple files
Multiple attachment
Oops I'm not reading...
Post new comment