Home > Backend Development > PHP Tutorial > Sending MIME emails using PHP (4)_PHP Tutorial

Sending MIME emails using PHP (4)_PHP Tutorial

WBOY
Release: 2016-07-13 17:31:12
Original
903 people have browsed it

Core: Method
We divide the generation of MIME headers, the generation of MIME segment headers and the generation of the final email message into several modules. The implementation of the method comes directly from the MIME basics we encountered earlier.

(as the current mainstream development language)

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 is a user-defined function
$emsg = base64_encode($data);
else
$emsg = $$this->base64_func($data);
}
$emsg = chunk_split($emsg);
//Check if the 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 (as with most of the others method will do the same):

The parameters used by this method are:
The actual data attached ($data)
The data description corresponding to the Content-Description header ($description)
The data content-type value ($contenttype) used in the Content-Type header
The encoding value ($encoding) used in Content-Transfer-Encoding
The layout used in the Content-Disposition header $disp The value can be INLINE or ATTACH, both of which are constants
such as BASE64, TEXT, etc., are defined as constants in the attached .def file
and use the $encoding value to determine which one needs to be used. Encode the data in one encoding. Valid values ​​are BIT7 (or 7bit), QP or BASE64.
This function also checks whether the user wants to use his/her own BASE64 or QP function at the time of writing this article. At this time, only BIT7 and BASE64 are implemented in our class, however, you can pass your own quoted-printable function to use, through the $qp_func class variable discussed earlier
. After the encoding process, you will notice that chunk_split() is used on the encoded information. This function splits the string
into smaller pieces according to the optional length. Since we didn't specify the length, the default length is 76. This is very suitable for mail processing habits.
Next, if the $contenttype parameter contains text/plain, the value of the "charset=" parameter must be given. Its default value is defined
in the constant CHARSET with the value us-ascii. Note that when headers are passed by 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 headers are passed to this method, these headers are created. After all we don't want to have a Content-Description header without a description. After creating these headers, we append the encoded data portion of the information. (Check the sprintf() statement in method
).
Also, note that we use a special description field called BODY (again a constant). This is what we use in our class implementation.
If the description field is the same as BODY, we assign it to the first element in the $mimeheaders array. Please read this several times.

attach() returns the current size of the $mimeparts array, used in references to the calling script. In this way, you can know which index an attachment "X" exists in (the actual returned value is 1 less than the index in the array)
Note that all headers must use a CRLF ( ) sequence ends.
Next, let’s look at the fattach() method. fattach() is similar to attach(), but it uses a file name as its first parameter (as a replacement for $data in attach()). This method is just a wrapper so that the caller can call fattach with a file. fattach() then reads the file, and then calls attach() to append the data. This method returns 0 on failure, the explanation of which can be found in the $errstr variable, or on success, the index number of the file attachment in the $mimeparts array.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/509092.htmlTechArticleCore: Method We divide the generation of MIME information headers, the generation of MIME segment headers and the generation of the final email message into Several modules. The implementation of the method is directly derived from the MIME basis we encountered earlier...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template