php实现的发送带附件邮件类实例

这篇文章主要介绍了php实现的发送带附件邮件类,是php程序设计中非常常见的实用技巧,实例演示了邮件类及对应的demo示例,需要的朋友可以参考下

本文实例讲述了php实现的发送带附件邮件类的方法,是一个非常实用的功能。分享给大家供大家参考。具体方法如下:

emailclass.php类文件如下:

  1. <?
  2. class CMailFile {
  3. var $subject;
  4. var $addr_to;
  5. var $text_body;
  6. var $text_encoded;
  7. var $mime_headers;
  8. var $mime_boundary = "--==================_846811060==_";
  9. var $smtp_headers;
  10. function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) {
  11. $this->subject = $subject;
  12. $this->addr_to = $to;
  13. $this->smtp_headers = $this->write_smtpheaders($from);
  14. $this->text_body = $this->write_body($msg);
  15. $this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename);
  16. $this->mime_headers = $this->write_mimeheaders($filename, $mime_filename);
  17. }
  18. function attach_file($filename,$downfilename,$mimetype,$mime_filename) {
  19. $encoded = $this->encode_file($filename);
  20. if ($mime_filename) $filename = $mime_filename;
  21. $out = "--" . $this->mime_boundary . "\n";
  22. $out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n";
  23. $out = $out . "Content-Transfer-Encoding: base64\n";
  24. $out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\n";
  25. $out = $out . $encoded . "\n";
  26. $out = $out . "--" . $this->mime_boundary . "--" . "\n";
  27. return $out;
  28. }
  29. function encode_file($sourcefile) {
  30. if (is_readable($sourcefile)) {
  31. $fd = fopen($sourcefile, "r");
  32. $contents = fread($fd, filesize($sourcefile));
  33. $encoded = chunk_split(base64_encode($contents));
  34. fclose($fd);
  35. }
  36. return $encoded;
  37. }
  38. function sendfile() {
  39. $headers = $this->smtp_headers . $this->mime_headers;
  40. $message = $this->text_body . $this->text_encoded;
  41. mail($this->addr_to,$this->subject,$message,$headers);
  42. }
  43. function write_body($msgtext) {
  44. $out = "--" . $this->mime_boundary . "\n";
  45. $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n";
  46. $out = $out . $msgtext . "\n";
  47. return $out;
  48. }
  49. function write_mimeheaders($filename, $mime_filename) {
  50. if ($mime_filename) $filename = $mime_filename;
  51. $out = "MIME-version: 1.0\n";
  52. $out = $out . "Content-type: multipart/mixed; ";
  53. $out = $out . "boundary=\"$this->mime_boundary\"\n";
  54. $out = $out . "Content-transfer-encoding: 7BIT\n";
  55. $out = $out . "X-attachments: $filename;\n\n";
  56. return $out;
  57. }
  58. function write_smtpheaders($addr_from) {
  59. $out = "From: $addr_from\n";
  60. $out = $out . "Reply-To: $addr_from\n";
  61. $out = $out . "X-Mailer: PHP3\n";
  62. $out = $out . "X-Sender: $addr_from\n";
  63. return $out;
  64. }
  65. }
  66. /*用法 - 例如:mimetype 为 "image/gif"
  67. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);
  68. $mailfile->sendfile();
  69. $subject -- 主题
  70. $sendto -- 收信人地址
  71. $replyto -- 回复地址
  72. $message -- 信件内容
  73. $filename -- 附件文件名
  74. $downfilename -- 下載的文件名
  75. $mimetype -- mime类型
  76. */
  77. ?>

Demo示例文件如下:

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

相信本文所述对大家php程序设计的学习有一定的借鉴价值。