PHP implementation message sent with attachment

Source: Internet
Author: User
Tags sendfile

  This article mainly introduces the example of PHP to send mail, and can send the message with the attachment, we refer to use bar  

emailclass.php   Code is as follows: <?  class Cmailfile {  var $subject;  var $addr _to;  var $text _body;&n Bsp 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;  $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)) {  $f D = 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 = "F" Rom: $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 &nbSp $message--Letter content   $filename--attachment file name   $downfilename--download filename   $mimetype--MIME type   */ ?> &NB Sp     Demo example   code is as follows: <?php  require_once (' emailclass.php ');  //Send email  //topic   $subject = "Test send email";  //Recipient   $sendto = ' ';  //inventor   $replyto = ' ';  //content $message = "Test send email content";  //Accessories   $filename = ' test.jpg ';  /Accessories   $mimetype = "Image/jpeg";   $mailfile = 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: and provide relevant evidence. A staff member will contact you within 5 working days.