Class for sending attachments in php

Source: Internet
Author: User
Tags sendfile
Class for sending attachments in php

  1. /**
  2. * Send an email with an attachment
  3. * By bbs.it-home.org
  4. */
  5. Class CMailFile {
  6. Var $ subject;
  7. Var $ addr_to;
  8. Var $ text_body;
  9. Var $ text_encoded;
  10. Var $ mime_headers;
  11. Var $ mime_boundary = "-- =========================_ 846811060 == _";
  12. Var $ smtp_headers;
  13. Function CMailFile ($ subject, $ to, $ from, $ msg, $ filename, $ downfilename, $ mimetype = "application/octet-stream", $ mime_filename = false ){
  14. $ This-> subject = $ subject;
  15. $ This-> addr_to = $;
  16. $ This-> smtp_headers = $ this-> write_smtpheaders ($ from );
  17. $ This-> text_body = $ this-> write_body ($ msg );
  18. $ This-> text_encoded = $ this-> attach_file ($ filename, $ downfilename, $ mimetype, $ mime_filename );
  19. $ This-> mime_headers = $ this-> write_mimeheaders ($ filename, $ mime_filename );
  20. }
  21. Function attach_file ($ filename, $ downfilename, $ mimetype, $ mime_filename ){
  22. $ Encoded = $ this-> encode_file ($ filename );
  23. If ($ mime_filename) $ filename = $ mime_filename;
  24. $ Out = "--". $ this-> mime_boundary. "\ n ";
  25. $ Out = $ out. "Content-type:". $ mimetype. "; name = \" $ filename \ "; \ n ";
  26. $ Out = $ out. "Content-Transfer-Encoding: base64 \ n ";
  27. $ Out = $ out. "Content-disposition: attachment; filename = \" $ downfilename \ "\ n ";
  28. $ Out = $ out. $ encoded. "\ n ";
  29. $ Out = $ out. "--". $ this-> mime_boundary. "--". "\ n ";
  30. Return $ out;
  31. }
  32. Function encode_file ($ sourcefile ){
  33. If (is_readable ($ sourcefile )){
  34. $ Fd = fopen ($ sourcefile, "r ");
  35. $ Contents = fread ($ fd, filesize ($ sourcefile ));
  36. $ Encoded = chunk_split (base64_encode ($ contents ));
  37. Fclose ($ fd );
  38. }
  39. Return $ encoded;
  40. }
  41. Function sendfile (){
  42. $ Headers = $ this-> smtp_headers. $ this-> mime_headers;
  43. $ Message = $ this-> text_body. $ this-> text_encoded;
  44. Mail ($ this-> addr_to, $ this-> subject, $ message, $ headers );
  45. }
  46. Function write_body ($ msgtext ){
  47. $ Out = "--". $ this-> mime_boundary. "\ n ";
  48. $ Out = $ out. "Content-Type: text/plain; charset = \" us-ascii \ "\ n ";
  49. $ Out = $ out. $ msgtext. "\ n ";
  50. Return $ out;
  51. }
  52. Function write_mimeheaders ($ filename, $ mime_filename ){
  53. If ($ mime_filename) $ filename = $ mime_filename;
  54. $ Out = "MIME-version: 1.0 \ n ";
  55. $ Out = $ out. "Content-type: multipart/mixed ;";
  56. $ Out = $ out. "boundary = \" $ this-> mime_boundary \ "\ n ";
  57. $ Out = $ out. "Content-transfer-encoding: 7BIT \ n ";
  58. $ Out = $ out. "X-attachments: $ filename; \ n ";
  59. Return $ out;
  60. }
  61. Function write_smtpheaders ($ addr_from ){
  62. $ Out = "From: $ addr_from \ n ";
  63. $ Out = $ out. "Reply-To: $ addr_from \ n ";
  64. $ Out = $ out. "X-Mailer: PHP3 \ n ";
  65. $ Out = $ out. "X-Sender: $ addr_from \ n ";
  66. Return $ out;
  67. }
  68. }
  69. /* Usage-for example, if mimetype is "image/gif"
  70. $ Mailfile = new CMailFile ($ subject, $ sendto, $ replyto, $ message, $ filename, $ mimetype );
  71. $ Mailfile-> sendfile ();
  72. $ Subject -- topic
  73. $ Sendto -- recipient address
  74. $ Replyto -- Reply address
  75. $ Message -- Mail content
  76. $ Filename -- attachment file name
  77. $ Downfilename -- Name of the file in the lower region
  78. $ Mimetype -- mime type
  79. */
  80. ?>

2. demo. php

  1. Require_once ('emailclass. php ');
  2. // Send an email
  3. // Subject
  4. $ Subject = "test send email ";
  5. // Recipient
  6. $ Sendto = 'ABC @ 163.com ';
  7. // Producer
  8. $ Replyto = 'cdf @ 163.com ';
  9. // Content
  10. $ Message = "test send email content ";
  11. // Attachment
  12. $ Filename = 'test.jpg ';
  13. // Annex
  14. $ Mimetype = "image/jpeg ";
  15. $ Mailfile = new CMailFile ($ subject, $ sendto, $ replyto, $ message, $ filename, $ excelname, $ mimetype );
  16. $ Mailfile-> sendfile ();
  17. ?>

>>> 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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.