- /**
- * Send a message with an attachment
- * by bbs.it-home.org
- */
- 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;
- $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 to "Image/gif"
- $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $mimetype);
- $mailfile->sendfile ();
- $subject--Themes
- $sendto--The address of the person receiving the letter
- $replyto--Reply to address
- $message--contents of the letter
- $filename--Attachment file name
- $downfilename--The file name of the download
- $mimetype--MIME type
- */
- ?>
Copy Code2, demo example demo.php
- Require_once (' emailclass.php ');
- Send mail
- Main topic
- $subject = "Test send email";
- Recipient
- $sendto = ' abc@163.com ';
- Sender
- $replyto = ' cdf@163.com ';
- Content
- $message = "Test send email content";
- Attachment
- $filename = ' test.jpg ';
- Annex Category
- $mimetype = "Image/jpeg";
- $mailfile = new Cmailfile ($subject, $sendto, $replyto, $message, $filename, $excelname, $mimetype);
- $mailfile->sendfile ();
- ?>
Copy Code>>> you may be interested in the article: PHP socket using SMTP to send messages with attachments IMAP application example in PHP (send and receive mail, delete mail, attachment download) Phpmailer Send an example with an attachment message phpmailer send a message Chinese attachment name garbled solution |