全部产品
云市场

SMTP 之 PHP 调用示例

更新时间:2019-05-30 18:18:05

使用 PHP 通过 SMTP 协议发信

  1. <?php
  2. require 'email.class.php';
  3. $mailto='收信地址';
  4. $mailsubject="测试邮件";
  5. $mailbody='这里是邮件内容';
  6. $smtpserver = "smtpdm.aliyun.com";
  7. $smtpserverport = 25;
  8. $smtpusermail = "发信地址";
  9. // 发件人的账号,填写控制台配置的发信地址,比如xxx@xxx.com
  10. $smtpuser = "发件人账号";
  11. // 访问SMTP服务时需要提供的密码(在控制台选择发信地址进行设置)
  12. $smtppass = "***";
  13. $mailsubject = "=?UTF-8?B?" . base64_encode($mailsubject) . "?=";
  14. $mailtype = "HTML";
  15. //可选,设置回信地址
  16. $smtpreplyto = "***";
  17. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
  18. $smtp->debug = false;
  19. $cc ="";
  20. $bcc = "";
  21. $additional_headers = "";
  22. //设置发件人名称,名称用户可以自定义填写。
  23. $sender = "发件人";
  24. $smtp->sendmail($mailto,$smtpusermail, $mailsubject, $mailbody, $mailtype, $cc, $bcc, $additional_headers, $sender, $smtpreplyto);

其中email.class.php的内容需修改

$header .=”Reply-To: “.$replyToAddress.”\r\n”;//其中$replyToAddress就是参数$smtpreplyto

注意

  • email.class.php 为外部依赖,请自行在网上下载。
  • mailto:收件人邮箱地址。
  • smtpuser:完整的发信地址。

