首頁 > 後端開發 > php教程 > 發送郵件的PHP類

發送郵件的PHP類

WBOY
發布: 2016-07-25 08:44:31
原創
861 人瀏覽過
  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_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' . "rn";
  32. $headers .= 'Content-type: text/html; charset ='.$db_charset.'' . "rn";
  33. $headers .= 'From: '.$sitename.' ' . "rn";
  34. mail($toemail , $subject, $message, $headers);
  35. return true;
  36. }
  37. //郵件頭的分隔符號
  38. $maildelimiter = $mail['maildelimiter'] == 1 ? "rn " : ($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", "rn", str_replace("r", "n", str_replace ("rn", "n", str_replace("nr", "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')." uchomern");
  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(substr($lastmessage, 3, 1) != '-' || empty(substr($lastmessage, 3, 1) != '-' || empty(substr($lastmessage, 3, 1) != '-' || empty(substr($lastmessage, 3, 1) != '-' || empty( $lastmessage)) {
  72. break;
  73. }
  74. $lastmessage = fgets($fp, 512);
  75. }
  76. if($mail['auth']) {
  77. fputs($fp, "AUTH LOGINrn");
  78. $lastmessage = fgets($fp, 512);
  79. if( substr($lastmessage, 0, 3) != 334) {
  80. self::runlog('SMTP', "($mail[server]:$mail[port]) 驗證登入- $lastmessage", 0) ;
  81. return false;
  82. }
  83. fputs($fp, base64_encode($mail['auth_username'])."rn");
  84. $lastmessage = fgets($fp, 512 ) ;
  85. if(substr($lastmessage, 0, 3) != 334) {
  86. self::runlog('SMTP', "($mail[伺服器]:$mail[連接埠]) 使用者名稱- $lastmessage ", 0);
  87. return false;
  88. }
  89. fputs($fp, base64_encode($mail['auth_password'])."rn");
  90. $ lastmessage = fgets( $fp, 512);
  91. if(substr($lastmessage, 0, 3) != 235) {
  92. self::runlog('SMTP', "($mail[伺服器]:$ mail[連接埠] ) 密碼- $lastmessage", 0);
  93. 回傳false;
  94. }
  95. $email_from = $mail['from'];
  96. }
  97. fputs($ fp, "郵件寄件人: .*/", "\1", $email_from).">rn");
  98. $lastmessage = fgets($fp, 512);
  99. if(substr($lastmessage, 0, 3) != 250) {
  100. fputs($fp, "mail來自: .*/", "\1", $email_from).">rn");
  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).">rn");
  108. $lastmessage = fgets($fp, 512);
  109. if(substr($lastmessage, 0, 3) != 250) {
  110. fputs( $ fp, "RCPT TO: .*/", "\1", $toemail).">rn");
  111. $ lastmessage = fgets($ fp, 512);
  112. self::runlog('SMTP', "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
  113. return false;
  114. }
  115. fputs($fp, "DATArn");
  116. $lastmessage = fgets($fp, 512);
  117. if(substr($lastmessage, 0, 3) != 354) {
  118. self::runlog('SMTP', "($mail[伺服器]:$mail[連接埠]) DATA - $lastmessage", 0);
  119. 回傳false;
  120. }
  121. $headers .= '訊息ID: {$maildelimiter}";
  122. fputs($fp, "日期: ".gmdate('r')."rn ");
  123. fputs( $fp, "收件人:".$email_to."rn");
  124. fputs($fp, "主題:".$email_subject."rn");
  125. fputs( $fp, $headers. "rn");
  126. fputs($fp, "rnrn");
  127. fputs($fp, "$email_messagern.rn");
  128. $lastmessage = fgets($fp , 512);
  129. $lastmessage = fgets($fp , 512);
  130. if(substr($lastmessage, 0, 3) != 250) {
  131. self::runlog('SMTP', "($mail[伺服器]:$mail[連接埠]) END - $lastmessage", 0 );
  132. }
  133. fputs($fp, "QUITrn");
  134. return true;
  135. }
}
//emailui::sendmail(' admin33@163.com' , '你好哦', '非常感謝');
複製代碼
寄郵件,PHP


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板