phpmailer发邮件常见的一些问题总结

来源:互联网 发布:事件提醒软件知乎 编辑:程序博客网 时间:2024/06/16 04:33
    这几天做mail群发,碰到不少问题。一些常见的错误网上很多但没有答案,靠自己不断的尝试终于OK了~这里把几个常见的问题列出来做为工作笔记!
前提条件 请确定当前环境如果是局域网内的服务器的话,dns 要正确的地址 要在本地试过autlook 能收发邮件。
     要做发送邮件功能,首先要明白邮件收发的原理,引用网友一段话比较容易懂:
Java代码  收藏代码
  1. 在Internet上将一段文本信息从一台计算机传送到另一台计算机上,可通过两种协议来完成,即SMTP(Simple Mail Transfer Protocol,简单邮件传输协议)和POP3(Post Office Protocol,邮局协议3)。SMTP是Internet协议集中的邮件标准。在Internet上能够接收电子邮件的服务器都有SMTP。电子邮件在发送前,发件方的SMTP服务器与接收方的SMTP服务器联系,确认接收方准备好了,则开始邮件传递;若没有准备好,发送服务器便会等待,并在一段时间后继续与接收方邮件服务器联系。这种方式在Internet上称为“存储——转发”方式。POP3可允许E-mail客户向某一SMTP服务器发送电子邮件,另外,也可以接收来自SMTP服务器的电子邮件。换句话说,电子邮件在客户PC机与服务提供商之间的传递是通过P0P3来完成的,而电子邮件在 Internet上的传递则是通过SMTP来实现。  

  如果觉得不够清楚的话,则引用网上的一张图来解释吧:

  有关phpmailer的介绍可以参考官网:http://phpmailer.codeworxtech.com/
  常见异常:
  1.SMTP Error: Could not authenticate.
   这个是因为smtp验证没通过,就是smtp server 的用户名和密码不正确了
   
Php代码  收藏代码
  1. $mail->Username   = "smtp@163.com";     // SMTP server username  
  2. t;Password   = "******";    

    2.Could not execute: /usr/sbin/sendmail
    这是因为
   
Java代码  收藏代码
  1. $mail->IsSendmail();  // tell the class to use Sendmail   

   去掉上面的代码就ok了!

3.关于phpmailer发送邮件产生中文乱码问题
  环境一:在普通环境,即标题内容等含中文的内容是在脚本中加上去的,或从文本中获取的,只需要进行如下操作(网上有很多):
   修改class.phpmailer.php中的EncodeHeader函数,改为:
Php代码  收藏代码
  1. public function EncodeHeader($str$position = 'text'$pl = 0) {  
  2.    $x = 0;  
  3.    if ($pl){return "=?".$this->CharSet."?B?".base64_encode($str)."?=";}   

再改下使用这个函数的一段:
Php代码  收藏代码
  1. if($this->Mailer != 'mail') {  
  2.       $result .= $this->HeaderLine('Subject'$this->EncodeHeader($this->SecureHeader($this->Subject),'text',1));  
  3.     }  

   当然编码设置也不能少了:
Php代码  收藏代码
  1. $mail->CharSet="utf-8";   
  2.        $mail->Encoding = "base64";  

环境二:从excel中提取内容然后再发送excel中的内容给用户,这个折腾了我好久。最终找到解决办法了。最关键的地方是:excel中的编码是html格式的unicode,所以得使用下面这个函数将其转化为utf8,这个帖子的最后回复的人帮了我,谢谢他!帖子地址是:http://www.phpchina.com/bbs/viewthread.php?tid=111554
Php代码  收藏代码
  1. private function uc2html($str)  
  2.   {  
  3.     $ret = '';  
  4.     for$i=0; $i<strlen($str)/2; $i++ )  
  5.     {  
  6.         $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);  
  7.         $ret .= '&#'.$charcode.';';  
  8.      }  
  9.     return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES');  
  10.   }  
  11.     


下面贴段测试代码:
  
