Hudzilla.org - the homepage of Paul Hudson
Contents > Networks > Sending mail: mail() Wish List | Report Bug | About Me ]

15.5.4     Sending attachments (at last!)

This is NOT the latest copy of this book; click here for the latest version.

If you have made it this far, you will be pleased to know that you are just inches away from adding proper attachments to your email. In fact, rather than add explanation and keep you waiting, I am going to jump right in. Add this line after the call to setHTMLBody() :

$message->addAttachment("<some pic>");

You will of course need to change <some pic> to the name of a picture file in the same directory as the script. And... you are done! That is all it takes to add an attachment once you are using PEAR::Mail and PEAR::Mail_Mime, so I hope you will agree it was worth the effort to get this far.

If you run the script again, you should see the attachment has come through properly. However, there is one more thing you can do with PEAR::Mail_Mime and attachments, and that is to attach HTML images. These are essentially the same thing as attachments, except they are not shown as an attachment most HTML-compliant mail readers - they are shown only in the message body. This makes better sense for HTML pictures, because it would likely confuse people to see a dozen pictures attached to the mail that aren't of importance.

To add a HTML picture, use the addHTMLImage() function. As with addAttachment(), this takes the filename to attach as its only parameter. In order to use this picture, you need to edit the HTML file you are attaching and add the appropriate line, e.g.:

// in the PHP file:
$message->addHTMLImage("button.png");
// and in the HTML file:
<IMG SRC="button.png" />

Now when you send the mail, button.png should be sent along too and displayed inside the message. In Outlook, this results in the first picture file being attached, and the second file being attached (but not listed as an attachment) and shown inside the message - perfect!





<< 15.5.3 Sending mixed-type messages with PEAR::Mail_Mime   15.6 Reading mail >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 28 Aug 2008

It works good, but i thing that you don't need to use addAttachment() function.

john.draughn@gmail.com - 28 Aug 2008

The above code did not work for me until I did the following:

// works if within your HTML you have 'image.png'
$message->addHTMLImage("path/to/image.png", "image/png");

You might also want to mention that it's possible to catch a PEAR error object should this function fail.

$res =& $message->addHTMLImage("path/to/nonexistantfile.png", "image/png");
if(PEAR::isError($res)) {
// handle error, or
die($res->getMessage());
}

smitharao@mmpsd.co.in - 28 Aug 2008

fdsfgds



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow