PHP smtp Send mail

Source: Internet
Author: User
Tags ereg

PHP sends mail through SMTP through a class SMTP

The code to send the message is as follows:

    1. Require_once ' smtp.php ';
    2. ##########################################
    3. $smtpserver = "smtp.sina.com";//SMTP server
    4. $smtpserverport = 25;//SMTP Server port
    5. $smtpusermail = "god_chen@sina.com"; user mailbox for//SMTP server
    6. $smtpemailto = "45323333@qq.com";//Send To whom
    7. $smtpuser = "name"; user account for//SMTP server
    8. $smtppass = "123456"; user password for//SMTP server
    9. $mailsubject = "Chinese";//Message subject
    10. $mailbody = "

      Chinese

      Can muddy the new year feeling under test ";//message content
    11. $mailtype = "HTML";//Message Format (html/txt), TXT as text mail
    12. ##########################################
    13. $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass);//A true in this indicates that authentication is used, otherwise no authentication is used.
    14. $SMTP->debug = false;//whether to display debug information sent
    15. $smtp->sendmail ($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
Copy Code


The source code for the SMTP class is as follows:

  1. Class SMTP
  2. {
  3. /* Public Variables */
  4. var $smtp _port;
  5. var $time _out;
  6. var $host _name;
  7. var $log _file;
  8. var $relay _host;
  9. var $debug;
  10. var $auth;
  11. var $user;
  12. var $pass;
  13. /* Private Variables */
  14. var $sock;
  15. /* Constractor */
  16. function smtp ($relay _host = "", $smtp _port = +, $auth = False, $user, $pass)
  17. {
  18. $this->debug = FALSE;
  19. $this->smtp_port = $smtp _port;
  20. $this->relay_host = $relay _host;
  21. $this->time_out = 30; is used in Fsockopen ()
  22. $this->auth = $auth;//auth
  23. $this->user = $user;
  24. $this->pass = $pass;
  25. $this->host_name = "localhost"; is used in HELO command
  26. $this->log_file = "";
  27. $this->sock = FALSE;
  28. }
  29. /* Main Function */
  30. function SendMail ($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $BCC = "", $additional _headers = "")
  31. {
  32. $mail _from = $this->get_address ($this->strip_comment ($from));
  33. $body = Ereg_replace ("(^| ( \ \ \)] (\) "," \1.\3 ", $body);
  34. $header = "mime-version:1.0\r\n";
  35. if ($mailtype = = "HTML")
  36. {
  37. $header. = "content-type:text/html\r\n";
  38. }
  39. $header. = "To:". $to. " \ r \ n ";
  40. if ($cc! = "")
  41. {
  42. $header. = "Cc:". $cc. " \ r \ n ";
  43. }
  44. $header. = "From: $from <". $from. " >\r\n ";
  45. $header. = "Subject:". $subject. " \ r \ n ";
  46. $header. = $additional _headers;
  47. $header. = "Date:". Date ("R"). " \ r \ n ";
  48. $header. = "X-mailer:by Redhat (php/". Phpversion (). ") \ r \ n ";
  49. List ($msec, $sec) = Explode ("", Microtime ());
  50. $header. = "Message-id: <". Date ("Ymdhis", $sec). ".". ($msec *1000000). ".". $mail _from. " >\r\n ";
  51. $TO = Explode (",", $this->strip_comment ($to));
  52. if ($cc! = "")
  53. {
  54. $TO = Array_merge ($TO, Explode (",", $this->strip_comment ($CC)));
  55. }
  56. if ($bcc! = "")
  57. {
  58. $TO = Array_merge ($TO, Explode (",", $this->strip_comment ($BCC)));
  59. }
  60. $sent = TRUE;
  61. foreach ($TO as $rcpt _to)
  62. {
  63. $RCPT _to = $this->get_address ($rcpt _to);
  64. if (! $this->smtp_sockopen ($rcpt _to))
  65. {
  66. $this->log_write ("Error:cannot send email to". $rcpt _to. " \ n ");
  67. $sent = FALSE;
  68. Continue
  69. }
  70. if ($this->smtp_send ($this->host_name, $mail _from, $rcpt _to, $header, $body))
  71. {
  72. $this->log_write ("e-mail has been sent to <". $rcpt _to. " >\n ");
  73. }
  74. Else
  75. {
  76. $this->log_write ("Error:cannot Send email to <". $rcpt _to. " >\n ");
  77. $sent = FALSE;
  78. }
  79. Fclose ($this->sock);
  80. $this->log_write ("Disconnected from remote host\n");
  81. }
  82. return $sent;
  83. }
  84. /* Private Functions */
  85. function Smtp_send ($helo, $from, $to, $header, $body = "")
  86. {
  87. if (! $this->smtp_putcmd ("HELO", $helo))
  88. {
  89. return $this->smtp_error ("Sending HELO command");
  90. }
  91. #auth
  92. if ($this->auth)
  93. {
  94. if (! $this->smtp_putcmd ("AUTH LOGIN", Base64_encode ($this->user)))
  95. {
  96. return $this->smtp_error ("Sending HELO command");
  97. }
  98. if (! $this->smtp_putcmd ("", Base64_encode ($this->pass))
  99. {
  100. return $this->smtp_error ("Sending HELO command");
  101. }
  102. }
  103. if (! $this->smtp_putcmd ("MAIL", "from:<". $from. " > "))
  104. {
  105. return $this->smtp_error ("Sending MAIL from command");
  106. }
  107. if (! $this->smtp_putcmd ("RCPT", "to:<". $to. " > "))
  108. {
  109. return $this->smtp_error ("Sending RCPT to command");
  110. }
  111. if (! $this->smtp_putcmd ("DATA"))
  112. {
  113. return $this->smtp_error ("Sending DATA command");
  114. }
  115. if (! $this->smtp_message ($header, $body))
  116. {
  117. return $this->smtp_error ("Sending message");
  118. }
  119. if (! $this->smtp_eom ())
  120. {
  121. return $this->smtp_error ("Sending . [EOM] ");
  122. }
  123. if (! $this->smtp_putcmd ("QUIT"))
  124. {
  125. return $this->smtp_error ("Sending QUIT command");
  126. }
  127. return TRUE;
  128. }
  129. function Smtp_sockopen ($address)
  130. {
  131. if ($this->relay_host = = "")
  132. {
  133. return $this->smtp_sockopen_mx ($address);
  134. }
  135. Else
  136. {
  137. return $this->smtp_sockopen_relay ();
  138. }
  139. }
  140. function Smtp_sockopen_relay ()
  141. {
  142. $this->log_write ("Trying to". $this->relay_host. ":". $this->smtp_port. " \ n ");
  143. $this->sock = @fsockopen ($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  144. if (! ( $this->sock && $this->SMTP_OK ()))
  145. {
  146. $this->log_write ("Error:cannot connenct to relay host". $this->relay_host. " \ n ");
  147. $this->log_write ("Error:". $errstr. " (". $errno.") \ n ");
  148. return FALSE;
  149. }
  150. $this->log_write ("Connected to relay Host". $this->relay_host. " \ n ");
  151. return TRUE;;
  152. }
  153. function Smtp_sockopen_mx ($address)
  154. {
  155. $domain = Ereg_replace ("^.+@ ([^@]+) $", "\1", $address);
  156. if (! @getmxrr ($domain, $MXHOSTS))
  157. {
  158. $this->log_write ("error:cannot resolve MX \" ". $domain." \ "\ n");
  159. return FALSE;
  160. }
  161. foreach ($MXHOSTS as $host)
  162. {
  163. $this->log_write ("Trying to". $host. ":". $this->smtp_port. " \ n ");
  164. $this->sock = @fsockopen ($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  165. if (! ( $this->sock && $this->SMTP_OK ()))
  166. {
  167. $this->log_write ("Warning:cannot Connect to MX host". $host. " \ n ");
  168. $this->log_write ("Error:". $errstr. " (". $errno.") \ n ");
  169. Continue
  170. }
  171. $this->log_write ("Connected to MX host". $host. " \ n ");
  172. return TRUE;
  173. }
  174. $this->log_write ("Error:cannot connect to any MX hosts (". Implode (",", $MXHOSTS). ") \ n ");
  175. return FALSE;
  176. }
  177. function Smtp_message ($header, $body)
  178. {
  179. Fputs ($this->sock, $header. " \ r \ n ". $body);
  180. $this->smtp_debug (">". Str_replace ("\ r \ n", "\ n".) > ", $header." \n> ". $body." \n> "));
  181. return TRUE;
  182. }
  183. function Smtp_eom ()
  184. {
  185. Fputs ($this->sock, "\r\n.\r\n");
  186. $this->smtp_debug (". [eom]\n ");
  187. return $this->SMTP_OK ();
  188. }
  189. function Smtp_ok ()
  190. {
  191. $response = Str_replace ("\ r \ n", "", Fgets ($this->sock, 512));
  192. $this->smtp_debug ($response. " \ n ");
  193. if (!ereg ("^[23]", $response))
  194. {
  195. Fputs ($this->sock, "quit\r\n");
  196. Fgets ($this->sock, 512);
  197. $this->log_write ("Error:remote host returned \" ". $response." \ "\ n");
  198. return FALSE;
  199. }
  200. return TRUE;
  201. }
  202. function Smtp_putcmd ($cmd, $arg = "")
  203. {
  204. if ($arg! = "")
  205. {
  206. if ($cmd = = "")
  207. {
  208. $cmd = $arg;
  209. }
  210. Else
  211. {
  212. $cmd = $cmd. " ". $arg;
  213. }
  214. }
  215. Fputs ($this->sock, $cmd. " \ r \ n ");
  216. $this->smtp_debug (">". $cmd. " \ n ");
  217. return $this->SMTP_OK ();
  218. }
  219. function Smtp_error ($string)
  220. {
  221. $this->log_write ("Error:error occurred while". $string. ". \ n ");
  222. return FALSE;
  223. }
  224. function Log_write ($message)
  225. {
  226. $this->smtp_debug ($message);
  227. if ($this->log_file = = "")
  228. {
  229. return TRUE;
  230. }
  231. $message = Date ("M D h:i:s"). Get_current_user (). " [". Getmypid ()."]: ". $message;
  232. if (! @file_exists ($this->log_file) | |! ( $fp = @fopen ($this->log_file, "a")))
  233. {
  234. $this->smtp_debug ("warning:cannot open log file \" ". $this->log_file." \ "\ n");
  235. return FALSE;;
  236. }
  237. Flock ($FP, LOCK_EX);
  238. Fputs ($fp, $message);
  239. Fclose ($FP);
  240. return TRUE;
  241. }
  242. function Strip_comment ($address)
  243. {
  244. $comment = "\ ([^ ()]*\)";
  245. while (Ereg ($comment, $address))
  246. {
  247. $address = ereg_replace ($comment, "", $address);
  248. }
  249. return $address;
  250. }
  251. function Get_address ($address)
  252. {
  253. $address = Ereg_replace ("([\t\r\n]) +", "", $address);
  254. $address = Ereg_replace ("^.*< (. +) >.*$", "\1", $address);
  255. return $address;
  256. }
  257. function Smtp_debug ($message)
  258. {
  259. if ($this->debug)
  260. {
  261. Echo $message;
  262. }
  263. }
  264. }
Copy Code

Send mail, PHP, SMTP
  • 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.