Php代码  收藏代码
  1. <?php  
  2. /** 
  3. * Simple example script using PHPMailer with exceptions enabled 
  4. * @package phpmailer 
  5. * @version $Id$ 
  6. */  
  7.   
  8. require '../class.phpmailer.php';  
  9.   
  10. try {  
  11.     $mail = new PHPMailer(true); //New instance, with exceptions enabled  
  12.   
  13.     $body             = file_get_contents('contents.html');  
  14.     $body             = preg_replace('/\\\\/',''$body); //Strip backslashes  
  15.   
  16.     $mail->IsSMTP();                           // tell the class to use SMTP  
  17.     $mail->SMTPAuth   = true;                  // enable SMTP authentication  
  18.     $mail->Port       = 25;                // set the SMTP server port  
  19.     $mail->Host       = "smtp.xxxx.com"// SMTP server  
  20.     $mail->Username   = "xxx@xxx.com";     // SMTP server username  
  21.     $mail->Password   = "xxxx";            // SMTP server password  
  22.   
  23.     $mail->IsSendmail();  // tell the class to use Sendmail  
  24.   
  25.     $mail->AddReplyTo("xxx@sina.com","xxxx");  
  26.   
  27.     $mail->From       = "xxxx@m6699.com";  
  28.     $mail->FromName   = "DJB";  
  29.   
  30.     $to = "xxx@sina.com";  
  31.   
  32.     $mail->AddAddress($to);  
  33.   
  34.     $mail->Subject  = "First PHPMailer Message";  
  35.   
  36.     $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"// optional, comment out and test  
  37.     $mail->WordWrap   = 80; // set word wrap  
  38.   
  39.     $mail->MsgHTML($body);  
  40.   
  41.     $mail->IsHTML(true); // send as HTML  
  42.   
  43.     $mail->Send();  
  44.     echo 'Message has been sent.';  
  45. } catch (phpmailerException $e) {  
  46.     echo $e->errorMessage();  
  47. }  
  48. ?>  
  49.      


