Home arrow PHP arrow Page 3 - Building A PHP-Based Mail Client (part 2)

The Way Things Work - 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.

  1. Building A PHP-Based Mail Client (part 2)
  2. A Picture Is Worth A Thousand Words
  3. The Way Things Work
  4. Structure And Syntax
  5. Room With A View
  6. Getting Down
  7. Miles To Go
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 12
January 11, 2002

print this article


After a little research, it quickly becomes apparent that a standard process flow can be developed for attachment handling, both while sending and reading mail. Here's my first draft of the process:

When reading email,

1. Check the message's Content-Type: header and count the number of message parts.

If the Content-Type: header indicates that the message is in plaintext, you can skip ahead to the last step. If it's anything but "text/plain", it's a fair bet that you'll have to parse it further.

An alternative here would be to parse the message body and count how many parts it's divided into - if it's two or more, you can again expect to have to parse it further.

2. Parse the message body.

If the message is a multipart message - for example, the type "multipart/mixed" - it is necessary to parse the various sections of the message to determine the attachment attributes.

As explained previously, every message section includes a header providing information on the data enclosed within that section. This data is used by the mail client to figure out how to handle that particular section.

3. Display the message section(s).

Once the client has sufficient information on the number and type of attachments, the final step is to display each section. Plaintext sections can be displayed as is; encoded sections may need to be decoded and then displayed. If the client supports HTML-encoded mail - as most Web-based clients do - it may need to jump through a few additional hoops to decode and render embedded HTML markup.

It's important to note that different mail clients handle multipart messages differently. Some clients display each section as is (raw text) without processing it further; others use the encoding information present in the section header to decode and display each section separately. Some clients display each message section as an attachment, while others use the data type of each section to decide whether the decoded section should be displayed inline (within the message body) or as an attachment.

What about sending email? Well, that's also fairly simple - all you need to do is build a message incrementally, with each part representing an attachment and a boundary marker separating the various parts.

1. Build the message headers.

When sending email, the first step is to build the message headers. Some of these headers are built in response to user input - for example, the subject line and recipient addresses - while others must be added by the client itself. Typically, if one or more message attachments exist, the client will need to alter the Content-Type: of the message and generate a boundary marker to demarcate the various message sections.

2. Add the message body and attachments.

Once the headers have been built, the next step is to add the message text. If attachments exist, they must be encoded into plaintext and appended to the message body, with the message's unique boundary marker separating the various sections from each other.

3. Send the message.

With the headers and body both ready, the final task is to actually connect to a mail server and send the mail out. Some programming languages (like PHP) offer high-level constructs to perform this function; others require you to use low-level constructs to open up a socket connection to the server and send the message using socket communication techniques.

>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: