方式1:使用stmp类发送邮件

代码:

stmp.php

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

View Code

sendCodeTest.php

  1. 1 <?php
  2. 2 header("Content-Type: text/html; charset=utf-8");
  3. 3
  4. 4 //引入发送邮件类
  5. 5 require("smtp.php");
  6. 6 //使用163邮箱服务器
  7. 7 $smtpserver = "smtp.163.com";
  8. 8 //163邮箱服务器端口
  9. 9 $smtpserverport = 25;
  10. 10 //你的163服务器邮箱账号
  11. 11 $smtpusermail = "18503001535@163.com";
  12. 12 //收件人邮箱
  13. 13 $smtpemailto = "244491216@qq.com";
  14. 14
  15. 15 //你的邮箱账号(去掉@163.com)
  16. 16 $smtpuser = "18503001535";//你的163邮箱去掉后面的163.com
  17. 17 //你的邮箱密码
  18. 18 $smtppass = "baomihua1234"; //你的163邮箱SMTP的授权码,千万不要填密码!!!
  19. 19
  20. 20 //邮件主题
  21. 21 $mailsubject = "测试邮件发送";
  22. 22 //邮件内容
  23. 23 $mailbody = "PHP+MySQL";
  24. 24 //邮件格式(HTML/TXT),TXT为文本邮件
  25. 25 $mailtype = "TXT";
  26. 26 //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  27. 27 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
  28. 28 //是否显示发送的调试信息
  29. 29 $smtp->debug = TRUE;
  30. 30 //发送邮件
  31. 31 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  32. 32
  33. 33 ?>

View Code

注意:

  1. 该代码得设置邮箱得授权码,163邮箱设置得地方:设置-客户端授权密码-重新授权码
  2. 使用这个发邮件,邮件可能会进入垃圾箱或者被拦截

方式2:使用socket协议发送邮件

sendMail.php

  1. <?php
  2. header("Content-Type:text/html;charset=utf8");
  3. $content = <<<\'html\'
  4. <table style="width:100%;height:100%;">
  5. <tr><td style="text-align: center;">
  6. <div style="display: inline-block; padding:30px; color:red; ">
  7. <img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"/>
  8. <h1>helloworld,这是一封测试邮件</h1>
  9. <a style="text-decoration: none; color: #ffffff" href="http://serverjs.cn" target="_blank">
  10. <div style="font-family:\'微软雅黑\';font-size: 18px; text-decoration: none; white-space: nowrap; color: #ffffff; padding-bottom: 10px; text-align: center; padding-top: 10px; padding-left: 25px; margin: 0px; padding-right: 25px; background-color: #cc0001; border-radius: 3px">马上激活 </div>
  11. </a>
  12. </div>
  13. </td></tr>
  14. </table>
  15. html;
  16. // $to = "15679700245@163.com";
  17. $to = "18503001535@163.com";
  18. echo sendMail("这是发件人", "admin@serverjs.cn", $to, "这是邮件标题", $content, true, "utf8");
  19. function sendMail($senderName, $sender, $to, $subject, $content, $isHtml = false, $encode = "utf-8")
  20. {
  21. date_default_timezone_set(\'Asia/Shanghai\');
  22. $toHostname = substr($to, strrpos($to, \'@\') + 1);
  23. if (dns_get_mx($toHostname, $mxhosts, $weight)) {
  24. // $best = array_search(min($weight), $weight);
  25. // $mxBestHost = $mxhosts[$best];
  26. $socket = $res = $errno = $errstr = null;
  27. foreach ($mxhosts as $v) {
  28. $socket = @fsockopen($v, 25, $errno, $errstr, 10);
  29. if ($socket) {
  30. $res = fgets($socket);
  31. if ($res != 220) {
  32. fclose($socket);
  33. return $res;
  34. }
  35. break;
  36. }
  37. }
  38. if (! $socket) {
  39. return "$errstr ($errno)";
  40. }
  41. $senderHost = substr($sender, strrpos($sender, \'@\') + 1);
  42. fwrite($socket, "HELO $senderHost\r\n");
  43. fflush($socket);
  44. $res = fgets($socket);
  45. if ($res != 250) {
  46. fclose($socket);
  47. return $res;
  48. }
  49. fwrite($socket, "MAIL FROM: <$sender>\r\n");
  50. fflush($socket);
  51. $res = fgets($socket);
  52. if ($res != 250) {
  53. fclose($socket);
  54. return $res;
  55. }
  56. fwrite($socket, "RCPT TO: <$to>\r\n");
  57. fflush($socket);
  58. $res = fgets($socket);
  59. if ($res != 250) {
  60. fclose($socket);
  61. return $res;
  62. }
  63. fwrite($socket, "DATA\r\n");
  64. fflush($socket);
  65. $res = fgets($socket);
  66. if ($res != 354) {
  67. fclose($socket);
  68. return $res;
  69. }
  70. fwrite($socket, "From: =?$encode?B?" . base64_encode($senderName) . "?= <$sender>\r\n");
  71. fwrite($socket, "To: $to\r\n");
  72. fwrite($socket, "Subject: =?$encode?B?" . base64_encode($subject) . "?=\r\n");
  73. fwrite($socket, "Date: " . date(DATE_RFC822) . "\r\n");
  74. fwrite($socket, "MIME-Version: 1.0\r\n");
  75. if ($isHtml) {
  76. fwrite($socket, "Content-Type: text/html; charset=\"$encode\"\r\n");
  77. } else {
  78. fwrite($socket, "Content-Type: text/plain; charset=\"$encode\"\r\n");
  79. }
  80. fwrite($socket, "Content-Transfer-Encoding: base64\r\n");
  81. fwrite($socket, "X-Priority: 3\r\n");
  82. fwrite($socket, "X-Mailer: PHP Server\r\n");
  83. fwrite($socket, "\r\n");
  84. fwrite($socket, base64_encode($content));
  85. fwrite($socket, "\r\n.\r\n");
  86. fflush($socket);
  87. $res = fgets($socket);
  88. if ($res == 550) {
  89. fclose($socket);
  90. return "邮件被拦截: $res";
  91. } elseif ($res != 250) {
  92. fclose($socket);
  93. return $res;
  94. }
  95. fwrite($socket, "QUIT\r\n");
  96. fflush($socket);
  97. $res = fgets($socket);
  98. if ($res != 221) {
  99. fclose($socket);
  100. return $res;
  101. }
  102. fclose($socket);
  103. return "发送成功";
  104. } else {
  105. return "找不到主机";
  106. }
  107. }

版权声明:本文为8013-cmf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/8013-cmf/p/9113351.html