Send MIME messages in PHP (v)

Source: Internet
Author: User
Tags empty header mail md5 md5 hash rand range unique id
MIME we will examine the next method is that Build_message (), which occupies the bulk of the whole work, but it is through a
Gen_email () method to invoke the. Please note that build_message () is a private method.

<?php

function Build_message () {

$msg = "";
$boundary = ' PM '. Chr (rand (65, 91)). ' ------'. MD5 (Uniqid (rand ()); # Boundary identification
$nparts = sizeof ($this->mimeparts);

Scenario 1: There is an attachment list, so the MIME header must be multipart/mixed
if (Is_array ($this->mimeparts) && ($nparts > 1)) {
$c _ver = "mime-version:1.0". CRLF;
$c _type = ' Content-type:
Multipart/mixed: '. CRLF. "Boundary=" $boundary "". CRLF;
$c _enc = "content-transfer-encoding:". BIT7. CRLF;
$c _desc = $c _desc? " Content-description: $c _desc ". CRLF: "";
$warning = CRLF. WARNING. CRLF. CRLF;

If there is a mimie segment, the message body also becomes an attachment
if (!empty ($this->body)) {
$this->attach ($this->body, Body, TEXT, BIT7);
}

Create each MIME segment of a message now
for ($i =0; $i < $nparts; $i + +) {
if (!empty ($this->mimeparts[$i]))
$msg. = CRLF. '--$boundary. CRLF. $this->mimeparts[$i]. CRLF;
}
$msg. = '-$boundary. '-'. CRLF;
$msg = $c _ver. $c _type. $c _enc. $c _desc. $warning $msg;
} else {
if (!empty ($this->body)) $msg. = $this->body. CRLF. CRLF;
}
return $msg;
}

?>

This method is a bit Sang, simple and complex. It depends on how you look at yourself.

We read in front (on MIME basis) that each MIME segment has a boundary tag that has a unique ID. Side
The boundary mark is used in:
In the MIME header, which indicates where the attachment must be divided
MIME segments, which are actually used to divide the bounds of the attachment at the front and back of each paragraph. (Recall that Alexander has a picture
and Atlas of the Mail! )
(Remember: The last boundary mark ends with two connectors (-) to indicate the end of the range).
The $boundary contains a boundary tag, and it is unique by a random number and then MD5 hash generated. In addition, we
Give $boundary a "PM?" The prefix, here "?" is a random letter. An example of a $boundary is
"PMK------2345ee5de0052eba4daf47287953d37e" (PM represents PHP MIME, so you can change it to your possible
Initial value! )

In the process of generating MIME headers, we must consider two situations. These situations affect the original message body of the message ($body in the constructor
In which way is regarded and the special expression of the MIME information header. Situation 1 is the reason to write this article, and you will see: you can
There are many attachments to be included! In this case, note that as part of the message is placed a warning string "this
is a MIME
Encoding message ". Therefore, the real message body itself must be added to the information in the form of an attachment! The text of the message is usually an attachment
The first attachment in the list, in our case, is $mimeparts. That's exactly why we're taking a
The reason $mimeparts the index so that the first index (0) can be used for the message text portion. The message body must be encoded in 7bit
Line Attach.
<?php

if (!empty ($this->body)) {
$this->attach ($this->body, Body, TEXT, BIT7);
}

?>

A small piece of code above completes the work of attaching the text portion of the message as a MIME attachment. Note that we use the ' body ' constants to
Instructs attach () where to add the attachment.

The second scenario is when the attachment does not exist, in which case, if the message text is provided, it will be included in the local variable $msg
, in which case the MIME header is not required. (However, in this case we should also only put the mime-version head
Specify----go back and look at the simplest MIME information for the previous demo. )

MIME Information Header (Mime-version,content-type,
Wait a minute. ) is created when there is an attachment. In order to use the MIME message header
The body of the message is created, first the MIME header is created. Each valid MIME segment is then processed repeatedly through the $mimeheaders array.
This is where the boundary markings are actually used. Based on the consistency of the rules, a MIME segment is prefixed with two connectors ('-
'. $BOUNDARY. Crlf) and after the last MIME segment, append two connectors after the boundary identification to indicate the end of the message range.
The complete information in the variable $msg is returned as the value of this method.
Next method, Get_email () completes the generation of MIME messages through the Build_message () method. Because Build_message () is a
Internal method, Get_email () creates an RFC 822 header and appends MIME information after the Build_message () is called.

<?php

function Gen_email ($force =false) {

if (!empty ($this->email) &&! $force) return $this->email; //
Saves processing
$email = "";
if (Empty ($this->subject)) $this->subject = Nosubject;
if (!empty ($this->from)) $email. = ' from: '. $this->from. CRLF;
if (!empty ($this->headers)) $email. = $this->headers. CRLF;
$email. = $this->build_message ();
$this->email = $email;
return $this->email;
}

?>

For an instance of our class, the members of the class $email have the entire message information generated. To avoid information being unnecessarily heavy
New build, this method continues to create the header and calls Build_message () only if $mail is empty. However, you can adjust
Use Gen_email () to force a reprocessing. (If the "to" information is changed or a new attachment is added, the caller appears to want to do so).

Gen_email () Creates a more familiar from header. In addition, if you do not specify a theme, it sets the theme to the default value (no
Subject).
We will not be to and subject until the back
is preserved. This method returns the complete message information, which ends up creating a mime letter
The task of the rest.


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.