Send MIME messages in PHP (c)

Source: Internet
Author: User
Tags array comments constructor contains empty header mail variables
MIME MIME class


with these basics, let's create and implement a MIME message class in PHP. In our PHP library function, we already have the code

The necessary tools for
.





MIME classes must be able to:





Add Accessories


encodes the attached data for each individual request


Create MIME segment/Header


generates a complete message containing a MIME segment/header


returns the entire message as a string


with a local mail handler (or choose to invoke an SMTP mail handler)


This class is called Mime_mail. We will discuss the methods of class and build bridges between the theoretical and practical gaps. (Reading suggestion: Luis


Argerich PHP object-oriented programming: A way to develop a large PHP project. Most of the comments have been removed for easy reading. A


some methods and class member variables are only used for internal processing and have been pointed out in the following comments (also in the original class file


pointed out).





<?php





class Mime_mail {


//Public:


var $to;


var $from;


var $subject;


var $body;


var $headers = "";


var $errstr = "";





var $base 64_func= '; If you do not specify the Base64 function to use PHP


var $qp _func = '; This is an empty





var $mailer = ""; Set it as a valid message object name





?>





here are some common variables (i.e., variables that can be manipulated directly in the script). Most of these variables are from the
.

Ming. The $headers contains the optional header information that you want to send to the message handler. $errstr


is a variable
containing a readable error string
The amount of
that can be used in the calling script.





$base 64_func and $qp_func are "function processors" that users can customize. By default, they are set to an empty string. For $base64_func, an empty string means we'll use PHP's built-in Base64_encode () function ... Yes Beautiful, isn't it! )。


Quoted printable can be processed through $qp_func. There is no built-in quoted-printable in PHP


encoding function (however, install


with IMAP, you can use Imap_qprint (). In this article we will not discuss the Quoted_printable method.





<?php





//private:


var $mimeparts = array ();





?>




The
$mimeparts is an internal array that contains the independent MIME segments in the message information. Do not manipulate it and other private methods/variables outside of this class (or derived class).





<?php





//Constructor


function Mime_mail ($from = "", $to = "", $subject = "",


$body = "", $headers = "") {


$this->to = $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 position 0


return;


}





?>





we have the constructor of the object, which uses the "from" and "to" mail addresses, subject and message body and headers as parameters. For the
of the mail body

, you can give a normal email that you will probably enter. The last parameter is an optional (user-defined) header. For example, X-mailer:


mymailer_1.0. Note that $headers can be an array that contains the different headers that will be sent to the mail-sending program, or just some


the special head of the container. You cannot send from:
in the $headers parameter

Head, if it is found, this part will be automatically removed. You can be like the next


faces use multiple headers: Array ("x-mailer:mymailer_1.0",


"x-organization:phpbuilder.com").




The
$mimeparts is created with an empty item (index 0), which we'll see 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.