This article mainly introduces the example of sending emails in php and can send emails with attachments. For details, refer to emailclass. php.
The Code 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 = $;
$ 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 );
} Www.php.net
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, if mimetype is "image/gif"
$ Mailfile = new CMailFile ($ subject, $ sendto, $ replyto, $ message, $ filename, $ mimetype );
$ Mailfile-> sendfile ();
$ Subject -- Topic
$ Sendto -- recipient address
$ Replyto -- reply address
$ Message -- mail content
$ Filename -- attachment file name
$ Downfilename -- name of the file in the lower region
$ Mimetype -- mime type
*/
?>
Demo
The Code is as follows:
Require_once ('emailclass. php ');
// Send an email
// Subject
$ Subject = "test send email ";
// Recipient
$ Sendto = 'abc @ 163.com ';
// Producer
$ Replyto = 'cdf @ 163.com ';
// Content www.php.net
$ Message = "test send email content ";
// Attachment
$ Filename = 'test.jpg ';
// Annex
$ Mimetype = "image/jpeg ";
$ Mailfile = new CMailFile ($ subject, $ sendto, $ replyto, $ message, $ filename, $ excelname, $ mimetype );
$ Mailfile-> sendfile ();
?>