Use PHP to send MIME mail (3)

Source: Internet
Author: User
Author: KarticKrishnamurthy translator: limodouMIME class after these basics, let's use php (as the mainstream development language) to create and implement a MIME mail class. In our php library functions (as the mainstream development language), we already have tools necessary for encoding. MIME must be SyntaxHigh author: Kartic Krishnamurthy translator: limodou

MIME class
With these foundations, let's use php (as the mainstream development language) to create and implement a MIME mail class. In our php library functions (as the mainstream development language), we already have tools necessary for encoding.

The MIME class must be:

Add Attachment
For each independent request, create a MIME Segment/header to encode the attached data to generate a complete message containing the MIME Segment/header. send the entire message as a string and use a local email handler (or choose to call it) an SMTP mail processing program)
This class is called MIME_mail. We will discuss class methods and build bridges between theory and practice. (Read suggestion: Luis Argerich's php (as the mainstream development language) object-oriented programming: the method for developing large php (as the mainstream development language) projects ). Most comments have been removed to facilitate reading. Some methods and class member variables are only used for internal processing and have been pointed out in the following annotations (also in the initial class file ).


Class MIME_mail {
// Public:
Var $;
Var $ from;
Var $ subject;
Var $ body;
Var $ headers = "";
Var $ errstr = "";

Var $ base64_func =; // If php (as the mainstream development language) is not specified, use the base64 function
Var $ qp_func =; // empty at this time

Var $ mailer = ""; // set it as the name of a valid email object

?>

There are some variables for public Processing (that is, variables that can be directly manipulated in the script ). Most of these variables are self-explanatory. $ Headers contains optional header information that you want to send to the Mail handler. $ Errstr is a variable that contains a readable error string. it can be used in call scripts.

$ Base64_func and $ qp_func are "function processors" that you can customize. By default, they are set as empty strings. For $ base64_func, an empty string means that we will use the base64_encode () function built in php (as the mainstream development language)... (yes! Beautiful, isn't it !).
Quoted Printable can be processed through $ qp_func. There is no built-in quoted-printable encoding function in php (as the mainstream development language) (however, install
If imap is enabled, imap_qprint () can be used ()). In this article, we will not discuss the quoted_printable method.


// Private:
Var $ mimeparts = array ();

?>

$ Mimeparts is an internal array that contains separate MIME-compliant segments in the mail information. Do not manipulate this class (or derived class) and other private methods/variables.


// Constructor
Function MIME_mail ($ from = "", $ to = "", $ subject = "", $ body = "", $ headers = ""){
$ This-> to = $;
$ This-> from = $ from;
$ This-> subject = $ subject;
$ This-> body = $ body;
If (is_array ($ headers )){
If (sizeof ($ headers)> 1)
$ Headers = join (CRLF, $ headers );
Else
$ Headers = $ headers [0];
}
If ($ from ){
$ Headers = preg_replace ("! (From :?. +? []?)! I ", $ headers );
}
$ This-> headers = chop ($ headers );
$ This-> mimeparts [] = ""; // add the position 0.
Return;
}

?>

We have an object constructor that uses the "from" and "to" email addresses, subject, body, and header as parameters. For the body of the email, you can provide the normal email you may enter. The last parameter is an optional (custom) header. For example, X-Mailer:
MyMailer_1.0. Note that $ headers can be an array containing different headers to be sent to the mail sender program, or a container with a special header. You cannot send the From: header in the $ headers parameter. if it is found, this part is automatically removed. You can use multiple headers: array ("X-Mailer: MYMailer_1.0", "X-Organization: php (as the mainstream development language) Builder.com ").

$ Mimeparts is created with an empty item (index 0). we will see this usage later.

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.