php 发送带附件邮件的类

WBOY
Lepaskan: 2016-07-25 08:55:23
asal
845 orang telah melayarinya
  1. /**
  2. * 发送带附件的邮件
  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: base64\n";
  27. $out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\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\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\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. /*用法 - 例如: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.php

  1. require_once('emailclass.php');
  2. //发送邮件
  3. //主題
  4. $subject = "test send email";
  5. //收件人
  6. $sendto = 'abc@163.com';
  7. //發件人
  8. $replyto = 'cdf@163.com';
  9. //內容
  10. $message = "test send email content";
  11. //附件
  12. $filename = 'test.jpg';
  13. //附件类別
  14. $mimetype = "image/jpeg";
  15. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);
  16. $mailfile->sendfile();
  17. ?>
复制代码

>>> 您可能感兴趣的文章: php socket使用smtp发送带附件的邮件 Php中IMAP应用举例(收发邮件、删除邮件、附件下载) PHPMailer发送带附件邮件的例子 PHPMailer发送邮件中文附件名乱码的解决办法



Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!