In this second part of a two-part article, you'll learn how to manage email headers when creating a PHP email program, attach a file to a message, and more. This article is excerpted from chapter eight of the Zend PHP Certification Study Guide, written by Zend Technlogies (Sams; ISBN: 0672327090).
HTML emails will attempt to download their images and stylesheets from your web server. Because of security and privacy reasons, many MUAs will refuse to attempt these downloads, ruining the look of your HTML email message.
You can add your images as attachments to your email, and then point your HTML at the attached images:
Change the first Content-Type of your email to be multipart/related. Don't forget to include the boundary definition.
When you add an image as an attachment, include this additional header:
URL is the URL that you use inside the <img> tag to include your image.
// who is the email going to?
// change this to be *your* email address ;-)
$to = "stuart";
// what is the URL of the image?
$image = http://static.php.net/http://www.php.net/
// what is the message?
$message = "This is my fourth email sent from "
. "This email has an image "
. "<img src=""
. ""> here.\r\n";
// don't forget our blank line
$blank_line = "\r\n";
// define our boundary
$boundary_text = "php-12345";
$boundary = "—" . $boundary_text . "\r\n";
$last_boundary = " . $boundary_text . "—\r\n";
// add the MIME headers
$more_headers = "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/related;
. ""; type="text/html"\r\n";
// create the first part of the message
$mime_message = "If you are reading this, it means
that your e-mail client\r\n"
. "does not support MIME. Please
upgrade your e-mail client\r\n"
. "to one that does support MIME.\r\n"
// add the second part of the message
$mime_message .= "Content-Type: text/html;
// now add the attachment
$mime_message .= "Content-Type: image/gif;
. "Content-Location: "
// send the email
$result = mail (
"My first HTML e-mail with an embedded
Using Extra Command-Line Parameters
The fifth argument to mail() is a list of parameters to use when executing the sendmail wrapper.
These parameters have no effect at all if you are using PHP on Windows without a sendmail wrapper.