Example: Send a message in MIME format with PHP3 (you can send an attachment. Oh)

Source: Internet
Author: User
Tags format array empty mail return string
MIME Here is an example:

/*
* Class Mime_mail
* Original implementation by Sascha Schumann <sascha@schumann.cx>
* Modified by Tobias Ratschiller <tobias@dnet.it>:
*-General Code Clean-up
*-Separate Body-and from-property
*-killed some mostly un-necessary stuff
*/

Class Mime_mail
{
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

/*
* void Mime_mail ()
* Class constructor
*/
function Mime_mail ()
{
$this->parts = Array ();
$this->to = "";
$this->from = "";
$this->subject = "";
$this->body = "";
$this->headers = "";
}

/*
* void Add_attachment (String message, [string name], [string CType])
* Add an attachment to the mail object
*/
function Add_attachment ($message, $name = "", $ctype =
"Application/octet-stream")
{
$this->parts[] = Array (
"CType" => $ctype,
"Message" => $message,
"Encode" => $encode,
"Name" => $name
);
}

/*
* void Build_message (array part=
* Build message parts of a multipart mail
*/
function Build_message ($part)
{
$message = $part ["message"];
$message = Chunk_split (Base64_encode ($message));
$encoding = "base64";
Return "Content-type:". $part ["CType"].
($part ["name]"?; Name=\ "". $part ["name]." \"":"").
"\ncontent-transfer-encoding: $encoding".
"\ncontent-disposition:inline".
($part ["name]"?; Filename=\ "". $part ["name]." \"":"").
"\n\n$message\n";
}

/*
* void Build_multipart ()
* Build a multipart mail
*/
function Build_multipart ()
{
$boundary = "B". MD5 (Uniqid (Time ()));
$multipart = "content-type:multipart/mixed; boundary = \ "$boundary \" \n\nthis
is a MIME encoded message.\n\n--$boundary ";

for ($i = sizeof ($this->parts)-1; $i >= 0; $i--)
{
$multipart. = "\ n". $this->build_message ($this->parts[$i]). " --$boundary ";
}
return $multipart. = "--\n";
}

/*
* Void Send ()
* Send the Mail (last class-function to be called)
*/
function Send ()
{
$mime = "";
if (!empty ($this->from))
$mime. = "From:" $this->from. " \ n ";
if (!empty ($this->headers))
$mime. = $this->headers. " \ n ";

if (!empty ($this->body))
$this->add_attachment ($this->body, "", "Text/plain");
$mime. = "mime-version:1.0\n". $this->build_multipart ();
Mail ($this->to, $this->subject, "", $mime);
}
}; End of class

/*
* Example Usage
*

$attachment = Fread (fopen ("Test.jpg", "R"), FileSize ("test.jpg"));

$mail = new Mime_mail ();
$mail->from = "foo@bar.com";
$mail->headers = "errors-to:foo@bar.com";
$mail->to = "bar@foo.com";
$mail->subject = "testing ...";
$mail->body = "This is just a test.";
$mail->add_attachment ("$attachment", "test.jpg", "image/jpeg");
$mail->send ();

*/
?>





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.