多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > php教程 > php中使用smtp類輕松的發(fā)送電子郵件示例

php中使用smtp類輕松的發(fā)送電子郵件示例

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-01-18 02:37:00 閱讀次數(shù):3602次

smtp發(fā)送郵箱個(gè)人覺(jué)得比php mail函數(shù)要實(shí)用的多了,mail函數(shù)不是隨便可以發(fā)郵箱的需要進(jìn)行相關(guān)配置哦,下面我們來(lái)看一個(gè)關(guān)于smtp類發(fā)送郵箱與問(wèn)題解決方法。

當(dāng)你還在糾結(jié)php內(nèi)置的mail()函數(shù)不能發(fā)送郵件時(shí),那么你現(xiàn)在很幸運(yùn),此時(shí)的這篇文章可以幫助到你!

php利用smtp類來(lái)發(fā)郵件真是屢試不爽,我用過(guò)很久了,基本上沒(méi)出過(guò)問(wèn)題。本博客后臺(tái),當(dāng)博主回復(fù)留言時(shí)候,會(huì)自動(dòng)給網(wǎng)友發(fā)一封有新回復(fù)提示的郵件也是用的本文這個(gè)方法實(shí)現(xiàn)的。

smtp類發(fā)送郵件的方法其實(shí)很簡(jiǎn)單,也很穩(wěn)定,類是別人已經(jīng)寫(xiě)好的了,你只需要調(diào)用就行了。幾行簡(jiǎn)單的配置就能發(fā)郵件,是不是很期待的試一試呢!

