程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php如何發送帶附件的郵件

php如何發送帶附件的郵件

編輯:PHP綜合

emailclass.php

[php] view plaincopyprint?

01.<?

02.class CMailFile {

03.

04. var $subject;

05. var $addr_to;

06. var $text_body;

07. var $text_encoded;

08. var $mime_headers;

09. var $mime_boundary = "--==================_846811060==_";

10. var $smtp_headers;

11.

12. function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) {

13. $this->subject = $subject;

14. $this->addr_to = $to;

15. $this->smtp_headers = $this->write_smtpheaders($from);

16. $this->text_body = $this->write_body($msg);

17. $this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename);

18. $this->mime_headers = $this->write_mimeheaders($filename, $mime_filename);

19. }

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.

33. function encode_file($sourcefile) {

34. if (is_readable($sourcefile)) {

35. $fd = fopen($sourcefile, "r");

36. $contents = fread($fd, filesize($sourcefile));

37. $encoded = chunk_split(base64_encode($contents));

38. fclose($fd);

39. }

40. return $encoded;

41. }

42.

43. function sendfile() {

44. $headers = $this->smtp_headers . $this->mime_headers;

45. $message = $this->text_body . $this->text_encoded;

46. mail($this->addr_to,$this->subject,$message,$headers);

47. }

48.

49. function write_body($msgtext) {

50. $out = "--" . $this->mime_boundary . "\n";

51. $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n";

52. $out = $out . $msgtext . "\n";

53. return $out;

54. }

55.

56. function write_mimeheaders($filename, $mime_filename) {

57. if ($mime_filename) $filename = $mime_filename;

58. $out = "MIME-version: 1.0\n";

59. $out = $out . "Content-type: multipart/mixed; ";

60. $out = $out . "boundary=\"$this->mime_boundary\"\n";

61. $out = $out . "Content-transfer-encoding: 7BIT\n";

62. $out = $out . "X-attachments: $filename;\n\n";

63. return $out;

64. }

65.

66. function write_smtpheaders($addr_from) {

67. $out = "From: $addr_from\n";

68. $out = $out . "Reply-To: $addr_from\n";

69. $out = $out . "X-Mailer: PHP3\n";

70. $out = $out . "X-Sender: $addr_from\n";

71. return $out;

72. }

73.}

74.

75./*用法 - 例如:mimetype 為 "image/gif"

76. $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);

77. $mailfile->sendfile();

78.

79. $subject -- 主題

80. $sendto -- 收信人地址

81. $replyto -- 回復地址

82. $message -- 信件內容

83. $filename -- 附件文件名

84. $downfilename -- 下載的文件名

85. $mimetype -- mime類型

86.*/

87.?>

查看本欄目

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved