PHP Write SMTP mail send class

Source: Internet
Author: User
Tags ereg php write
    1. $smtpserver = "* * * *";
    2. $smtpserverport = 25;
    3. $smtpuser = "******";
    4. $smtppass = "*******";
    5. $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass); A true in this case is that authentication is used, otherwise it is not used.
    6. $smtp->debug = false;
    7. $emailtype = "HTML";
    8. for ($i =0; $i <5; $i + +) {
    9. $SMTP->sendmail ("* * * *", "******", "Hello world!", "This was only a test!");
    10. }
    11. echo "sent a total of $i email! ";
    12. ?>
Copy Code

The following is an implementation of a specific class.

  1. Class SMTP {

  2. /* Public Variables */
  3. var $smtp _port;
  4. var $time _out;
  5. var $host _name;
  6. var $log _file;
  7. var $relay _host;
  8. var $debug;
  9. var $auth;
  10. var $user;
  11. var $pass;
  12. /* Private Variables */
  13. var $sock;
  14. /* Constractor */
  15. function smtp ($relay _host = "", $smtp _port = +, $auth = False, $user, $pass)
  16. {
  17. $this->debug = false;
  18. $this->smtp_port = $smtp _port;
  19. $this->relay_host = $relay _host;
  20. $this->time_out = 30; is used in Fsockopen ()
  21. $this->auth = $auth; Auth
  22. $this->user = $user;
  23. $this->pass = $pass;
  24. $this->host_name = "localhost"; is used in HELO command
  25. $this->log_file = "";
  26. $this->sock = false;
  27. }
  28. /* Main Function */
  29. function SendMail ($to, $from, $subject = "", $body = "", $mailtype = "", $CC = "", $BCC = "", $additional _headers = "")
  30. {
  31. $mail _from = $this->get_address ($this->strip_comment ($from));
  32. $body = Ereg_replace ("(^| ()) (.) "," 1.3 ", $body);
  33. $header. = "mime-version:1.0";
  34. if ($mailtype = = "HTML") {
  35. $header. = "Content-type:text/html";
  36. }
  37. $header. = "To:". $to. " ";
  38. if ($cc! = "") {
  39. $header. = "Cc:". $CC. " ";
  40. }
  41. $header. = "From: $from <". $from. ">;";
  42. $header. = "Subject:". $subject. " ";
  43. $header. = $additional _headers;
  44. $header. = "Date:". Date ("R"). " ";
  45. $header. = "X-mailer:by Redhat (php/". Phpversion (). ") ";
  46. List ($msec, $sec) = Explode ("", Microtime ());
  47. $header. = "Message-id: <". Date ("Ymdhis", $sec). "." . ($MSEC * 1000000). "." . $mail _from. ">;";
  48. $TO = Explode (",", $this->strip_comment ($to));
  49. if ($cc! = "") {
  50. $TO = Array_merge ($TO, Explode (",", $this->strip_comment ($CC)));
  51. }
  52. if ($bcc! = "") {
  53. $TO = Array_merge ($TO, Explode (",", $this->strip_comment ($BCC)));
  54. }
  55. $sent = true;
  56. foreach ($TO as $rcpt _to) {
  57. $RCPT _to = $this->get_address ($rcpt _to);
  58. if (! $this->smtp_sockopen ($rcpt _to)) {
  59. $this->log_write ("Error:cannot send email to". $RCPT _to. " ");
  60. $sent = false;
  61. Continue
  62. }
  63. if ($this->smtp_send ($this->host_name, $mail _from, $rcpt _to, $header, $body)) {
  64. $this->log_write ("e-mail has been sent to <". $rcpt _to. ">;");
  65. } else {
  66. $this->log_write ("Error:cannot Send email to <". $rcpt _to. ">;");
  67. $sent = false;
  68. }
  69. Fclose ($this->sock);
  70. $this->log_write ("Disconnected from remote host");
  71. }
  72. return $sent;
  73. }

  74. /* Private Functions */

  75. function Smtp_send ($helo, $from, $to, $header, $body = "")
  76. {
  77. if (! $this->smtp_putcmd ("HELO", $helo)) {
  78. return $this->smtp_error ("Sending HELO command");
  79. }
  80. Auth
  81. if ($this->auth) {
  82. if (! $this->smtp_putcmd ("AUTH LOGIN", Base64_encode ($this->user))) {
  83. return $this->smtp_error ("Sending HELO command");
  84. }
  85. if (! $this->smtp_putcmd ("", Base64_encode ($this->pass)) {
  86. return $this->smtp_error ("Sending HELO command");
  87. }
  88. }
  89. if (! $this->smtp_putcmd ("MAIL", "from:<". $from. ">;")) {
  90. return $this->smtp_error ("Sending MAIL from command");
  91. }
  92. if (! $this->smtp_putcmd ("RCPT", "to:<". $to. ">;")) {
  93. return $this->smtp_error ("Sending RCPT to command");
  94. }
  95. if (! $this->smtp_putcmd ("DATA")) {
  96. return $this->smtp_error ("Sending DATA command");
  97. }
  98. if (! $this->smtp_message ($header, $body)) {
  99. return $this->smtp_error ("Sending message");
  100. }
  101. if (! $this->smtp_eom ()) {
  102. return $this->smtp_error ("Sending ; ;. ; ; [EOM] ");
  103. }
  104. if (! $this->smtp_putcmd ("QUIT")) {
  105. return $this->smtp_error ("Sending QUIT command");
  106. }
  107. return true;
  108. }
  109. function Smtp_sockopen ($address)
  110. {
  111. if ($this->relay_host = = "") {
  112. return $this->smtp_sockopen_mx ($address);
  113. } else {
  114. return $this->smtp_sockopen_relay ();
  115. }
  116. }
  117. function Smtp_sockopen_relay ()
  118. {
  119. $this->log_write ("Trying to". $this->relay_host. ":" . $this->smtp_port. " ");
  120. $this->sock = @fsockopen ($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  121. if (! ( $this->sock && $this->SMTP_OK ())) {
  122. $this->log_write ("Error:cannot connenct to relay host". $this->relay_host. " ");
  123. $this->log_write ("Error:".) $errstr. " (" . $errno. ") ");
  124. return false;
  125. }
  126. $this->log_write ("Connected to relay host". $this->relay_host. " ");
  127. return true;;
  128. }
  129. function Smtp_sockopen_mx ($address)
  130. {
  131. $domain = Ereg_replace ("^.+@ ([^@]+) $", "1", $address);
  132. if (! @getmxrr ($domain, $MXHOSTS)) {
  133. $this->log_write ("error:cannot resolve MX" ". $domain. "" ");
  134. return false;
  135. }
  136. foreach ($MXHOSTS as $host) {
  137. $this->log_write ("Trying to". $host. ":" . $this->smtp_port. " ");
  138. $this->sock = @fsockopen ($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  139. if (! ( $this->sock && $this->SMTP_OK ())) {
  140. $this->log_write ("Warning:cannot Connect to MX host". $host. " ");
  141. $this->log_write ("Error:".) $errstr. " (" . $errno. ") ");
  142. Continue
  143. }
  144. $this->log_write ("Connected to MX host". $host. " ");
  145. return true;
  146. }
  147. $this->log_write ("Error:cannot connect to any MX hosts (". Implode (",", $MXHOSTS). ");
  148. return false;
  149. }
  150. function Smtp_message ($header, $body)
  151. {
  152. Fputs ($this->sock, $header. " " . $body);
  153. $this->smtp_debug (">;". Str_replace ("", "". " >; ", $header." >; " . $body. ">;"));
  154. return true;
  155. }
  156. function Smtp_eom ()
  157. {
  158. Fputs ($this->sock, ".");
  159. $this->smtp_debug (". [EOM] ");
  160. return $this->SMTP_OK ();
  161. }
  162. function Smtp_ok ()
  163. {
  164. $response = Str_replace ("", "", Fgets ($this->sock, 512));
  165. $this->smtp_debug ($response. " ");
  166. if (!ereg ("^[23]", $response)) {
  167. Fputs ($this->sock, "QUIT");
  168. Fgets ($this->sock, 512);
  169. $this->log_write ("Error:remote host returned" ". $response. "" ");
  170. return false;
  171. }
  172. return true;
  173. }
  174. function Smtp_putcmd ($cmd, $arg = "")
  175. {
  176. if ($arg! = "") {
  177. if ($cmd = = "") $cmd = $arg;
  178. else $cmd = $cmd. " " . $arg;
  179. }
  180. Fputs ($this->sock, $cmd. " ");
  181. $this->smtp_debug (">;". $cmd. " ");
  182. return $this->SMTP_OK ();
  183. }
  184. function Smtp_error ($string)
  185. {
  186. $this->log_write ("Error:error occurred while". $string. ". ");
  187. return false;
  188. }
  189. function Log_write ($message)
  190. {
  191. $this->smtp_debug ($message);
  192. if ($this->log_file = = "") {
  193. return true;
  194. }
  195. $message = Date ("M D h:i:s"). Get_current_user (). "[" . Getmypid (). "]: " . $message;
  196. if (! @file_exists ($this->log_file) | |! ( $fp = @fopen ($this->log_file, "a")) {
  197. $this->smtp_debug ("Warning:cannot open log File". $this->log_file. "" ");
  198. return false;;
  199. }
  200. Flock ($FP, LOCK_EX);
  201. Fputs ($fp, $message);
  202. Fclose ($FP);
  203. return true;
  204. }
  205. function Strip_comment ($address)
  206. {
  207. $comment = "([^ ()]*)";
  208. while (Ereg ($comment, $address)) {
  209. $address = ereg_replace ($comment, "", $address);
  210. }
  211. return $address;
  212. }
  213. function Get_address ($address)
  214. {
  215. $address = Ereg_replace ("([]) +", "", $address);
  216. $address = Ereg_replace ("^.*< (. +)";;. *$ "," 1 ", $address);
  217. return $address;
  218. }
  219. function Smtp_debug ($message)
  220. {
  221. if ($this->debug) {
  222. Echo $message. ";";
  223. }
  224. }
  225. }
  226. ?>

Copy Code
  • Related Article

    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.