当前位置:Gxlcms > PHP教程 > php实现在站点里面添加邮件发送的功能_php实例

php实现在站点里面添加邮件发送的功能_php实例

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

下面夏日博客来讲下如何在站点里面添加一个邮件发送的功能。
首先需要下载一个smtp 的 php 邮件发送类,代码如下:

  1. <?php
  2. class smtp
  3. {
  4. /* Public Variables */
  5. public $smtp_port;
  6. public $time_out;
  7. public $host_name;
  8. public $log_file;
  9. public $relay_host;
  10. public $debug;
  11. public $auth;
  12. public $user;
  13. public $pass;
  14. /* Private Variables */
  15. private $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. #
  24. $this->auth = $auth;//auth
  25. $this->user = $user;
  26. $this->pass = $pass;
  27. #
  28. $this->host_name = "localhost"; //is used in HELO command
  29. $this->log_file ="";
  30. $this->sock = FALSE;
  31. }
  32. /* Main Function */
  33. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  34. {
  35. $mail_from = $this->get_address($this->strip_comment($from));
  36. $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body);
  37. $header .= "MIME-Version:1.0\r\n";
  38. if($mailtype=="HTML"){
  39. $header .= "Content-Type:text/html\r\n";
  40. }
  41. $header .= "To: ".$to."\r\n";
  42. if ($cc != "") {
  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. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  55. }
  56. if ($bcc != "") {
  57. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  58. }
  59. $sent = TRUE;
  60. foreach ($TO as $rcpt_to) {
  61. $rcpt_to = $this->get_address($rcpt_to);
  62. if (!$this->smtp_sockopen($rcpt_to)) {
  63. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  64. $sent = FALSE;
  65. continue;
  66. }
  67. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  68. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  69. } else {
  70. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  71. $sent = FALSE;
  72. }
  73. fclose($this->sock);
  74. $this->log_write("Disconnected from remote host\n");
  75. }
  76. echo "<br>";
  77. //echo $header;
  78. return $sent;
  79. }
  80. /* Private Functions */
  81. function smtp_send($helo, $from, $to, $header, $body = "")
  82. {
  83. if (!$this->smtp_putcmd("HELO", $helo)) {
  84. return $this->smtp_error("sending HELO command");
  85. }
  86. #auth
  87. if($this->auth){
  88. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  92. return $this->smtp_error("sending HELO command");
  93. }
  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."<br>";
  230. }
  231. }
  232. function get_attach_type($image_tag) { //
  233. $filedata = array();
  234. $img_file_con=fopen($image_tag,"r");
  235. unset($image_data);
  236. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
  237. $image_data.=$tem_buffer;
  238. fclose($img_file_con);
  239. $filedata['context'] = $image_data;
  240. $filedata['filename']= basename($image_tag);
  241. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
  242. switch($extension){
  243. case ".gif":
  244. $filedata['type'] = "image/gif";
  245. break;
  246. case ".gz":
  247. $filedata['type'] = "application/x-gzip";
  248. break;
  249. case ".htm":
  250. $filedata['type'] = "text/html";
  251. break;
  252. case ".html":
  253. $filedata['type'] = "text/html";
  254. break;
  255. case ".jpg":
  256. $filedata['type'] = "image/jpeg";
  257. break;
  258. case ".tar":
  259. $filedata['type'] = "application/x-tar";
  260. break;
  261. case ".txt":
  262. $filedata['type'] = "text/plain";
  263. break;
  264. case ".zip":
  265. $filedata['type'] = "application/zip";
  266. break;
  267. default:
  268. $filedata['type'] = "application/octet-stream";
  269. break;
  270. }
  271. return $filedata;
  272. }
  273. } // end class
  274. ?>
  275. </lf></cr></lf></cr>

这类的邮件发送类网上有许多,可以挑一款自己喜欢的就行,将以上的代码保存为 ZC_Email.class.php 文件,然后在使用的时候直接 include_once("ZC_Email.class.php") 就可以了,代码如下:

  1. <?php
  2. include_once("ZC_Email.class.php");
  3. ?>

这是将邮件的发送类已经嵌入到页面中了,下一步就要在页面进行内容的发送了,我这里的系统是用户购买成功后进行发送,实例代码如下:

  1. //空间购买成功发送邮件
  2. $smtpserver = "smtp.163.com";//SMTP服务器
  3. $smtpserverport =25;//SMTP服务器端口
  4. $smtpusermail = "xiariboke@163.com";//SMTP服务器的用户邮箱
  5. $smtpemailto = "xiariboke@qq.com";//发送给谁
  6. $smtpuser = "xiariboke@163.com";//SMTP服务器的用户帐号
  7. $smtppass = "xiariboke";//SMTP服务器的用户密码
  8. $mailsubject = "精品门业网香港空间购买";//邮件主题
  9. $mailbody = "成功购买香港空间 $size M空间,购买域名为:$domain 购买时间为:".format_date(time(),2)." 到期时间为: $lasttime ".format_date(time(),5);//邮件内容
  10. $mailtype = "TXT";//邮件格式(HTML/TXT),TXT为文本邮件
  11. ##########################################
  12. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  13. $smtp->debug = false;//是否显示发送的调试信息
  14. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  15. //邮件发送结束
  16. showmsg('购买成功!','ZC_Link_List.php?sort=2');

其中 SMTP 服务器的用户邮箱和密码自己要设置好,这是发给指定的一个邮箱,当然可以自定义改成自己的,这里面的变量都是我站点内使用的,如果不需要可以自己稍微修改一下,这里不多讲了。
希望本文所述对大家学习php程序设计有所帮助。

人气教程排行