email.class.php可参考示例

  1. <?php
  2. /**
  3. * email smtp (support php7)
  4. *
  5. */
  6. class Smtp
  7. {
  8. /* Public Variables */
  9. public $smtp_port;
  10. public $time_out;
  11. public $host_name;
  12. public $log_file;
  13. public $relay_host;
  14. public $debug;
  15. public $auth;
  16. public $user;
  17. public $pass;
  18. /* Private Variables */
  19. private $sock;
  20. /* Constractor */
  21. function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  22. {
  23. $this->debug = FALSE;
  24. $this->smtp_port = $smtp_port;
  25. $this->relay_host = $relay_host;
  26. $this->time_out = 30; //is used in fsockopen()
  27. $this->auth = $auth;//auth
  28. $this->user = $user;
  29. $this->pass = $pass;
  30. $this->host_name = "localhost"; //is used in HELO command
  31. $this->log_file = "";
  32. $this->sock = FALSE;
  33. }
  34. /* Main Function */
  35. function sendmail($to, $from, $subject, $body, $mailtype, $cc, $bcc, $additional_headers, $fromUser, $replyToAddress)
  36. {
  37. $mail_from = $this->get_address($this->strip_comment($from));
  38. $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
  39. $header = "MIME-Version:1.0\r\n";
  40. if($mailtype=="HTML"){
  41. $header .= "Content-Type:text/html\r\n";
  42. }
  43. $header .= "To: ".$to."\r\n";
  44. if ($cc != "") {
  45. $header .= "Cc: ".$cc."\r\n";
  46. }
  47. $header .= "From: $fromUser<".$from.">\r\n";
  48. $header .= "Subject: ".$subject."\r\n";
  49. $header .= "Reply-To: ".$replyToAddress."\r\n";
  50. $header .= $additional_headers;
  51. $header .= "Date: ".date("r")."\r\n";
  52. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  53. list($msec, $sec) = explode(" ", microtime());
  54. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
  55. $TO = explode(",", $this->strip_comment($to));
  56. if ($cc != "") {
  57. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  58. }
  59. if ($bcc != "") {
  60. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  61. }
  62. $sent = TRUE;
  63. foreach ($TO as $rcpt_to) {
  64. $rcpt_to = $this->get_address($rcpt_to);
  65. if (!$this->smtp_sockopen($rcpt_to)) {
  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. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  72. } else {
  73. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  74. $sent = FALSE;
  75. }
  76. fclose($this->sock);
  77. $this->log_write("Disconnected from remote host\n");
  78. }
  79. return $sent;
  80. }
  81. /* Private Functions */
  82. function smtp_send($helo, $from, $to, $header, $body = "")
  83. {
  84. if (!$this->smtp_putcmd("HELO", $helo)) {
  85. return $this->smtp_error("sending HELO command");
  86. }
  87. //auth
  88. if($this->auth){
  89. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  90. return $this->smtp_error("sending HELO command");
  91. }
  92. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  93. return $this->smtp_error("sending HELO command");
  94. }
  95. }
  96. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
  97. return $this->smtp_error("sending MAIL FROM command");
  98. }
  99. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
  100. return $this->smtp_error("sending RCPT TO command");
  101. }
  102. if (!$this->smtp_putcmd("DATA")) {
  103. return $this->smtp_error("sending DATA command");
  104. }
  105. if (!$this->smtp_message($header, $body)) {
  106. return $this->smtp_error("sending message");
  107. }
  108. if (!$this->smtp_eom()) {
  109. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  110. }
  111. if (!$this->smtp_putcmd("QUIT")) {
  112. return $this->smtp_error("sending QUIT command");
  113. }
  114. return TRUE;
  115. }
  116. function smtp_sockopen($address)
  117. {
  118. if ($this->relay_host == "") {
  119. return $this->smtp_sockopen_mx($address);
  120. } else {
  121. return $this->smtp_sockopen_relay();
  122. }
  123. }
  124. function smtp_sockopen_relay()
  125. {
  126. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  127. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  128. if (!($this->sock && $this->smtp_ok())) {
  129. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  130. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  131. return FALSE;
  132. }
  133. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  134. return TRUE;
  135. }
  136. function smtp_sockopen_mx($address)
  137. {
  138. $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
  139. if (!@getmxrr($domain, $MXHOSTS)) {
  140. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  141. return FALSE;
  142. }
  143. foreach ($MXHOSTS as $host) {
  144. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  145. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  146. if (!($this->sock && $this->smtp_ok())) {
  147. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  148. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  149. continue;
  150. }
  151. $this->log_write("Connected to mx host ".$host."\n");
  152. return TRUE;
  153. }
  154. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  155. return FALSE;
  156. }
  157. function smtp_message($header, $body)
  158. {
  159. fputs($this->sock, $header."\r\n".$body);
  160. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  161. return TRUE;
  162. }
  163. function smtp_eom()
  164. {
  165. fputs($this->sock, "\r\n.\r\n");
  166. $this->smtp_debug(". [EOM]\n");
  167. return $this->smtp_ok();
  168. }
  169. function smtp_ok()
  170. {
  171. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  172. $this->smtp_debug($response."\n");
  173. if (!preg_match("/^[23]/", $response)) {
  174. fputs($this->sock, "QUIT\r\n");
  175. fgets($this->sock, 512);
  176. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  177. return FALSE;
  178. }
  179. return TRUE;
  180. }
  181. function smtp_putcmd($cmd, $arg = "")
  182. {
  183. if ($arg != "") {
  184. if($cmd=="") $cmd = $arg;
  185. else $cmd = $cmd." ".$arg;
  186. }
  187. fputs($this->sock, $cmd."\r\n");
  188. $this->smtp_debug("> ".$cmd."\n");
  189. return $this->smtp_ok();
  190. }
  191. function smtp_error($string)
  192. {
  193. $this->log_write("Error: Error occurred while ".$string.".\n");
  194. return FALSE;
  195. }
  196. function log_write($message)
  197. {
  198. $this->smtp_debug($message);
  199. if ($this->log_file == "") {
  200. return TRUE;
  201. }
  202. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  203. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  204. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  205. return FALSE;
  206. }
  207. flock($fp, LOCK_EX);
  208. fputs($fp, $message);
  209. fclose($fp);
  210. return TRUE;
  211. }
  212. function strip_comment($address)
  213. {
  214. $comment = "/\([^()]*\)/";
  215. while (preg_match($comment, $address)) {
  216. $address = preg_replace($comment, "", $address);
  217. }
  218. return $address;
  219. }
  220. function get_address($address)
  221. {
  222. $address = preg_replace("/([ \t\r\n])+/", "", $address);
  223. $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
  224. return $address;
  225. }
  226. function smtp_debug($message)
  227. {
  228. if ($this->debug) {
  229. echo $message;
  230. }
  231. }
  232. }