Use PHP to send MIME mail (4) _ PHP Tutorial

Source: Internet
Author: User
Use PHP to send MIME mail (4 ). Core: The method divides the generation of MIME information headers, the generation of MIME field headers, and the generation of final mail messages into several modules. The implementation of the method is directly from the MIME base we have encountered and the core: method
We divide the generation of MIME information headers, the generation of MIME field headers, and the generation of final mail messages into several modules. The implementation of the method comes directly from the MIME base we met earlier.


Function attach ($ data, $ description = "", $ contenttype = OCTET, $ encoding = BASE64, $ disp = ){
If (empty ($ data ))
Return 0;
If (trim ($ contenttype) =)
$ Contenttype = OCTET;
If (trim ($ encoding) =)
$ Encoding = BASE64;
If ($ encoding = BIT7)
$ Emsg = $ data;
Elseif ($ encoding = QP)
$ Emsg =$ $ this-> qp_func ($ data );
Elseif ($ encoding = BASE64 ){
If (! $ This-> base64_func) # check whether there are user-defined functions
$ Emsg = base64_encode ($ data );
Else
$ Emsg =$ $ this-> base64_func ($ data );
}
$ Emsg = chunk_split ($ emsg );
// Check whether the content-type is text/plain. If no charset is specified, append the default CHARSET
If (preg_match ("! ^ ". TEXT ."! I ", $ contenttype )&&! Preg_match ("!; Charset =! I ", $ contenttype ))
$ Contenttype. = "; charset =". CHARSET;
$ Msg = sprintf ("Content-Type: % sContent-Transfer-Encoding: % s ",
$ Contenttype. CRLF,
$ Encoding. CRLF,
($ Description) & (BODY! = $ Description ))? "Content-Description: $ description". CRLF :""),
($ Disp? "Content-Disposition: $ disp". CRLF :""),
CRLF. $ emsg. CRLF );
BODY = $ description? $ This-> mimeparts [0] = $ msg: $ this-> mimeparts [] = $ msg;
Return sizeof ($ this-> mimeparts );
}

?>

Let's take a closer look at this method (for most other methods ):

Parameters used in this method include:
Attached actual data ($ data)
Data Description corresponding to the Content-description header ($ Description)
The Content-Type value ($ contentype) of the data that will be used in the content-type header)
Encoding value used in Content-Transfer-encoding ($ Encoding)
The layout value used in the Content-Disposition header $ disp, which can be INLINE or ATTACH. Both are constants.
Values such as BASE64 and TEXT are defined as constants in the attached. def file.
Use the $ encoding value to determine the encoding method used to encode the data. Valid values include BIT7 (or 7bit), QP, and BASE64.
This function also checks whether the user wants to use his/her own BASE64 or QP function. When writing this article
Only BIT7 and BASE64 are implemented. However, you can pass your own quoted-printable function.
$ Qp_func class variable discussed.
After encoding, you will notice that chunk_split () is used for encoding information (). This function divides strings according to the optional length
Segment to a small segment. Because the length is not specified, the default length is 76. This is a perfect combination of mail processing habits.
Then, if the $ contenttype parameter contains text/plain, the value of "charset =" must be given. Its default value is defined.
In the constant CHARSET, the value is us-ascii. Note that a semicolon (;) must be included between the header and the parameter when the parameter value is passed in the header (;).
For example, Content-Type: text/plain; charset = us-ascii
If the values of other MIME field headers are passed to this method, these field headers are created. After all, we do not want to have a Content-Description header without a Description. After creating these headers, we append the encoded data information. (Check the method.
Sprintf () statement ).
Similarly, note that we use a special description field called BODY (and a constant. This is what we use in class implementation.
If the description field is the same as the BODY field, we assign it to the first element in the $ mimeheaders array. Read this file several times.

Attach () returns the current size of the $ mimeparts array, which is used in reference to the call script. In this way, you can know which index exists in an attachment "X" (the actual returned value is 1 smaller than the index in the array)
Note that all headers must end with a CRLF () sequence.
Next, let's take a look at the fattach () method. fattach () is similar to attach (), but it uses a file name as its first parameter (as attach () $ data replacement ). This method is only an encapsulation, so that the caller can use a file to call fattach. Fattach () then reads the file and calls attach () to append data. This method returns 0 upon failure. you can find the explanation in the $ errstr variable or return the index number of the file attachment in the $ mimeparts array when the explanation is successful.

The MIME header is generated, the MIME header is generated, and the final Mail message is generated into several modules. The implementation of the method is directly based on the MIME mentioned above...

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.