Home  >  Article  >  Backend Development  >  PHP class for sending emails

PHP class for sending emails

WBOY
WBOYOriginal
2016-07-25 08:44:31836browse
  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']; //Email sending mode
  12. $mail = $cfg;
  13. } else {
  14. $adminemail = $mail_id;
  15. $ mail_type = $mail_type; //Mail sending mode
  16. #Port, mail header delimiter, recipient address contains user name
  17. $mail= Array (
  18. 'mailsend' => 2,
  19. 'maildelimiter' => 1,
  20. 'mailusername' => 1,
  21. 'server' => $mail_server, #server
  22. 'port' => $mail_port, #port
  23. 'auth' => $mail_id, #author
  24. 'from ' => $mail_id, #Sender
  25. 'auth_username' => $mail_id, #Account
  26. 'auth_password' => $mail_pw #Password
  27. );
  28. }
  29. //mail sending mode
  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.' <'.$from.'>' . "rn";
  34. mail($toemail, $subject, $message, $headers);
  35. return true;
  36. }
  37. / /The delimiter of the mail header
  38. $maildelimiter = $mail['maildelimiter'] == 1 ? "rn" : ($mail['maildelimiter'] == 2 ? "r" : "n");
  39. //Receive The sender address contains the username
  40. $mailusername = isset($mail['mailusername']) ? $mail['mailusername'] : 1;
  41. //Port
  42. $mail['port'] = $mail['port '] ? $mail['port'] : 25;
  43. $mail['mailsend'] = $mail['mailsend'] ? $mail['mailsend'] : 1;
  44. //Sender
  45. $email_from = $ from == '' ? '=?'.$db_charset.'?B?'.base64_encode($sitename)."?= <".$adminemail.">" : (preg_match('/^(.+ ?) <(.+?)>$/',$from, $mats) ? '=?'.$db_charset.'?B?'.base64_encode($mats[1])."?= < $mats[2]>" : $from);
  46. //Recipient
  47. $email_to = preg_match('/^(.+?) <(.+?)>$/',$toemail, $mats ) ? ($mailusername ? '=?'.$db_charset.'?B?'.base64_encode($mats[1])."?= <$mats[2]>" : $mats[2]) : $toemail;;
  48. //Email title
  49. $email_subject = '=?'.$db_charset.'?B?'.base64_encode(preg_replace("/[r|n]/", '', $subject.'-- '.$db_wwwname)).'?=';
  50. //Mail content
  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} 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($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]) AUTH LOGIN - $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[server]:$mail[port]) USERNAME - $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[server]:$mail[port]) PASSWORD - $lastmessage", 0);
  93. return false;
  94. }
  95. $email_from = $mail['from'];
  96. }
  97. fputs($fp, "MAIL FROM: <".preg_replace("/.*<(.+?)>.*/", "\1", $email_from).">rn");
  98. $lastmessage = fgets($fp, 512);
  99. if(substr($lastmessage, 0, 3) != 250) {
  100. fputs($fp, "MAIL FROM: <".preg_replace("/.*<(.+?)>.*/", "\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: <".preg_replace("/.*<(.+?)>.*/", "\1", $toemail).">rn");
  108. $lastmessage = fgets($fp, 512);
  109. if(substr($lastmessage, 0, 3) != 250) {
  110. fputs($fp, "RCPT TO: <".preg_replace("/.*<(.+?)>.*/", "\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[server]:$mail[port]) DATA - $lastmessage", 0);
  119. return false;
  120. }
  121. $headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}";
  122. fputs($fp, "Date: ".gmdate('r')."rn");
  123. fputs($fp, "To: ".$email_to."rn");
  124. fputs($fp, "Subject: ".$email_subject."rn");
  125. fputs($fp, $headers."rn");
  126. fputs($fp, "rnrn");
  127. fputs($fp, "$email_messagern.rn");
  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, "QUITrn");
  133. return true;
  134. }
  135. }
  136. //emailui::sendmail('admin33@163.com', '你好哦', '非常感谢');
复制代码

发送邮件, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn