- /**
- * Send an email 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 = $;
- $ 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, 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
- */
- ?>
2. demo. php
- Require_once ('emailclass. php ');
-
- // Send an email
-
- // Subject
- $ Subject = "test send email ";
-
- // Recipient
- $ Sendto = 'ABC @ 163.com ';
-
- // Producer
- $ Replyto = 'cdf @ 163.com ';
-
- // Content
- $ 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 ();
- ?>
>>> Articles you may be interested in: php socket uses smtp to send emails with attachments. example of IMAP application in Php (sending and receiving emails, deleting emails, and downloading attachments) PHPMailer example of sending an email with attachments PHPMailer solution to garbled Chinese attachments |