Send MIME messages in PHP (ii)

Source: Internet
Author: User
Tags base64 header mail mixed client
Mime "OK, how do I create MIME-compliant information?" "With the general description above, let's take a look at what the so-called MIME information is!"





The simplest MIME information


This information does not have any paragraph, namely, has no attachment. However, since it is a MIME message, it must have the necessary headers.





from:php@php.net


to: ' Alex (The Great) ' <alex@greece.net>


Subject:bucephalus


mime-version:1.0





Hello Alexander,





how ' s bucephalus doing?





there's nothing in it, it's just a simple one with a MIME header that conforms to RFC-822


information (text message). Note that if there is no


Specifies the Content-type header, then assume content-type:text/plain;charset= ' us-ascii '! Of course, it's a bit simple and complex


some of the following:





from: ' Alex (The Great) ' <alex@greece.net>


to:php@php.net


Subject:re:Bucephalus


mime-version:1.0


content-type:image/jpg;


name= ' buce.jpg '


content-transfer-encoding:base64


Content-description:take a look at him yourself





<.....base64 encoded JPG image of bucephalus...>





"Hi, but I want to send a Word document and a picture of my puppy in the same email ...


! "A user said!" If it's true


, the example above is too simple, and it does not have enough content to support the needs of enthusiasts and modern mail processing. In fact,


many mail client software can not even display the Description field!





This is the "multi-part information" We are facing.








Multi-part information (Multipart Messages)


This concept allows multiple items to be sent in a single message. For example, suppose Alexander wants to send php@php.net a
for his horse.

photos of the mail, also accompanied by a horse family Atlas and wonderful description! Such a simple requirement without the notion of a partial message is not


is satisfied. In this case, we create a package that uses the Content-type header to support different parts of the message so that the


recipient get pictures, family Atlas and wonderful description!




The
Content-type header now has a value of "multipart", which means that it is a complete message and that this header encapsulates only the


information. And it has a subtype of "mixed" (after all, pictures, family maps and 7bit text messages are different types, right?). )。





Let's take a look at the whole picture looks like:








from: ' Alex (The Great) ' <alex@greece.net>


to:php@php.net


Subject:re:Bucephalus


mime-version:1.0


content-type:multipart/mixed;


boundary= "xx-1234ded00099a";


Content-transfer-encoding:7bit





this are a MIME encoded message





--xx-1234ded00099a


Content-type:text/plain; Charset=us-ascii


Content-transfer-encoding:7bit





Hi PHP,





attached you'll find my horse, Bucephalus ', pedigree chart and photo.





Alex





--xx-1234ded00099a


content-type:image/jpg;


name= "Buce.jpg";


content-transfer-encoding:base64


content-description: "A photo of Bucephalus"





<.....base64 encoded JPG image of bucephalus...>





--xx-1234ded00099a


Content-type:application/octet-stream;


name= "Pedigree.doc"


content-transfer-encoding:base64


content-description: "Pedigree Chart of the Great horse"





<.....base64 encoded doc (pedigree.doc) bucephalus...>





--xx-1234ded00099a--





yo, it looks complicated, doesn't it? Anyway, let's go through the details:





If you notice the content-transfer-encoding in the MIME message header, "7bit". Because Content-type is
.

multipart/mixed, encoding should be one of the 7bit,8bit or binary, 7bit is a widely used format.


like such a piece of information contains a variety of information. How does a client program know the difference between a JPG picture, a document, and an ordinary text? You'll
.

notes that there is a boundary= "xx-1234ded00099a" parameter behind the Content-type. This value is used to separate the different
in the message

part. It is called a MIME boundary tag. The value of the boundary tag must be as unique as possible to avoid confusion when the message range is exceeded.


"Warning" message (translator: Refers to "This is a MIME encoded


message ") is there to allow non-MIME-compliant client programs


can show it to the user, otherwise they don't understand what a blank message means.


now, back to the boundary marker. If you look at this simple message, you will find the boundary tag (xx-1234ded00099a in each of the points


all appear, that is, a boundary tag is used between each part, however, each boundary marker starts with two connectors.


It's important to note that at the end of the last MIME segment, the boundary tag does not just start with those two edges,


also ends with both of them. This must not be forgotten, because it defines the scope of the message.


Let's take a look at the first two MIME segments:


The first paragraph is plain text information, so Content-type is text/plain and encoded 7bit (we can also omit it,


because it also defaults to this if it is not indicated.


the second is a JPEG image. The corresponding representation is content-type:image/jpg. Name= "Buce.jpg" (appearing in


Content-type, called a parameter, indicates the name of the file; it is the attachment that can be seen in the client program


's name. If you don't give a Name= "buce.jpg"


, the Description field (if given) will be displayed as the name of the attachment (however


and, in all client programs It is not a unified approach.


Note JPEG


pictures can be displayed in the mail, if the client program can display the inline attachment. Alternatively, you can
to the client

specify how you want to display attachments. For example, if there is a





content-disposition:attachment headers, JPEG images will be displayed as an attachment icon.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.