最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php中使用smtp类轻松的发送电子邮件示例

时间:2014-06-09 16:40来源: 作者: 点击:
smtp发送邮箱个人觉得比php mail函数要实用的多了,mail函数不是随便可以发邮箱的需要进行相关配置哦,下面我们来看一个关于smtp类发送邮箱与问题解决方法。 当你还在纠结php内置的mail()函数

smtp发送邮箱个人觉得比php mail函数要实用的多了,mail函数不是随便可以发邮箱的需要进行相关配置哦,下面我们来看一个关于smtp类发送邮箱与问题解决方法。

当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你!

php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题。本博客后台,当博主回复留言时候,会自动给网友发一封有新回复提示的邮件也是用的本文这个方法实现的。

smtp类发送邮件的方法其实很简单,也很稳定,类是别人已经写好的了,你只需要调用就行了。几行简单的配置就能发邮件,是不是很期待的试一试呢!

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学习 http://www.daixiaorui.com 欢迎您的访问  
  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. ?>  

发送邮箱的php文件,代码如下:

  1. <?php  
  2.  
  3.  require_once "email.class.php";  
  4.  
  5.  //******************** 配置信息 ********************************  
  6.  
  7.  $smtpserver = "smtp.126.com";//SMTP服务器  
  8.  
  9.  $smtpserverport =25;//SMTP服务器端口  
  10.  
  11.  $smtpusermail = "new2008oh@126.com";//SMTP服务器的用户邮箱  
  12.  
  13.  $smtpemailto = $_POST['toemail'];//发送给谁  
  14.  
  15.  $smtpuser = "new2008oh";//SMTP服务器的用户帐号  
  16.  
  17.  $smtppass = "您的邮箱密码";//SMTP服务器的用户密码  
  18.  
  19.  $mailtitle = $_POST['title'];//邮件主题  
  20.  
  21.  $mailcontent = "<h1>".$_POST['content']."</h1>";//邮件内容  
  22.  
  23.  $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件  
  24.  
  25.  //************************ 配置信息 ****************************  
  26.  
  27.  $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.  
  28.  
  29.  $smtp->debug = false;//是否显示发送的调试信息  
  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 "对不起,邮件发送失败!请检查邮箱填写是否有误。";  
  39.  
  40.   echo "<a href='index.html'>点此返回</a>";  
  41.  
  42.   exit();  
  43.  
  44.  }  
  45.  echo "恭喜!邮件发送成功!!";  
  46.  echo "<a href='index.html'>点此返回</a>";  
  47.  echo "</div>";  
  48. ?> 

smtp类无法发送邮件解决方法,偶然发现我网站后台自动发送邮件功能不能用了,报这个错误:

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

大概意思是:无法connenct中继主机smtp.126.com 错误:()错误:无法发送电子邮件给

上网找了n多资料后终于找到了解决方案,不是smtp类的问题,而就是linux配置的问题。原来是服务器的php.ini 禁用了fsockopen函数。

打开空间下的php.ini文件,linux空间一般都可以自定义php.ini,所以根目录下面一般会有这个文件。

有两个地方可能禁用此函数:

1. allow_url_fopen = On 查看等于后面是否为 ON,如果为OFF时函数将被禁用

2. disable_functions = fsockopen pfsockopen (我的就是这样)这里应该去掉前面的“fsockopen”。使之变成:disable_functions = pfsockopen

改过之后,保存,再重新刷新页面,就发现在linux下能成功利用smtp类发送电子邮件了。感谢网友分享的方法,问题终于得到了解决。

(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容