email.class.php文件,代碼如下:

  1. <?php  
  2.  class smtp  
  3.  
  4. {  
  5.  
  6. /* Public Variables */ 
  7.  
  8. var $smtp_port;  
  9.  
  10. var $time_out;  
  11.  
  12. var $host_name;  
  13.  
  14. var $log_file;  
  15.  
  16. var $relay_host;  
  17.  
  18. var $debug;  
  19.  
  20. var $auth;  
  21.  
  22. var $user;  
  23.  
  24. var $pass;  
  25.  
  26. /* Private Variables */ 
  27.  var $sock;  
  28.  
  29. /* Constractor */ 
  30.  
  31. function smtp($relay_host = ""$smtp_port = 25,$auth = false,$user,$pass)  
  32.  
  33. {  
  34.  
  35. $this->debug = FALSE;  
  36.  
  37. $this->smtp_port = $smtp_port;  
  38.  
  39. $this->relay_host = $relay_host;  
  40.  
  41. $this->time_out = 30; //is used in fsockopen()  
  42.  #  
  43.  
  44. $this->auth = $auth;//auth  
  45.  
  46. $this->user = $user;  
  47.  
  48. $this->pass = $pass;  
  49.  
  50. #  
  51.  
  52. $this->host_name = "localhost"//is used in HELO command  
  53.  $this->log_file = "";  
  54.  
  55. $this->sock = FALSE;  
  56.  
  57. }  
  58.  
  59. /* Main Function */ 
  60.  
  61. function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = "")  
  62.  
  63. {  
  64.  
  65. $mail_from = $this->get_address($this->strip_comment($from));  
  66.  
  67. $body = ereg_replace("(^|(rn))(.)""1.3"$body);  
  68.  
  69. $header = "MIME-Version:1.0rn";  
  70.  
  71. if($mailtype=="HTML"){  
  72.  
  73. $header .= "Content-Type:text/htmlrn";  
  74.  
  75. }  
  76.  
  77. $header .= "To: ".$to."rn";  
  78.  
  79. if ($cc != "") {  
  80.  
  81. $header .= "Cc: ".$cc."rn";  
  82.  
  83. }  
  84.  
  85. $header .= "From: $from<".$from.">rn";  
  86.  
  87. $header .= "Subject: ".$subject."rn";  
  88.  
  89. $header .= $additional_headers;  
  90.  
  91. $header .= "Date: ".date("r")."rn";  
  92.  
  93. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";  
  94.  
  95. list($msec$sec) = explode(" ", microtime());  
  96.  
  97. $header .= "Message-ID: <".date("YmdHis"$sec).".".($msec*1000000).".".$mail_from.">rn";  
  98.  
  99. $TO = explode(","$this->strip_comment($to));  
  100.  
  101. if ($cc != "") {  
  102.  
  103. $TO = array_merge($TOexplode(","$this->strip_comment($cc)));  
  104.  
  105. }  
  106.  
  107. if ($bcc != "") {  
  108.  
  109. $TO = array_merge($TOexplode(","$this->strip_comment($bcc)));  
  110.  
  111. }  
  112.  
  113. $sent = TRUE;  
  114.  
  115. foreach ($TO as $rcpt_to) {  
  116.  
  117. $rcpt_to = $this->get_address($rcpt_to);  
  118.  
  119. if (!$this->smtp_sockopen($rcpt_to)) {  
  120.  
  121. $this->log_write("Error: Cannot send email to ".$rcpt_to."n");  
  122.  
  123. $sent = FALSE;  
  124.  
  125. continue;  
  126.  
  127. }  
  128.  
  129. if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) {  
  130.  
  131. $this->log_write("E-mail has been sent to <".$rcpt_to.">n");  
  132.  
  133. else {  
  134.  
  135. $this->log_write("Error: Cannot send email to <".$rcpt_to.">n");  
  136.  
  137. $sent = FALSE;  
  138.  
  139. }  
  140.  
  141. fclose($this->sock);  
  142.  
  143. $this->log_write("Disconnected from remote hostn");  
  144.  
  145. }  
  146.  
  147. return $sent;  
  148.  
  149. }  
  150.  
  151. /* Private Functions */ 
  152.  
  153. function smtp_send($helo$from$to$header$body = "")  
  154.  
  155. {  
  156.  
  157. if (!$this->smtp_putcmd("HELO"$helo)) {  
  158.  
  159. return $this->smtp_error("sending HELO command");  
  160.  
  161. }  
  162.  
  163. #auth  
  164.  
  165. if($this->auth){  
  166.  
  167. if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) {  
  168.  
  169. return $this->smtp_error("sending HELO command");  
  170.  
  171. }  
  172.  
  173. if (!$this->smtp_putcmd(""base64_encode($this->pass))) {  
  174.  
  175. return $this->smtp_error("sending HELO command");  
  176.  
  177. }  
  178.  
  179. }  
  180.  
  181. #  
  182.  
  183. if (!$this->smtp_putcmd("MAIL""FROM:<".$from.">")) {  
  184.  
  185. return $this->smtp_error("sending MAIL FROM command");  
  186.  
  187. }  
  188.  
  189. if (!$this->smtp_putcmd("RCPT""TO:<".$to.">")) {  
  190.  
  191. return $this->smtp_error("sending RCPT TO command");  
  192.  
  193. }  
  194.  
  195. if (!$this->smtp_putcmd("DATA")) {  
  196.  
  197. return $this->smtp_error("sending DATA command");  
  198.  
  199. }  
  200.  
  201. if (!$this->smtp_message($header$body)) {  
  202.  
  203. return $this->smtp_error("sending message");  
  204.  
  205. }  
  206.  
  207. if (!$this->smtp_eom()) {  
  208.  
  209. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");  
  210.  
  211. }  
  212.  
  213. if (!$this->smtp_putcmd("QUIT")) {  
  214.  
  215. return $this->smtp_error("sending QUIT command");  
  216.  
  217. }  
  218.  
  219. return TRUE;  
  220.  
  221. }  
  222.  
  223. function smtp_sockopen($address)  
  224.  
  225. {  
  226.  
  227. if ($this->relay_host == "") {  
  228.  
  229. return $this->smtp_sockopen_mx($address);  
  230.  
  231. else {  
  232.  
  233. return $this->smtp_sockopen_relay();  
  234.  
  235. }  
  236.  
  237. }  
  238.  
  239. function smtp_sockopen_relay()  
  240.  
  241. {  
  242.  
  243. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");  
  244.  
  245. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out);  
  246.  
  247. if (!($this->sock && $this->smtp_ok())) {  
  248.  
  249. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");  
  250.  
  251. $this->log_write("Error: ".$errstr." (".$errno.")n");  
  252.  
  253. return FALSE;  
  254.  
  255. }  
  256.  
  257. $this->log_write("Connected to relay host ".$this->relay_host."n");  
  258.  
  259. return TRUE;;  
  260.  
  261. }  
  262.  
  263. function smtp_sockopen_mx($address)  
  264.  
  265. {  
  266.  
  267. $domain = ereg_replace("^.+@([^@]+)$""1"$address);  
  268.  
  269. if (!@getmxrr($domain$MXHOSTS)) {  
  270.  
  271. $this->log_write("Error: Cannot resolve MX "".$domain.""n");  
  272.  
  273. return FALSE;  
  274.  
  275. }  
  276.  //專注與php學(xué)習(xí) http://www.daixiaorui.com 歡迎您的訪問(wèn)  
  277.  
  278. foreach ($MXHOSTS as $host) {  
  279.  
  280. $this->log_write("Trying to ".$host.":".$this->smtp_port."n");  
  281.  
  282. $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out);  
  283.  
  284. if (!($this->sock && $this->smtp_ok())) {  
  285.  
  286. $this->log_write("Warning: Cannot connect to mx host ".$host."n");  
  287.  
  288. $this->log_write("Error: ".$errstr." (".$errno.")n");  
  289.  
  290. continue;  
  291.  
  292. }  
  293.  
  294. $this->log_write("Connected to mx host ".$host."n");  
  295.  
  296. return TRUE;  
  297.  
  298. }  
  299.  
  300. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", "$MXHOSTS).")n");  
  301.  
  302. return FALSE;  
  303.  
  304. }  
  305.  
  306. function smtp_message($header$body)  
  307.  
  308. {  
  309.  
  310. fputs($this->sock, $header."rn".$body);  
  311.  
  312. $this->smtp_debug("> ".str_replace("rn""n"."> "$header."n> ".$body."n> "));  
  313.  
  314. return TRUE;  
  315.  
  316. }  
  317.  
  318. function smtp_eom()  
  319.  
  320. {  
  321.  
  322. fputs($this->sock, "rn.rn");  
  323.  
  324. $this->smtp_debug(". [EOM]n");  
  325.  
  326. return $this->smtp_ok();  
  327.  
  328. }  
  329.  
  330. function smtp_ok()  
  331.  
  332. {  
  333.  
  334. $response = str_replace("rn"""fgets($this->sock, 512));  
  335.  
  336. $this->smtp_debug($response."n");  
  337.  
  338. if (!ereg("^[23]"$response)) {  
  339.  
  340. fputs($this->sock, "QUITrn");  
  341.  
  342. fgets($this->sock, 512);  
  343.  
  344. $this->log_write("Error: Remote host returned "".$response.""n");  
  345.  
  346. return FALSE;  
  347.  
  348. }  
  349.  
  350. return TRUE;  
  351.  
  352. }  
  353.  
  354. function smtp_putcmd($cmd$arg = "")  
  355.  
  356. {  
  357.  
  358. if ($arg != "") {  
  359.  
  360. if($cmd==""$cmd = $arg;  
  361.  
  362. else $cmd = $cmd." ".$arg;  
  363.  
  364. }  
  365.  
  366. fputs($this->sock, $cmd."rn");  
  367.  
  368. $this->smtp_debug("> ".$cmd."n");  
  369.  
  370. return $this->smtp_ok();  
  371.  
  372. }  
  373.  
  374. function smtp_error($string)  
  375.  
  376. {  
  377.  
  378. $this->log_write("Error: Error occurred while ".$string.".n");  
  379.  
  380. return FALSE;  
  381.  
  382. }  
  383.  
  384. function log_write($message)  
  385.  
  386. {  
  387.  
  388. $this->smtp_debug($message);  
  389.  
  390. if ($this->log_file == "") {  
  391.  
  392. return TRUE;  
  393.  
  394. }  
  395.  
  396. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;  
  397.  
  398. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {  
  399.  
  400. $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");  
  401.  
  402. return FALSE;;  
  403.  
  404. }  
  405.  
  406. flock($fp, LOCK_EX);  
  407.  
  408. fputs($fp$message);  
  409.  
  410. fclose($fp);  
  411.  
  412.  
  413.  return TRUE;  
  414.  
  415. }  
  416.  
  417.  
  418.  function strip_comment($address)  
  419.  
  420. {  
  421.  
  422. $comment = "([^()]*)";  
  423.  
  424. while (ereg($comment$address)) {  
  425.  
  426. $address = ereg_replace($comment""$address);  
  427.  
  428. }  
  429.  
  430.  
  431.  return $address;  
  432.  
  433. }  
  434.  
  435.  
  436.  function get_address($address)  
  437.  
  438. {  
  439.  
  440. $address = ereg_replace("([ trn])+"""$address);  
  441.  
  442. $address = ereg_replace("^.*<(.+)>.*$""1"$address);  
  443.  
  444. return $address;  
  445.  
  446. }  
  447.  
  448. function smtp_debug($message)  
  449.  
  450. {  
  451.  
  452. if ($this->debug) {  
  453.  
  454. echo $message;  
  455.  
  456. }  
  457.  
  458. }  
  459.  
  460. }  
  461.  
  462. ?>  

