PHP implementation of send with Attachment message class instance, attachment instance _php tutorial

Source: Internet
Author: User
Tags fread sendfile

PHP implementation of send with Attachment message class instance, attachment instance


This paper describes the implementation of PHP to send the message class with attachments, is a very useful function. Share to everyone for your reference. Here's how:

The emailclass.php class file is as follows:

<?    Class Cmailfile {var $subject;    var $addr _to;    var $text _body;    var $text _encoded;    var $mime _headers;    var $mime _boundary = "--==================_846811060==_";       var $smtp _headers; function Cmailfile ($subject, $to, $from, $msg, $filename, $downfilename, $mimetype = "Application/octet-stream", $mime _         filename = false) {$this->subject = $subject;         $this->addr_to = $to;     $this->smtp_headers = $this->write_smtpheaders ($from);     $this->text_body = $this->write_body ($msg);     $this->text_encoded = $this->attach_file ($filename, $downfilename, $mimetype, $mime _filename);   $this->mime_headers = $this->write_mimeheaders ($filename, $mime _filename); } function Attach_file ($filename, $downfilename, $mimetype, $mime _filename) {$encoded = $this->encode_file ($filen     AME);     if ($mime _filename) $filename = $mime _filename; $out = "--". $this->mime_boundary.     "\ n"; $out = $out. "Content-type:". $mimetype. ";     Name=\ "$filename \"; \ n "; $out = $out.     "Content-transfer-encoding:base64\n"; $out = $out. "Content-disposition:attachment;     Filename=\ "$downfilename \" \ n "; $out = $out. $encoded.     "\ n"; $out = $out. "--" . $this->mime_boundary. "--" .     "\ n";   return $out;        } function Encode_file ($sourcefile) {if (is_readable ($sourcefile)) {$fd = fopen ($sourcefile, "R");        $contents = Fread ($FD, FileSize ($sourcefile));        $encoded = Chunk_split (Base64_encode ($contents));      Fclose ($FD);    } return $encoded;      } function Sendfile () {$headers = $this->smtp_headers. $this->mime_headers; $message = $this->text_body.      $this->text_encoded;    Mail ($this->addr_to, $this->subject, $message, $headers); } function Write_body ($msgtext) {$out = "--". $this->mime_boundary.      "\ n"; $out = $out. "Content-type:text/plain;      Charset=\ "us-ascii\" \ n "; $out = $out. $msgtext.      "\ n"; RetUrn $out;      } function Write_mimeheaders ($filename, $mime _filename) {if ($mime _filename) $filename = $mime _filename;      $out = "mime-version:1.0\n"; $out = $out.      "Content-type:multipart/mixed;"; $out = $out.      "Boundary=\" $this->mime_boundary\ "\ n"; $out = $out.      "Content-transfer-encoding:7bit\n"; $out = $out.      "x-attachments: $filename; \ n";    return $out;      } function Write_smtpheaders ($addr _from) {$out = "from: $addr _from\n"; $out = $out.      "Reply-to: $addr _from\n"; $out = $out.      "X-mailer:php3\n"; $out = $out.      "X-sender: $addr _from\n";    return $out; }}/* Usage-for example: MimeType for "image/gif" $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $mimetype   );    $mailfile->sendfile (); $subject--Subject $sendto--recipient address $replyto--reply to address $message--Letter content $filename--attachment file name $downfilename--Download file name $  MimeType--MIME type */?>

The demo sample file is as follows:

<?php   require_once (' emailclass.php ');    Send mail      /topic   $subject = "Test send email";    Recipient   $sendto = ' abc@163.com ';      Sender   $replyto = ' cdf@163.com ';      Contents   $message = "Test send email content";      Attachment   $filename = ' test.jpg ';      Attachment category   $mimetype = "Image/jpeg";    $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $excelname, $mimetype);    $mailfile->sendfile ();?>

It is believed that this article has some reference value to the study of PHP program design.


How to implement PHP to send mail attachments? Need source code!

Direct hair is no good, use Phpmailer

How to implement PHP to send mail attachments? Need source code!

/*
$to goals
$subject topics
$message body
$from from
$content _type Types
$attache attachment, the file name is placed in the array
*/
$email = $_post[' email '];
$email = Trim ("$email");

$message = "Hello";

function Mail2 ($to, $subject, $message, $from, $content _type, $attache = "") {
if (!empty ($from)) $head = "From: $from \ n";
if (Empty ($content _type)) $content _type = "Text/plain";

if (Is_array ($attache)) {
$boundary = "= = =". MD5 (Uniqid ("")). " ===";
$head. = "mime-version:1.0\ncontent-type:multipart/mixed; Boundary=\ "";
$head. = "$boundary \" \n\nthis is a multi-part message in MIME format.\n\n ";
$head. = "--$boundary \ n";
$head. = "Content-type: $content _type\n";
$head. = "\n$message\n\n";

while (list ($key, $val) = each ($attache)) {
$FD = fopen ("$val", "R") or Die ("Unable to open file $val");
$contents = Chunk_split (Base64_encode (Fread ($FD, FileSize ("$val")));
Fclose ($FD);
...... Remaining full text >>

http://www.bkjia.com/PHPjc/882906.html www.bkjia.com true http://www.bkjia.com/PHPjc/882906.html techarticle PHP Implementation of the Send with attachment message class instance, attachment instance This article describes the PHP implementation of the Send with Attachment message class method, is a very useful function. Share to everyone ...

  • 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.