再给一个网上的操作excel的类,返回结果是一个数组。非常方便!用到的组件是PHP ExcelParser Pro v.4.2

 
Php代码
<?php  
  1. /** 
  2.  * CopyRight (c) 2009, 
  3.  * All rights reserved. 
  4.  * 文件名:excel数据获取 
  5.  * 摘  要: 
  6.  * 
  7.  * @author 星期八 [email=ixqbar@hotmail.com]ixqbar@hotmail.com[/email] 
  8.  * @version 0.1 
  9.  */  
  10.   
  11. class ExcelParser  
  12. {  
  13.     private $_data=array(0,'');  
  14.     private $_excel_handle;  
  15.     private $_excel=array();  
  16.     /** 
  17.      * 构造函数 
  18.      * @param <string> $filename 上传文件临时文件名称 
  19.      */  
  20.     public function __construct($filename)  
  21.     {  
  22.         /** 
  23.          * 引入excelparser类 
  24.          * 普通方法为 
  25.          * requires 路径.'excelparser.php'; 
  26.          * import为ThinkPHP自带导入类方法 
  27.          */  
  28.          require("excelparser.php");  
  29.         $this->_excel_handle=new ExcelFileParser();  
  30.         //错误获取  
  31.         $this->checkErrors($filename);  
  32.     }  
  33.     /** 
  34.      * 错误校验 
  35.      */  
  36.     private function checkErrors($filename)  
  37.     {  
  38.         /** 
  39.          * 方法一 
  40.          */  
  41.         $error_code=$this->_excel_handle->ParseFromFile($filename);  
  42.         /** 
  43.          * 方法二 
  44.          * $file_handle = fopen($this->_filename,'rb'); 
  45.          * $content = fread($file_handle,filesize($this->_filename)); 
  46.          * fclose($file_handle); 
  47.          * $error_code = $this->_excel->ParseFromString($content); 
  48.          * unset($content,$file_handle); 
  49.          */  
  50.         switch($error_code)  
  51.         {  
  52.             case 0:  
  53.                 //无错误不处理  
  54.                 break;  
  55.             case 1:  
  56.                 $this->_data=array(1,'文件读取错误(Linux注意读写权限)');  
  57.                 break;  
  58.             case 2:  
  59.                 $this->_data=array(1,'文件太小');  
  60.                 break;  
  61.             case 3:  
  62.                 $this->_data=array(1,'读取Excel表头失败');  
  63.                 break;  
  64.             case 4:  
  65.                 $this->_data=array(1,'文件读取错误');  
  66.                 break;  
  67.             case 5:  
  68.                 $this->_data=array(1,'文件可能为空');  
  69.                 break;  
  70.             case 6:  
  71.                 $this->_data=array(1,'文件不完整');  
  72.                 break;  
  73.             case 7:  
  74.                 $this->_data=array(1,'读取数据错误');  
  75.                 break;  
  76.             case 8:  
  77.                 $this->_data=array(1,'版本错误');  
  78.                 break;  
  79.         }  
  80.         unset($error_code);  
  81.     }  
  82.     /** 
  83.      * Excel信息获取 
  84.      */  
  85.     private function getExcelInfo()  
  86.     {  
  87.         if(1==$this->_data[0])return;  
  88.         /** 
  89.          * 获得sheet数量 
  90.          * 获得sheet单元对应的行和列 
  91.          */  
  92.         $this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);  
  93.         for($i=0;$i<$this->_excel['sheet_number'];$i++)  
  94.         {  
  95.             /** 
  96.              * 行于列 
  97.              * 注意:从0开始计数 
  98.              */  
  99.             $row=$this->_excel_handle->worksheet['data'][$i]['max_row'];  
  100.             $col=$this->_excel_handle->worksheet['data'][$i]['max_col'];  
  101.             $this->_excel['row_number'][$i]=($row==NULL)?0:++$row;  
  102.             $this->_excel['col_number'][$i]=($col==NULL)?0:++$col;  
  103.             unset($row,$col);  
  104.         }  
  105.     }  
  106.     /** 
  107.      * 中文处理函数 
  108.      * @return <string> 
  109.      */  
  110.   private function uc2html($str)  
  111.   {  
  112.     $ret = '';  
  113.     for$i=0; $i<strlen($str)/2; $i++ )  
  114.     {  
  115.         $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);  
  116.         $ret .= '&#'.$charcode.';';  
  117.      }  
  118.     return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES');  
  119.    }  
  120.     /** 
  121.      * Excel数据获取 
  122.      */  
  123.     private function getExcelData()  
  124.     {  
  125.         if(1==$this->_data[0])return;  
  126.   
  127.         //修改标记  
  128.         $this->_data[0]=1;  
  129.         //获取数据  
  130.         for($i=0;$i<$this->_excel['sheet_number'];$i++)  
  131.         {  
  132.             /** 
  133.              * 对行循环 
  134.              */  
  135.             for($j=0;$j<$this->_excel['row_number'][$i];$j++)  
  136.             {  
  137.                 /** 
  138.                  * 对列循环 
  139.                  */  
  140.                 for($k=0;$k<$this->_excel['col_number'][$i];$k++)  
  141.                 {  
  142.                     /** 
  143.                      * array(4) { 
  144.                      *   ["type"]   => 类型 [0字符类型1整数2浮点数3日期] 
  145.                      *   ["font"]   => 字体 
  146.                      *   ["data"]   => 数据 
  147.                      *   ... 
  148.                      * } 
  149.                      */  
  150.                     $data=$this->_excel_handle->worksheet['data'][$i]['cell'][$j][$k];  
  151.                     switch($data['type'])  
  152.                     {  
  153.                         case 0:  
  154.                             //字符类型  
  155.                             if($this->_excel_handle->sst['unicode'][$data['data']])  
  156.                             {  
  157.                                 //中文处理  
  158.                                 $data['data'] = $this->uc2html($this->_excel_handle->sst['data'][$data['data']]);  
  159.                             }  
  160.                             else  
  161.                             {  
  162.                                 $data['data'] = $this->_excel_handle->sst['data'][$data['data']];  
  163.                             }  
  164.                             break;  
  165.                         case 1:  
  166.                             //整数  
  167.                             //TODO  
  168.                             break;  
  169.                         case 2:  
  170.                             //浮点数  
  171.                             //TODO  
  172.                             break;  
  173.                         case 3:  
  174.                             //日期  
  175.                             //TODO  
  176.                             break;  
  177.                     }  
  178.                     $this->_data[1][$i][$j][$k]=$data['data'];  
  179.                     unset($data);  
  180.                 }  
  181.             }  
  182.         }  
  183.     }  
  184.     /** 
  185.      * 主函数 
  186.      * @return <array> array(标识符,内容s) 
  187.      */  
  188.     public function main()  
  189.     {  
  190.         //Excel信息获取  
  191.         $this->getExcelInfo();  
  192.         //Excel数据获取  
  193.         $this->getExcelData();  
  194.         return $this->_data;  
  195.     }  
  196. }