Send MIME messages in PHP (d)

Source: Internet
Author: User
Tags array base64 constant contains header mail sprintf trim
MIME Core: Method


we divide the generation of MIME headers, the generation of MIME segment headers, and the generation of final mail messages into several modules. The implementation of the method comes directly from the MIME Foundation we encountered earlier.





<?php





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 for user-defined functions


$emsg = Base64_encode ($data);


Else


$emsg = $ $this->base64_func ($data);


}


$emsg = Chunk_split ($emsg);


Check if Content-type is Text/plain and if CharSet is not 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%s%s%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 (and for most other methods):





the parameters used in this method are:

Actual data ($data) attached to



Data Description ($description) corresponding to the content-description header


will use the data Content-type value ($contentype) in the Content-type header


encoded value ($encoding) used in content-transfer-encoding


the layout value used in the Content-disposition header $disp, which can be inline or attach, and two are constant


such as base64,text values, and so on, are defined as constants in the attached. def file.


uses the $encoding value to determine which encoding is needed to encode the data. Valid values are BIT7 (or 7bit), QP or BASE64.


This function also checks whether the user wants to use his or her own BASE64 or QP function. In writing this article,
in our class

only BIT7 and BASE64 are implemented, however, you can pass on your own quoted-printable


function to use, through the front


discusses the $qp_func class variables.


after coding, you will notice that the encoded information is used Chunk_split (). This function
the string according to the optional length

cut into small pieces. Because we do not point to length, the default length uses 76. This is very attached to the habit of mail processing.


then, if the $contenttype parameter contains Text/plain, then "charset=" must be given

The value of the
parameter. Its default value is defined


in constant charset, the value is us-ascii. Note that when the head is passed with the parameter value, there must be a semicolon (;) between the header and the parameter.


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


if the respective values of other MIME segment headers are passed to this method, the segment headers are created. After all, we don't want to have a
that isn't described.

Content-description head. After these headers are created, we append the encoded data portion of the information. (Check the method

The sprintf () statement in
).


Similarly, note that we use a special description field called Body (which is also a constant). This is what we use in the class implementation.


If the Description field is the same as the body, we assign it to the first element in the $mimeheaders array. Please read it a few more times for this.





Attach () returns the current size of the $mimeparts array, used in a reference to the calling script. By this method you can know a


"X" in which index (the actual returned value is smaller than the index in the array 1)


Note that all heads must be made of a CRLF (


) sequence ends.


then, let's look at the Fattach () method, Fattach () is similar to attach (), but it uses a filename as its first

The
parameter (as a replacement for $data in Attach ()). This method is only an encapsulation so that callers can invoke Fattach with a single file.


Fattach () then reads the file and then calls attach () to append the data. This method returns 0 in the case of a failure and can be $errstr


variable


to find an explanation or, when successful, returns the index number of the file attachment in the $mimeparts array.





We have now developed the ability to append data, encode them, and place separate MIME segments in a private array. We need to finish
.

's work is:





complete the various segments of the MIME


creates a message header that contains a MIME header, the message's original header (such as To:,from:, and so on) and includes any user-defined


's head.


appends the full MIME segment to the header, so that a complete mail package is generated.














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.