My *nix world

PHP send html mail with attachment

I've seen this topic placed all over at stackoverflow.com so I guess that people just want to send mail with attachments via PHP 🙂

You may found (incomplete) fragments of answer here and there but very few seem to be curious enough to study the problem from ground-up.

So, what is an email anyway? Well, the short version: it is an form of Internet message. You can track it back somewhere in the middle of 1982 (see RFC 822). The things changed since then and the format of these Internet messages (whether it is a HTML document or just an email message) improved over the time. Although there may exist different versions of that specification I will refer here the RFC 2045 as it contains exactly the specifications of "Format of Internet Message Bodies".

Basically an e-mail message looks like this:

internet-message

The simplest version of it would be a plain-text message which would have the following format/content:


We can clearly see two blocks, one on top of the other (separated by an empty line / a CRLF). The one above is the message header (lines 1-6), the next on (after the empty line) is the message body (line 8). Normally we should declare the format of the content we send, i.e. what is the Content-Type and how is encoded during transfer. If we skip this fields the email agents will assume by default text/plain and respectively 7bit. I just declared these fields in the example above for the rigorousness.

A message can be just a plain text (as the example above) or a mixed content message (like an email that supports both plain-text and HTML format and why not, it may even contain an attachment or two).

Let's analyze few cases!

Plain text with attachment


Here the header is between lines 1-4 and the body is between lines 6-28. Note that in these header I have fewer fields (eg. I skipped the MIME-Version and the Content-Transfer-Encoding). Some of them will be assumed by the email agent by default, some of them can't. For example if you miss the From field who could possibly guess from who comes the message? Likewise for the To field.

In this case our message contains a mixed content: a text/plain on the one hand and a image/png on the other hand. In such case we have to declare the content type otherwise the email agent will not know how to display the message content (I mean we would like to see an image instead of some strange characters, don't we?).

This is done by specifying the content type followed by other parameter called boundary which should have a unique value (that under no circumstances may be part of your image/body/message/whatever. If that string appears in the message the email agent will regard it as a boundary separator. Capisci?

[x] The section (entity) encapsulated within boundaries should be prefixed by the boundary opening tag and by the boundary closing tag. The opening boundary tag is prefixed by the "--" plus the boundary (unique) string and the closing starts like earlier but should also be suffixed with two dashes (two minus ASCII characters).

So right after the header we leave one line empty (CRLF, ie. ASCII 13+ASCII 10) and then insert the boundary separator followed immediately by the entity content type. After this declaration we should leave one line empty followed by our message. When the message is done we leave one line empty after it. If there are more other sections to add then we repeat these steps (see [x] mark above). If there is no other entity to include then we end by inserting the boundary closing tag (ie. exactly the same thing as opening boundary but we add two more "--" at the end).

In our case we have one more section (the image) so we start by leaving one line empty then repeating the steps from [x] mark up till here. Note that the image (like any other attachments should be converted to base-64 otherwise that entity isn't recognized or event the whole message might suffer!

As I've said, when there is no more data to insert we just close the multipart/mixed boundary opened right after the message header.

Note that we've used the same value for boundary (and that's not just a fad) !

Mixed message only (plain-text and HTML)


In this case we sent an email that contains just the following text: "A mixed message is like mixed artial marts."

Because we want it to be displayed both, in HTML format when the email browser can handle it and in plain-text format when don't, we actually create the message by inserting two different entities, each with its own format, although they both represent the same part of the message: the body. In such cases with have a entity with two (or more) alternative content types.

We declare the whole message as having the format "multipart/alternative" and then insert each alternative separated by the boundary.

Note that the same logic/steps as above is valid here, except that we deal with the same entity that has two or more alternative representations.

Mixed message with attachment


This is probably the message that will help you better the email message format because it contains both, an alternative representation of the same content plus an attachment.

Please note that the boundary used to mark the beginning and the end of the multiparted/mixed content differs from the boundary used for the multiparted/alternative content (eg. line 5 vs. line 8). The mixed boundary is used to separate one entity from the other. At line 7 we insert the body message, at line 38 we insert the attachment content. The alternative boundary on the other hand is used to separate alternative contents within the same entity (namely body message). For instance at line 10 we use it because right below that line we insert an alternative content type of the current entity (ie. body message in plain-text format). Then the same at line 19, when we insert the HTML format of the same body message. Nonetheless when we don't have any other alternative representation of that entity we just close the alternative boundary tag (see line 36).

When we don't have any other entity to insert in our message we insert (as the last line) the closing tag of the multiparted/mixed boundary. If for instance we would had one attachment more to insert then after the first attachment we would insert a new line then a new opening tag of multiparted/mixed boundary and the attachment section as the one between lines 38-51.

A working example of PHP send html mail with attachment is presented below:

@Edit: I should probably add that if you connect via telnet command at port 25 (SMTP) of your email server and type exactly a message like any of those presented above (+ end the message by pressing Ctrl+Z) then the server will eat-up the message, will parse it and if it's ok then it will deliver it as it would be written and sent via a normal email client. So you can turn telnet in a little email client. Actually I'm using telnet even for querying SQL databases or for getting/posting data to some HTML servers so, you see, telnet is kind of a swiss army knife.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
PHP send html mail with attachment

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
PHP send html mail with attachment

Latest posts by Eugen Mihailescu (see all)

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: