PHP class for sending mail

Source: Internet
Author: User
  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 ']; Mail delivery mode
  12. $mail = $cfg;
  13. } else {
  14. $adminemail = $mail _id;
  15. $mail _type = $mail _type; Mail delivery mode
  16. #端口, the delimiter for the message header, which contains the user name in the recipient address
  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 Send mode
  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. ' < '. $from. ' > '. "\ r \ n";
  34. Mail ($toemail, $subject, $message, $headers);
  35. return true;
  36. }
  37. Delimiter for message headers
  38. $maildelimiter = $mail [' maildelimiter '] = = 1? "\ r \ n": ($mail [' maildelimiter '] = = 2? "\ r": "\ n");
  39. The recipient address contains the user name
  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. The addressee.
  47. $email _to = Preg_match ('/^ (. +?) \< (. +?) \>$/', $toemail, $mats)? ($mailusername? '=?'. $db _charset. '? B? '. Base64_encode ($mats [1]). "? = < $mats [2]> ": $mats [2]): $toemail;;
  48. Message header
  49. $email _subject = ' =? '. $db _charset. '? B? '. Base64_encode (Preg_replace ("/[\r|\n]/", "', $subject. '--". $db _wwwname)). '? =';
  50. Message 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", "\ 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)! = && 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: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $email _from)." >\r\n ");
  98. $lastmessage = fgets ($FP, 512);
  99. if (substr ($lastmessage, 0, 3)! = 250) {
  100. Fputs ($fp, "MAIL from: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\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: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $toemail)." >\r\n ");
  108. $lastmessage = fgets ($FP, 512);
  109. if (substr ($lastmessage, 0, 3)! = 250) {
  110. Fputs ($fp, "RCPT to: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\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: < '. Gmdate (' Ymdhs '). SUBSTR (MD5 ($email _message.microtime ()), 0, 6). Rand (100000, 999999). ' @ '. $_server[' Http_host ']. " >{$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 ', ' Hello Oh ', ' thank you ');
Copy Code
Send Mail, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.