ホームページ  >  記事  >  バックエンド開発  >  添付ファイル付きのメールを送信するためのphpクラス

添付ファイル付きのメールを送信するためのphpクラス

WBOY
WBOYオリジナル
2016-07-25 08:55:23839ブラウズ
  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. functionattach_file($filename,$downfilename,$mimetype,$mime_filename) {
  22. $encoded = $this->encode_file($filename);
  23. if ($mime_filename) $filename = $mime_filename;
  24. $out = "--" 。 $this->mime_boundary 。 「ん」;
  25. $out = $out 。 "コンテンツ タイプ: " 。 $mimetype 。 "; 名前="$ファイル名";n";
  26. $out = $out 。 "コンテンツ転送エンコーディング:base64n";
  27. $out = $out 。 "コンテンツの性質: 添付ファイル; ファイル名="$downfilename"nn";
  28. $out = $out 。 $encoded 。 「ん」;
  29. $out = $out 。 「――」。 $this->mime_boundary 。 「――」。 「ん」;
  30. $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. $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 。 「ん」;
  48. $out = $out 。 "Content-Type: text/plain; charset="us-ascii"nn";
  49. $out = $out 。 $msgtext 。 「ん」;
  50. $out を返す;
  51. }
  52. function write_mimeheaders($filename, $mime_filename) {
  53. if ($mime_filename) $filename = $mime_filename;
  54. $out = "MIME バージョン: 1.0n";
  55. $out = $out 。 "コンテンツ タイプ: マルチパート/混合; ";
  56. $out = $out 。 "boundary="$this->mime_boundary"n";
  57. $out = $out 。 "コンテンツ転送エンコーディング: 7BITn";
  58. $out = $out 。 "X 添付ファイル: $filename;nn";
  59. $out を返す;
  60. }
  61. function write_smtpheaders($addr_from) {
  62. $out = "From: $addr_fromn";
  63. $out = $out 。 "返信先: $addr_fromn";
  64. $out = $out 。 "X-メーラー: PHP3n";
  65. $out = $out 。 "X 送信者: $addr_fromn";
  66. $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 = "メール送信テスト"
  5. //受信者
  6. = 'abc@163.com';
  7. //Sender
  8. $replyto = 'cdf@163.com';
  9. //Content
  10. $message = "メールの内容をテスト送信"; 'test.jpg';
  11. //添付ファイルのカテゴリ
  12. $mimetype = "image/jpeg"
  13. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename, $excelname,$mimetype );
  14. $mailfile->sendfile();
  15. ?>
  16. コードをコピー
>>> 興味のある記事: phpソケットはsmtpを使用して添付ファイル付きのメールを送信します
Php での IMAP アプリケーションの例 (メールの送受信、メールの削除、添付ファイルのダウンロード)

添付ファイル付きのメールを送信するPHPMailerの例 PHPMailer によって送信されたメール内の中国語の添付ファイル名が文字化けする問題の解決策

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。