Home  >  Article  >  Backend Development  >  php smtp send email

php smtp send email

WBOY
WBOYOriginal
2016-07-25 08:42:111182browse

php needs to pass a class smtp to send emails through smtp

The code for sending emails 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 ";//The user email of the SMTP server
  6. $smtpemailto = "45323333@qq.com";//Who to send it to
  7. $smtpuser = "name";//The user account of the SMTP server
  8. $smtppass = "123456";/ /SMTP server user password
  9. $mailsubject = "Chinese";//Email subject
  10. $mailbody = "

    Chinese

    Testing to create a sense of New Year";//Email content
  11. $mailtype = " HTML";//Email format (HTML/TXT), TXT is text email
  12. ############################### ##########
  13. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//A true here means that authentication is used, otherwise authentication is not used .
  14. $smtp->debug = FALSE;//Whether to display the sent debugging information
  15. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
Copy code



The source code of 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 = 25,$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("(^|(rn))(.)", "1.3", $body);
  34. $header = "MIME-Version:1.0rn";
  35. if($mailtype=="HTML")
  36. {
  37. $header .= "Content-Type:text/htmlrn";
  38. }
  39. $header .= "To: ".$to."rn";
  40. if ($cc != "")
  41. {
  42. $header .= "Cc: ".$cc."rn";
  43. }
  44. $header .= "From: $from<".$from.">rn";
  45. $header .= "Subject: ".$subject."rn";
  46. $header .= $additional_headers;
  47. $header .= "Date: ".date("r")."rn";
  48. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
  49. list($msec, $sec) = explode(" ", microtime());
  50. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
  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 hostn");
  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."rn".$body);
  180. $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  181. return TRUE;
  182. }
  183. function smtp_eom()
  184. {
  185. fputs($this->sock, "rn.rn");
  186. $this->smtp_debug(". [EOM]n");
  187. return $this->smtp_ok();
  188. }
  189. function smtp_ok()
  190. {
  191. $response = str_replace("rn", "", fgets($this->sock, 512));
  192. $this->smtp_debug($response."n");
  193. if (!ereg("^[23]", $response))
  194. {
  195. fputs($this->sock, "QUITrn");
  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."rn");
  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("([ trn])+", "", $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. }
复制代码


发送邮件, php, smtp


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
Previous article:php curl upload fileNext article:php curl upload file