当前位置:Gxlcms > PHP教程 > PHP实现发送邮件的方法(基于简单邮件发送类)_PHP

PHP实现发送邮件的方法(基于简单邮件发送类)_PHP

时间:2021-07-01 10:21:17 帮助过:28人阅读

本文实例讲述了PHP实现发送邮件的方法。分享给大家供大家参考,具体如下:

邮件发送类

  1. <?php
  2. /*邮件发送类
  3. *功能:使用smtp服务器发送邮件
  4. */
  5. class smtp {
  6. /* 全局变量 */
  7. var $smtp_port;
  8. var $time_out;
  9. var $host_name;
  10. var $log_file;
  11. var $relay_host;
  12. var $debug;
  13. var $auth;
  14. var $user;
  15. var $pass;
  16. var $sock;
  17. /* 构造函数 */
  18. function smtp($relay_host = "", $smtp_port = 25, $auth = false, $log_file="", $user="", $pass="") {
  19. $this->debug = FALSE;
  20. $this->smtp_port = $smtp_port;
  21. $this->relay_host = $relay_host;
  22. $this->time_out = 30; //is used in fsockopen()
  23. $this->auth = $auth; //auth
  24. $this->user = $user;
  25. $this->pass = $pass;
  26. $this->host_name = "localhost"; //is used in HELO command
  27. $this->log_file = $log_file; //邮件发送成功失败的日志记录文件
  28. $this->sock = FALSE;
  29. }
  30. function mail_encode($str)
  31. {
  32. //return '=?utf-8?B?'.base64_encode(mb_convert_encoding($str, "GBK", "UTF-8")).'?=';
  33. return "=?UTF-8?B?".base64_encode($str)."?=";
  34. }
  35. /* 主函数,发送邮件 */
  36. function sendmail($flag, $boundary, $to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
  37. $mail_from = $this->get_address ( $this->strip_comment ( $from ) );
  38. $body = ereg_replace ( '(^|(\r\n))(\.)', "\1.\3", $body );
  39. $header = "MIME-Version:1.0\r\n";
  40. if ($mailtype == "HTML") {
  41. if ($flag == 2) {
  42. $header .= "Content-Type:multipart/mixed; boundary= $boundary\r\n";
  43. } else {
  44. $header .= "Content-Type:text/html; charset=\"UTF-8\" \r\n";
  45. }
  46. }
  47. $header .= "To: " . $to . "\r\n";
  48. if ($cc != "") {
  49. $header .= "Cc: " . $cc . "\r\n";
  50. }
  51. $header .= "From: $from<" . $from . ">\r\n";
  52. $subject = self::mail_encode($subject);
  53. $header .= "Subject: " . $subject . "\r\n";
  54. $header .= $additional_headers;
  55. $header .= "Date: " . date ( "r" ) . "\r\n";
  56. $header .= "X-Mailer:By redhat (PHP/" . phpversion () . ")\r\n";
  57. list ( $msec, $sec ) = explode ( " ", microtime () );
  58. $header .= "Message-ID: <" . date ( "YmdHis", $sec ) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
  59. $TO = explode ( ",", $this->strip_comment ( $to ) );
  60. if ($cc != "") {
  61. $TO = array_merge ( $TO, explode ( ",", $this->strip_comment ( $cc ) ) );
  62. }
  63. if ($bcc != "") {
  64. $TO = array_merge ( $TO, explode ( ",", $this->strip_comment ( $bcc ) ) );
  65. }
  66. $sent = TRUE;
  67. foreach ( $TO as $rcpt_to ) {
  68. $rcpt_to = $this->get_address ( $rcpt_to );
  69. if (! $this->smtp_sockopen ( $rcpt_to )) {
  70. $this->log_write ( "Error: Cannot send email to " . $rcpt_to . "\n" );
  71. $sent = FALSE;
  72. continue;
  73. }
  74. if ($this->smtp_send ( $this->host_name, $mail_from, $rcpt_to, $header, $body )) {
  75. $this->log_write ( "E-mail has been sent to <" . $rcpt_to . ">\n" );
  76. } else {
  77. $this->log_write ( "Error: Cannot send email to <" . $rcpt_to . ">\n" );
  78. $sent = FALSE;
  79. }
  80. fclose ( $this->sock );
  81. $this->log_write ( "Disconnected from remote host\n" );
  82. }
  83. return $sent;
  84. }
  85. /* 私有函数 */
  86. function smtp_send($helo, $from, $to, $header, $body = "") {
  87. if (! $this->smtp_putcmd ( "HELO", $helo )) {
  88. return $this->smtp_error ( "sending HELO command" );
  89. }
  90. if ($this->auth) {
  91. if (! $this->smtp_putcmd ( "AUTH LOGIN", base64_encode ( $this->user ) )) {
  92. return $this->smtp_error ( "sending HELO command" );
  93. }
  94. if (! $this->smtp_putcmd ( "", base64_encode ( $this->pass ) )) {
  95. return $this->smtp_error ( "sending HELO command" );
  96. }
  97. }
  98. if (! $this->smtp_putcmd ( "MAIL", "FROM:<" . $from . ">" )) {
  99. return $this->smtp_error ( "sending MAIL FROM command" );
  100. }
  101. if (! $this->smtp_putcmd ( "RCPT", "TO:<" . $to . ">" )) {
  102. return $this->smtp_error ( "sending RCPT TO command" );
  103. }
  104. if (! $this->smtp_putcmd ( "DATA" )) {
  105. return $this->smtp_error ( "sending DATA command" );
  106. }
  107. if (! $this->smtp_message ( $header, $body )) {
  108. return $this->smtp_error ( "sending message" );
  109. }
  110. if (! $this->smtp_eom ()) {
  111. return $this->smtp_error ( "sending <cr><lf>.<cr><lf> [EOM]" );
  112. }
  113. if (! $this->smtp_putcmd ( "QUIT" )) {
  114. return $this->smtp_error ( "sending QUIT command" );
  115. }
  116. return TRUE;
  117. }
  118. function smtp_sockopen($address) {
  119. if ($this->relay_host == "") {
  120. return $this->smtp_sockopen_mx ( $address );
  121. } else {
  122. return $this->smtp_sockopen_relay ();
  123. }
  124. }
  125. function smtp_sockopen_relay() {
  126. $this->log_write ( "Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n" );
  127. $this->sock = @fsockopen ( $this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out );
  128. if (! ($this->sock && $this->smtp_ok ())) {
  129. $this->log_write ( "Error: Cannot connenct to relay host " . $this->relay_host . "\n" );
  130. $this->log_write ( "Error: " . $errstr . " (" . $errno . ")\n" );
  131. return FALSE;
  132. }
  133. $this->log_write ( "Connected to relay host " . $this->relay_host . "\n" );
  134. return TRUE;
  135. }
  136. function smtp_sockopen_mx($address) {
  137. $domain = ereg_replace ( "^.+@([^@]+){1}quot;, "\1", $address );
  138. if (! @getmxrr ( $domain, $MXHOSTS )) {
  139. $this->log_write ( "Error: Cannot resolve MX \"" . $domain . "\"\n" );
  140. return FALSE;
  141. }
  142. foreach ( $MXHOSTS as $host ) {
  143. $this->log_write ( "Trying to " . $host . ":" . $this->smtp_port . "\n" );
  144. $this->sock = @fsockopen ( $host, $this->smtp_port, $errno, $errstr, $this->time_out );
  145. if (! ($this->sock && $this->smtp_ok ())) {
  146. $this->log_write ( "Warning: Cannot connect to mx host " . $host . "\n" );
  147. $this->log_write ( "Error: " . $errstr . " (" . $errno . ")\n" );
  148. continue;
  149. }
  150. $this->log_write ( "Connected to mx host " . $host . "\n" );
  151. return TRUE;
  152. }
  153. $this->log_write ( "Error: Cannot connect to any mx hosts (" . implode ( ", ", $MXHOSTS ) . ")\n" );
  154. return FALSE;
  155. }
  156. function smtp_message($header, $body) {
  157. fputs ( $this->sock, $header . "\r\n" . $body );
  158. $this->smtp_debug ( "> " . str_replace ( "\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> " ) );
  159. return TRUE;
  160. }
  161. function smtp_eom() {
  162. fputs ( $this->sock, "\r\n.\r\n" );
  163. $this->smtp_debug ( ". [EOM]\n" );
  164. return $this->smtp_ok ();
  165. }
  166. function smtp_ok() {
  167. $response = str_replace ( "\r\n", "", fgets ( $this->sock, 512 ) );
  168. $this->smtp_debug ( $response . "\n" );
  169. if (! ereg ( "^[23]", $response )) {
  170. fputs ( $this->sock, "QUIT\r\n" );
  171. fgets ( $this->sock, 512 );
  172. $this->log_write ( "Error: Remote host returned \"" . $response . "\"\n" );
  173. return FALSE;
  174. }
  175. return TRUE;
  176. }
  177. function smtp_putcmd($cmd, $arg = "") {
  178. if ($arg != "") {
  179. if ($cmd == "")
  180. $cmd = $arg;
  181. else
  182. $cmd = $cmd . " " . $arg;
  183. }
  184. fputs ( $this->sock, $cmd . "\r\n" );
  185. $this->smtp_debug ( "> " . $cmd . "\n" );
  186. return $this->smtp_ok ();
  187. }
  188. function smtp_error($string) {
  189. $this->log_write ( "Error: Error occurred while " . $string . ".\n" );
  190. return FALSE;
  191. }
  192. function log_write($message) {
  193. $this->smtp_debug ( $message );
  194. if ($this->log_file == "") {
  195. return TRUE;
  196. }
  197. $message = date ( "M d H:i:s " ) . get_current_user () . "[" . getmypid () . "]: " . $message;
  198. if (! @file_exists ( $this->log_file ) || ! ($fp = @fopen ( $this->log_file, "a" ))) {
  199. $this->smtp_debug ( "Warning: Cannot open log file \"" . $this->log_file . "\"\n" );
  200. return FALSE;
  201. }
  202. flock ( $fp, LOCK_EX );
  203. fputs ( $fp, $message );
  204. fclose ( $fp );
  205. return TRUE;
  206. }
  207. function strip_comment($address) {
  208. $comment = '[()]∗';
  209. while ( ereg ( $comment, $address ) ) {
  210. $address = ereg_replace ( $comment, "", $address );
  211. }
  212. return $address;
  213. }
  214. function get_address($address) {
  215. $address = ereg_replace ( "([ \t\r\n])+", "", $address );
  216. $address = ereg_replace ( "^.*<(.+)>.*{1}quot;, "\1", $address );
  217. return $address;
  218. }
  219. function smtp_debug($message) {
  220. if ($this->debug) {
  221. echo $message;
  222. }
  223. }
  224. }
  225. ?>
  226. </lf></cr></lf></cr>

调用邮件发送类发送邮件

  1. <?php
  2. //文件全路径名称,文件名称
  3. function send_smtp_mail($file, $fileName) {
  4. require ("smtp_mail.php");
  5. date_default_timezone_set ( 'Asia/Shanghai' );
  6. $subject = date ( "Y-m-d" ) . "邮件标题"; //邮件标题
  7. $content = "邮件内容!"; //邮件内容
  8. //$file = "/a/b/c.txt"; //附件
  9. //$fileName = "email_log.log"; //附件名称
  10. $smtpserver = "服务器ip"; //SMTP服务器
  11. $smtpserverport = 25; //SMTP服务器端口
  12. $bcc = ""; //副收件人
  13. //$smtpuser = ""; //SMTP服务器的用户帐号
  14. //$smtppass = ""; //SMTP服务器的用户密码
  15. $smtpmailfrom = "aaa@bbb.com"; //SMTP服务器的用户邮箱,邮件发送者
  16. $smtpemailto = "邮箱1,邮箱2,邮箱3"; //邮件接受者
  17. $cc = ""; //抄送
  18. $mailsubject = $subject; //邮件主题
  19. $mailtype = "HTML"; //邮件格式(HTML/TXT),TXT为文本邮件
  20. $additional_headers = "";
  21. $smtplogfile = ""; //发送邮件的日志文件,如果没有就不记录
  22. // 定义分界线
  23. $boundary = uniqid ( "" );
  24. $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
  25. //附件类型
  26. $mimeType = "application/unknown";
  27. // 打开文件
  28. $fp = fopen ( $file, "r" );
  29. // 把整个文件读入一个变量
  30. $read = fread ( $fp, filesize ( $file ) );
  31. //我们用base64方法把它编码
  32. $read = base64_encode ( $read );
  33. //把这个长字符串切成由每行76个字符组成的小块
  34. $read = chunk_split ( $read );
  35. fclose ( $fp ); //关闭文件
  36. /* 邮件发送代码 */
  37. $flag = 1; //判断使用什么样的文件头1或2
  38. if ($fileName == "") {
  39. //没有附件
  40. $body = $content;
  41. } else {
  42. //有附件
  43. $flag = 2;
  44. if ($mailtype == "HTML") {
  45. $body = "--$boundary\r\n"; //此必须\r\n
  46. $body .= "Content-Type:text/html\r\n\r\n"; //此必须\r\n\r\n
  47. $body .= "$content\r\n";
  48. $body .= "--$boundary\r\n";
  49. $body .= "Content-type: $mimeType; name=$fileName\r\n";
  50. $body .= "Content-disposition: attachment; filename=$fileName\r\n";
  51. $body .= "Content-Transfer-Encoding: BASE64\r\n\r\n";
  52. $body .= "$read\r\n\r\n";
  53. $body .= "--$boundary--\r\n";
  54. } else {
  55. $body = "--$boundary\r\n";
  56. $body .= "Content-type: Content-type: text/plain; charset=iso-8859-1\r\n\r\n";
  57. $body .= "Content-transfer-encoding: 8bit\r\n\r\n";
  58. $body .= "$content\r\n\r\n";
  59. $body .= "--$boundary\r\n";
  60. $body .= "Content-type: $mimeType; name=$fileName\r\n\r\n";
  61. $body .= "Content-disposition: attachment; filename=$fileName\r\n\r\n";
  62. $body .= "Content-transfer-encoding: base64\r\n\r\n";
  63. $body .= "$read\r\n\r\n";
  64. $body .= "--$boundary--\r\n";
  65. }
  66. }
  67. $mailbody = $body; //邮件内容
  68. //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  69. $smtp = new smtp ( $smtpserver, $smtpserverport, false, $smtplogfile );
  70. //$smtp->debug = TRUE;//是否显示发送的调试信息
  71. if ($smtp->sendmail ( $flag, $boundary, $smtpemailto, $smtpmailfrom, $mailsubject, $mailbody, $mailtype, $cc, $bcc, $additional_headers )) {
  72. echo ("发送成功");
  73. } else {
  74. echo ("发送失败");
  75. }
  76. }
  77. ?>

希望本文所述对大家php程序设计有所帮助。

人气教程排行