发布于 2014-11-22 06:50:36 | 991 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

PHPMailer 发送电子邮件的PHP函数包

PHPMailer是一个用于发送电子邮件的PHP函数包。支持发送HTML内容的电子邮件,以及可以添加附件发送,并不像PHP本身mail()函数需要服务器环境支持,您只需要设置邮件服务器以相关信息就能实现邮件发送功能。


本文为大家整理汇总了一些phpmailer发邮件常见的问题及解决方法,感兴趣的同学参考下。

PHPMailer是一个用于发送电子邮件的PHP函数包。支持发送HTML内容的电子邮件,以及可以添加附件发送,并不像PHP本身mail()函数需要服务器环境支持,您只需要设置邮件服务器以相关信息就能实现邮件发送功能。

邮件收发的原理:

在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/

一,没有定义发送邮箱$mail->From或格式不正确,错误提示:Language string failed to load: recipients_failed test@test.com,注意,这个配置一定要正确,而且是正确的邮箱

二,没有定义邮件服务主机$mail->Host或连接失败,错误提示:Language string failed to load: connect_host

三,没有定义发送邮箱$mail->AddAddress或邮箱格式不正确,错误提示:Language string failed to load: provide_address

四,没有定义邮箱发送用户名$mail->Username,错误提示:Language string failed to load: connect_host

五,没有定义邮箱发送密码$mail->Password,错误提示:Language string failed to load: connect_host,这类错误非常明显,一般都是邮箱服务器配置不正确不能边接。

六,邮件正文编码,如果发送HTML邮件,需要定义正确的编码格式和字符,发送GBK邮件如下:
$mail->IsHTML ( true ); 是否支持HTML邮件
$mail->CharSet = "GB2312"; 字符设置
$mail->Encoding = "base64"; 编码方式
配置后可直接发送HTML邮件,如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>PHPMailer邮件测试</title>
</head>
<body>
<div>PHPMailer邮件类使用错误分析</div>
</body>
</html>

七,学会正确使用错误提示$mail->ErrorInfo查看邮件错误,可直接查找问题。

使用PHPMailer邮件类发送邮件使用非常简单,基本配置如上所示,在使用过程中正确了解错误提示,并及时了解错误原因,对于正确使用PHPMailer邮件类来说非常重要。

 

常见异常:

1.SMTP Error: Could not authenticate.

这个是因为smtp验证没通过,就是smtp server 的用户名和密码不正确了

$mail->Username    = "smtp@163.com";     // SMTP server username     

t;Password    = "******";       

$mail->Username = "smtp@163.com"; // SMTP server username $mail->Password = "******";  

2.Could not execute: /usr/sbin/sendmail

这是因为

$mail->IsSendmail();  // tell the class to use Sendmail     

l->IsSendmail(); // tell the class to use Sendmail  


去掉上面的代码就ok了!(PHP同理)

3.关于phpmailer发送邮件产生中文乱码问题

环境一:在普通环境,即标题内容等含中文的内容是在脚本中加上去的,或从文本中获取的,只需要进行如下操作(网上有很多):
修改class.phpmailer.php中的EncodeHeader函数,改为:

public function EncodeHeader($str, $position = 'text', $pl = 0) {     
$x = 0;     
if ($pl){return "=?".$this->CharSet."?B?".base64_encode($str)."?=";}     

ic function EncodeHeader($str, $position = 'text', $pl = 0) { $x = 0; if ($pl){return "=?".$this->CharSet."?B?".base64_encode($str)."?=";} 


再改下使用这个函数的一段:

if($this->Mailer != 'mail') {     
  $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject),'text',1));     
 }    

$this->Mailer != 'mail') { $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject),'text',1)); } 


当然编码设置也不能少了:

$mail->CharSet="utf-8";     
       $mail->Encoding = "base64";    

$mail->CharSet="utf-8"; $mail->Encoding = "base64"; 


环境二:从excel中提取内容然后再发送excel中的内容给用户,这个折腾了我好久。最终找到解决办法了。最关键的地方是:excel中的编码是 html格式的unicode,所以得使用下面这个函数将其转化为utf8

private function uc2html($str)     
       {     
        $ret = '';     
        for( $i=0; $i<strlen($str)/2; $i++ )     
         {     
            $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);     
            $ret .= '&#'.$charcode.';';     
          }     
        return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES');     
       }     
          
 
private function uc2html($str) { $ret = ''; for( $i=0; $i<strlen($str)/2; $i++ ) { $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]); $ret .= '&#'.$charcode.';'; } return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES'); }


测试示例代码:

<?php     
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/    
    
require '../class.phpmailer.php';     
    
try {     
    $mail = new PHPMailer(true); //New instance, with exceptions enabled     
    
    $body              = file_get_contents('contents.html');     
    $body              = preg_replace('/\\\\/','', $body); //Strip backslashes     
    
    $mail->IsSMTP();                           // tell the class to use SMTP     
    $mail->SMTPAuth    = true;                  // enable SMTP authentication     
    $mail->Port        = 25;                // set the SMTP server port     
    $mail->Host        = "smtp.xxxx.com"; // SMTP server     
    $mail->Username    = "xxx@xxx.com";     // SMTP server username     
    $mail->Password    = "xxxx";            // SMTP server password     
    
    $mail->IsSendmail();  // tell the class to use Sendmail     
    
    $mail->AddReplyTo("xxx@sina.com","xxxx");     
    
    $mail->From        = "xxxx@m6699.com";     
    $mail->FromName    = "DJB";     
    
    $to = "xxx@sina.com";     
    
    $mail->AddAddress($to);     
    
    $mail->Subject   = "First PHPMailer Message";     
    
    $mail->AltBody     = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test     
    $mail->WordWrap    = 80; // set word wrap     
    
    $mail->MsgHTML($body);     
    
    $mail->IsHTML(true); // send as HTML     
    
    $mail->Send();     
    echo 'Message has been sent.';     
} catch (phpmailerException $e) {     
    echo $e->errorMessage();     
}     
?>



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务