發(fā)送郵箱的php文件,代碼如下:

  1. <?php  
  2.  
  3.  require_once "email.class.php";  
  4.  
  5.  //******************** 配置信息 ********************************  
  6.  
  7.  $smtpserver = "smtp.126.com";//SMTP服務(wù)器  
  8.  
  9.  $smtpserverport =25;//SMTP服務(wù)器端口  
  10.  
  11.  $smtpusermail = "new2008oh@126.com";//SMTP服務(wù)器的用戶郵箱  
  12.  
  13.  $smtpemailto = $_POST['toemail'];//發(fā)送給誰(shuí)  
  14.  
  15.  $smtpuser = "new2008oh";//SMTP服務(wù)器的用戶帳號(hào)  
  16.  
  17.  $smtppass = "您的郵箱密碼";//SMTP服務(wù)器的用戶密碼  
  18.  
  19.  $mailtitle = $_POST['title'];//郵件主題  
  20.  
  21.  $mailcontent = "<h1>".$_POST['content']."</h1>";//郵件內(nèi)容  
  22.  
  23.  $mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件  
  24.  
  25.  //************************ 配置信息 ****************************  
  26.  
  27.  $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這里面的一個(gè)true是表示使用身份驗(yàn)證,否則不使用身份驗(yàn)證.  
  28.  
  29.  $smtp->debug = false;//是否顯示發(fā)送的調(diào)試信息  
  30.  
  31.  $state = $smtp->sendmail($smtpemailto$smtpusermail$mailtitle$mailcontent$mailtype);  
  32.  
  33.  
  34.   echo "<div style='width:300px; margin:36px auto;'>";  
  35.  
  36.  if($state==""){  
  37.  
  38.   echo "對(duì)不起,郵件發(fā)送失敗!請(qǐng)檢查郵箱填寫(xiě)是否有誤。";  
  39.  
  40.   echo "<a href='index.html'>點(diǎn)此返回</a>";  
  41.  
  42.   exit();  
  43.  
  44.  }  
  45.  echo "恭喜!郵件發(fā)送成功!!";  
  46.  echo "<a href='index.html'>點(diǎn)此返回</a>";  
  47.  echo "</div>";  
  48. ?> 

