PHP Send with Attachment Mail sample

Source: Internet
Author: User
Tags php send mail sendfile

 php send mail, on the internet will be a lot of related articles, and this article is for you to send a message with attachments, do not understand the friend can refer to the following

emailclass.php    Code 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 ($filename);  if ($mime _filename) $filename = $mime _filename;&nbsP $out = "--". $this->mime_boundary. "n";  $out = $out. "Content-type:". $mimetype. "; Name= "$filename" n ";  $out = $out. "content-transfer-encoding:base64n";  $out = $out. "Content-disposition:attachment; Filename= "$downfilename" nn ";  $out = $out. $encoded. "n";  $out = $out. "--" . $this->mime_boundary. "--" . "N";  return $out; }    function encode_file ($sourcefile) {  if (is_readable ($sourcefile)) {& nbsp $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" nn ";  $out = $out. $msgtext. "N";  return $out; }    function write_mimeheaders ($filename, $mime _filename) {  if ($mime _ FileName) $filename = $mime _filename;  $out = "mime-version:1.0n";  $out = $out. "Content-type:multipart/mixed;";   $out = $out. "Boundary=" $this->mime_boundary "n";  $out = $out. "Content-transfer-encoding:7bitn";  $out = $out. "X-attachments: $filename nn";  return $out; }    function write_smtpheaders ($addr _from) {  $ out = "From: $addr _fromn";  $out = $out. "Reply-to: $addr _fromn";  $out = $out. "X-mailer:php3n";  $out = $out. "X-sender: $addr _fromn";  return $out; } }   /* Usage-for example: MimeType to "image/gif"   $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $mimetype);  $mailfile->sendfile ();    $subject-- Theme   $sendto--delivery address   $replyto--reply address   $message--Letter content   $filename--attachment file name   $downfilename--Next Download filename   $mimetype--MIME type   */ ?>    demo  copy Code as follows: <?php  require_once (' Emai lclass.php ');   //email    //topic   $subject = "Test send email";   //recipient   $sendto = ' abc@163.com ';   //sender   $replyto = ' cdf@163.com ';   //content   $message = ' Test send email con Tent ";   //Accessories   $filename = ' test.jpg ';   //Annex type   $mimetype =" Image/jpeg ";    $m Ailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $excelname, $mimetype);  $mailfile-> Sendfile (); ?> 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.