Heim  >  Artikel  >  Backend-Entwicklung  >  发送邮件的PHP类

发送邮件的PHP类

WBOY
WBOYOriginal
2016-07-25 08:44:31836Durchsuche
  1. class emailui
  2. {
  3. static function runlog($mode = 'SMTP',$b = '',$c = '',$d='') {
  4. }
  5. static function sendmail($toemail, $subject, $message, $from='',$cfg = array(), $sitename='') {
  6. global $db_wwwname,$mail_port,$mail_id,$mail_server,$mail_pw,$mail_type,$db_charset,$version,$smtpfrom;
  7. $sitename = $sitename ? $sitename : $db_wwwname;
  8. $adminemail = $mail_id ? $mail_id : 'admin@54ui.com';
  9. if($cfg && is_array($cfg)) {
  10. $adminemail = $cfg['from'];
  11. $mail_type = $cfg['mail_type']; //邮件发送模式
  12. $mail = $cfg;
  13. } else {
  14. $adminemail = $mail_id;
  15. $mail_type = $mail_type; //邮件发送模式
  16. #端口,邮件头的分隔符,收件人地址中包含用户名
  17. $mail= Array (
  18. 'mailsend' => 2,
  19. 'maildelimiter' => 1,
  20. 'mailusername' => 1,
  21. 'server' => $mail_server, #服务器
  22. 'port' => $mail_port, #端口
  23. 'auth' => $mail_id, #作者
  24. 'from' => $mail_id, #发信者
  25. 'auth_username' => $mail_id, #账号
  26. 'auth_password' => $mail_pw #密码
  27. );
  28. }
  29. //mail 发送模式
  30. if(!$mail_type) {
  31. $headers = 'MIME-Version: 1.0' . "\r\n";
  32. $headers .= 'Content-type: text/html; charset='.$db_charset.'' . "\r\n";
  33. $headers .= 'From: '.$sitename.' ' . "\r\n";
  34. mail($toemail, $subject, $message, $headers);
  35. return true;
  36. }
  37. //邮件头的分隔符
  38. $maildelimiter = $mail['maildelimiter'] == 1 ? "\r\n" : ($mail['maildelimiter'] == 2 ? "\r" : "\n");
  39. //收件人地址中包含用户名
  40. $mailusername = isset($mail['mailusername']) ? $mail['mailusername'] : 1;
  41. //端口
  42. $mail['port'] = $mail['port'] ? $mail['port'] : 25;
  43. $mail['mailsend'] = $mail['mailsend'] ? $mail['mailsend'] : 1;
  44. //发信者
  45. $email_from = $from == '' ? '=?'.$db_charset.'?B?'.base64_encode($sitename)."?= " : (preg_match('/^(.+?) \$/',$from, $mats) ? '=?'.$db_charset.'?B?'.base64_encode($mats[1])."?= " : $from);
  46. //收信人
  47. $email_to = preg_match('/^(.+?) \$/',$toemail, $mats) ? ($mailusername ? '=?'.$db_charset.'?B?'.base64_encode($mats[1])."?= " : $mats[2]) : $toemail;;
  48. //邮件标题
  49. $email_subject = '=?'.$db_charset.'?B?'.base64_encode(preg_replace("/[\r|\n]/", '', $subject.'--'.$db_wwwname)).'?=';
  50. //邮件内容
  51. $message = $message."Published at ".gmdate("Y-m-d h:i:s",mktime()+8*3600)." , Powered By uicms ".$version." (service.54ui.com)";
  52. $email_message = chunk_split(base64_encode(str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message))))));
  53. $headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: abaoei cms {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=".$db_charset."{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
  54. if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
  55. self::runlog('SMTP', "($mail[server]:$mail[port]) CONNECT - Unable to connect to the SMTP server", 0);
  56. return false;
  57. }
  58. stream_set_blocking($fp, true);
  59. $lastmessage = fgets($fp, 512);
  60. if(substr($lastmessage, 0, 3) != '220') {
  61. self::runlog('SMTP', "$mail[server]:$mail[port] CONNECT - $lastmessage", 0);
  62. return false;
  63. }
  64. fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." uchome\r\n");
  65. $lastmessage = fgets($fp, 512);
  66. if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
  67. self::runlog('SMTP', "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0);
  68. return false;
  69. }
  70. while(1) {
  71. if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
  72. break;
  73. }
  74. $lastmessage = fgets($fp, 512);
  75. }
  76. if($mail['auth']) {
  77. fputs($fp, "AUTH LOGIN\r\n");
  78. $lastmessage = fgets($fp, 512);
  79. if(substr($lastmessage, 0, 3) != 334) {
  80. self::runlog('SMTP', "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0);
  81. return false;
  82. }
  83. fputs($fp, base64_encode($mail['auth_username'])."\r\n");
  84. $lastmessage = fgets($fp, 512);
  85. if(substr($lastmessage, 0, 3) != 334) {
  86. self::runlog('SMTP', "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0);
  87. return false;
  88. }
  89. fputs($fp, base64_encode($mail['auth_password'])."\r\n");
  90. $lastmessage = fgets($fp, 512);
  91. if(substr($lastmessage, 0, 3) != 235) {
  92. self::runlog('SMTP', "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0);
  93. return false;
  94. }
  95. $email_from = $mail['from'];
  96. }
  97. fputs($fp, "MAIL FROM: .*/", "\\1", $email_from).">\r\n");
  98. $lastmessage = fgets($fp, 512);
  99. if(substr($lastmessage, 0, 3) != 250) {
  100. fputs($fp, "MAIL FROM: .*/", "\\1", $email_from).">\r\n");
  101. $lastmessage = fgets($fp, 512);
  102. if(substr($lastmessage, 0, 3) != 250) {
  103. self::runlog('SMTP', "($mail[server]:$mail[port]) MAIL FROM - $lastmessage", 0);
  104. return false;
  105. }
  106. }
  107. fputs($fp, "RCPT TO: .*/", "\\1", $toemail).">\r\n");
  108. $lastmessage = fgets($fp, 512);
  109. if(substr($lastmessage, 0, 3) != 250) {
  110. fputs($fp, "RCPT TO: .*/", "\\1", $toemail).">\r\n");
  111. $lastmessage = fgets($fp, 512);
  112. self::runlog('SMTP', "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
  113. return false;
  114. }
  115. fputs($fp, "DATA\r\n");
  116. $lastmessage = fgets($fp, 512);
  117. if(substr($lastmessage, 0, 3) != 354) {
  118. self::runlog('SMTP', "($mail[server]:$mail[port]) DATA - $lastmessage", 0);
  119. return false;
  120. }
  121. $headers .= 'Message-ID: {$maildelimiter}";
  122. fputs($fp, "Date: ".gmdate('r')."\r\n");
  123. fputs($fp, "To: ".$email_to."\r\n");
  124. fputs($fp, "Subject: ".$email_subject."\r\n");
  125. fputs($fp, $headers."\r\n");
  126. fputs($fp, "\r\n\r\n");
  127. fputs($fp, "$email_message\r\n.\r\n");
  128. $lastmessage = fgets($fp, 512);
  129. if(substr($lastmessage, 0, 3) != 250) {
  130. self::runlog('SMTP', "($mail[server]:$mail[port]) END - $lastmessage", 0);
  131. }
  132. fputs($fp, "QUIT\r\n");
  133. return true;
  134. }
  135. }
  136. //emailui::sendmail('admin33@163.com', '你好哦', '非常感谢');
复制代码

发送邮件, PHP


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn