HomePHP Page 2 - Building A PHP-Based Mail Client (part 2)
A Picture Is Worth A Thousand Words - PHP
Now that you've got a basic mail reader up and running, it's timeto learn a little more about attachments. This second segment analyzesMIME-encoded attachments, demonstrating how to decode and download them,and then integrates attachment handling features into the primitive mailclient previously demonstrated.
The best way to understand how message attachments work is with an example. Consider the following email message, which contains no attachments,
Return-Path: <jane@some.domain.com>
Received: from localdomain (unknown [205.89.123.112])
by mail.domain.com (Postfix) with ESMTP id B0AYBHA41
for <john@some.domain.com>; Thu, 6 Dec 2001 21:05:51 +0530 (IST)
Message-Id: <3.3.7.32.20011206210943.00b26d40@melonfire.com>
Date: Thu, 06 Dec 2001 21:09:43 +0500
To: John Doe <john@some.domain.com>
From: Jane Doe <jane@some.domain.com>
Subject: Photos
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Do you still have those vacation photos with you? Send me a copy if you do.
- Jane
and then contrast it with this one, which does.
Return-Path: <john@some.domain.com>
Received: from localdomain (unknown [205.89.123.112])
by mail.domain.com (Postfix) with ESMTP id B0AYBHA41
for <jane@some.domain.com>; Thu, 6 Dec 2001 21:05:51 +0530 (IST)
Message-Id: <3.0.3.32.20011207082225.006e927c@localhost>
Date: Fri, 07 Dec 2001 08:22:25 +0500
To: Jane Doe <jane@some.domain.com>
From: John Doe <john@some.domain.com>
Subject: Re: Photos
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="=====================_1007675545==_"
--=====================_1007675545==_
Content-Type: text/plain; charset="us-ascii"
Hi Jane,
Find photos attached.
John
--=====================_1007675545==_
Content-Type: application/zip; name="photos1.zip";
x-mac-type="705A4950"; x-mac-creator="705A4950"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="photos1.zip"
UEsDBBQAAAAIABythCvu1mnpugIAALsIAAALAAAAY29tcG9zZS5waHC1lltP2zAUx59Xqd/
-- snip --
AK6LAAAAAA==
--=====================_1007675545==_
Content-Type: application/zip; name="photos2.zip";
x-mac-type="705A4950"; x-mac-creator="705A4950"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="photos2.zip"
UEsDBBQAAAAIAACRDSux3m3PFwIAABsIAAAXAAAAc29hcGxpYi5zb2FwaW50ZXJvcC5waHC
-- snip --
cFBLBQYAAAAACwALALcCAABRfQAAAAA=
--=====================_1007675545==_--
As you can see, the message containing attachments differs
from the plaintext message in a couple of important ways.
1. Since it consists of different parts (one for the body and one for each attachment), it's Content-Type: header specifies the message type as "multipart/mixed".
Content-Type: multipart/mixed;
2. The various message sections are demarcated by a boundary
"marker"; this marker is unique to each message, and is defined in the main message header so that MIME-compliant mail clients can distinguish between the various message parts.
If you look at the example above, you'll see that, as
specified in the message header, the boundary marker "=====================_1007675545==_" is used as a separator between the different parts of the message.
3. Since the SMTP protocol cannot handle binary data, binary attachments - in this case, zip archives - must be encoded into a text representation using one of a number of different encoding methods. This information, together with information on the attachment type and original filename, appears in the header for each message section.
This provides the mail client with all the information it
needs to decode and display the attachment. It can use the Content-Transfer-Encoding: header to determine how best to decode the text block back into binary data, the filename to determine what the resulting file should be called, and the Content-Disposition: header to decide whether the binary data should be displayed as an attachment or within the message body.
Note that this discussion is restricted to MIME attachments only. Other standards to handle mail attachments do exist. Take a look at http://www.faqs.org/rfcs/rfc2045.html for the MIME specification.