smtp類無(wú)法發(fā)送郵件解決方法,偶然發(fā)現(xiàn)我網(wǎng)站后臺(tái)自動(dòng)發(fā)送郵件功能不能用了,報(bào)這個(gè)錯(cuò)誤:

Trying to smtp.126.com:25 Error: Cannot connenct to relay host smtp.126.com Error: () Error: Cannot send email to web@daixiaorui.com state

大概意思是:無(wú)法connenct中繼主機(jī)smtp.126.com 錯(cuò)誤:()錯(cuò)誤:無(wú)法發(fā)送電子郵件給web@daixiaorui.com

上網(wǎng)找了n多資料后終于找到了解決方案,不是smtp類的問(wèn)題,而就是linux配置的問(wèn)題。原來(lái)是服務(wù)器的php.ini 禁用了fsockopen函數(shù)。

打開(kāi)空間下的php.ini文件,linux空間一般都可以自定義php.ini,所以根目錄下面一般會(huì)有這個(gè)文件。

有兩個(gè)地方可能禁用此函數(shù):

1. allow_url_fopen = On 查看等于后面是否為 ON,如果為OFF時(shí)函數(shù)將被禁用

2. disable_functions = fsockopen pfsockopen (我的就是這樣)這里應(yīng)該去掉前面的“fsockopen”。使之變成:disable_functions = pfsockopen

改過(guò)之后,保存,再重新刷新頁(yè)面,就發(fā)現(xiàn)在linux下能成功利用smtp類發(fā)送電子郵件了。感謝網(wǎng)友分享的方法,問(wèn)題終于得到了解決。

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 最近的中文字幕免费视频1 最近的中文字幕免费完整 最近的中文字幕视频大全高清 | 美国伊人网 | 精品第一页 | 暮色大丽菊 | 久久精品一品道久久精品9 久久精品一区二区 | 久久国产精品久久久久久久久久 | 在线亚洲不卡 | 性福利视频 | 亚洲高清在线视频 | 国产精品久久久久一区二区三区 | 成人性欧美丨区二区三区 | 欧美一区二区在线免费观看 | 最近中文字幕无免费视频 | 欧美a色 | 午夜影院啪啪 | 国产精品网站 夜色 | 欧美 日韩 亚洲另类专区 | 日韩v片| 精久久| 中文字幕免费视频精品一 | 亚洲 欧美 小说 国产 图片 | 日本高清www视频在线观看 | 久久精品隔壁老王影院 | 色网站综合 | 中文字幕一区二区三区精彩视频 | 中文字幕3 | 鲁在线 | 中文字幕日韩一区 | 黄色在线网站视频 | 亚洲精品欧美精品国产精品 | 中文亚洲日韩欧美 | 69热精品视频在线看影院 | 日本大片免费播放网站 | 18jzz大全中文 | 亚洲国产成人久久综合区 | 欧美在线一区二区三区不卡 | 久久中国| 99久久精品毛片免费播放 | haodiaose在线精品免费视频 | 欧美日韩一区二区三区视频在线观看 | 最近最新高清免费中文字幕 |