当前位置:Gxlcms > PHP教程 > PHP演示如何发送邮件给某个邮箱

PHP演示如何发送邮件给某个邮箱

时间:2021-07-01 10:21:17 帮助过:15人阅读

首先,建立一个index.html文件,代码如下:

  1. <html>
  2. <head>
  3. <title>Simple Send Mail </title>
  4. </head>
  5. <body>
  6. <h1>Mail Form</h1>
  7. <form name="form1" method="post" action="mail.php">
  8. <table>
  9. <tr><td><b>To</b></td><td>
  10. <input type="text" name="mailto" size="35">
  11. </td></tr>
  12. <tr><td><b>Subject</b></td>
  13. <td><input type="text" name="mailsubject" size="35"></td>
  14. </tr>
  15. <tr><td><b>Message</b></td>
  16. <td>
  17. <textarea name="mailbody" cols="50" rows="7"></textarea>
  18. </td>
  19. </tr>
  20. <tr><td colspan="2">
  21. <input type="submit" name="Submit" value="Send">
  22. </td>
  23. </tr>
  24. </table>
  25. </form>
  26. </body>
  27. </html>

然后新建一个“mail.php”文档把传输的文档进行发送

  1. <?php
  2. $stm="邮件内容";
  3. require("smtp.php");
  4. ##########################################
  5. $smtpserver = "smtp.qq.com";//SMTP服务器
  6. $smtpserverport = "465";//SMTP服务器端口
  7. $smtpusermail = "XXX@qq.com";//SMTP服务器的用户邮箱
  8. $smtpemailto = "AAA@qq.com";//发送给谁
  9. $smtpuser = "XXX@qq.com";//SMTP服务器的用户帐号
  10. $smtppass = "666";//SMTP服务器的用户密码
  11. $mailsubject = "666 ";//邮件主题
  12. $mailbody = $stm;//邮件内容
  13. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  14. ##########################################
  15. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  16. $smtp->debug = TRUE;//是否显示发送的调试信息
  17. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  18. echo "<script>alert('邮件发送成功');parent.document.ADDUser.cheheh.click();</script>";
  19. exit;
  20. }
  21. ?>

最后编写一个邮件类“smtp.php”


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

相关学习推荐:PHP编程从入门到精通

以上就是PHP演示如何发送邮件给某个邮箱的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行