Home  >  Article  >  Backend Development  >  php class for sending emails with attachments

php class for sending emails with attachments

WBOY
WBOYOriginal
2016-07-25 08:55:23839browse
  1. /**
  2. * Send email with attachments
  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 = $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: base64n";
  27. $out = $out . "Content-disposition: attachment; filename="$downfilename"nn";
  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"nn";
  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.0n";
  55. $out = $out . "Content-type: multipart/mixed; ";
  56. $out = $out . "boundary="$this->mime_boundary"n";
  57. $out = $out . "Content-transfer-encoding: 7BITn";
  58. $out = $out . "X-attachments: $filename;nn";
  59. return $out;
  60. }
  61. function write_smtpheaders($addr_from) {
  62. $out = "From: $addr_fromn";
  63. $out = $out . "Reply-To: $addr_fromn";
  64. $out = $out . "X-Mailer: PHP3n";
  65. $out = $out . "X-Sender: $addr_fromn";
  66. return $out;
  67. }
  68. }
  69. /*用法 - 例如:mimetype 为 "image/gif"
  70. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);
  71. $mailfile->sendfile();
  72. $subject -- 主题
  73. $sendto -- 收信人地址
  74. $replyto -- 回复地址
  75. $message -- 信件内容
  76. $filename -- 附件文件名
  77. $downfilename -- 下載的文件名
  78. $mimetype -- mime类型
  79. */
  80. ?>
复制代码

2, demo example demo.php

  1. require_once('emailclass.php');
  2. //Send email
  3. //Subject
  4. $subject = "test send email";
  5. //Recipient
  6. $ sendto = 'abc@163.com';
  7. //Sender
  8. $replyto = 'cdf@163.com';
  9. //Content
  10. $message = "test send email content";
  11. //Attachment
  12. $filename = 'test.jpg';
  13. //Attachment category
  14. $mimetype = "image/jpeg";
  15. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename, $ excelname,$mimetype);
  16. $mailfile->sendfile();
  17. ?>
Copy code

>>>> Articles you may be interested in: php socket uses smtp to send emails with attachments Examples of IMAP applications in Php (send and receive emails, delete emails, download attachments) Example of PHPMailer sending emails with attachments Solution to garbled Chinese attachment names in emails sent by PHPMailer



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn