Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

$attach[] = array("name"=>basename(FILE),
"content"=>fread($fp, filesize(FILE)),
"type"=>"application/octet-stream");
fclose($fp);


//send mail to root
mailAttachment("root@localhost",
"httpd@localhost",
"Listing 18.6",
$attach);


print("Mail sent!
\n");
?>


The mailAttachment function assembles the parts that make up a MIME message.
These parts are sent in the fourth argument of the mail function, which is generally used
for headers. In the case of a MIME message, this area is used for both headers and
attachments. After sending the customary From headers, a MIME-Version header is
sent. Unless there's onlyone attachment, a boundary string must be created. This is used
to divide attachments from one another. We want to avoid using a boundary value that
might appear in the message itself, so we use the uniqid function.


Each attachment is surrounded by the boundaries that always start with two dashes. The
attachment itself is prepared by the makeAttachment function. Each attachment
requires Content-Type and Content-Transfer-Encoding headers. The type
of content depends on the attachment itself. If an image file is being sent, it might be
image/jpg. These are the same codes discussed above with regard to the HTTP
protocol. For the sake of simplicity, this function always encodes attachments using
base64, which can turn binary files into 7-bit ASCII. This prevents them from being
corrupted as they travel through the network. As you might imagine, text files don't
require encoding, and complete implementations encode attachments based on content
type.


It may be instructive to see the assembled message in full. Try sending yourself a
message. On a UNIX operating system, you should be able to peek at the file itself inside
/var/spool/mail before reading it, or perhaps inside ~/Mail/received
afterward.


Verifying an Email Address


It doesn't take much experience with email to discover what happens when it is
misaddressed. The email is returned to you. This is called bounced email. Consider for a
moment a Web site that allows users to fill out a form that includes an email address and
sends a thank-you message. Certainly many people will either mistakenly mistype their
addresses or purposely give a bad address. You can check the form of the address, of
course, but a well-formed address can fail to match to a real mail box. When this

Free download pdf