php发邮件有时很卡,phpmailer发邮件常见的问题及解决方法汇总

phpmailer发邮件常见的问题及解决方法汇总

发布于 2014-11-22 14:50:36 | 808 次阅读 | 评论: 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来实现。

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

d463c25e84ff04bbc28891c90deb5aac.png

有关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邮件,如下:

PHPMailer邮件测试
PHPMailer邮件类使用错误分析

七,学会正确使用错误提示$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

{

$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

测试示例代码:

/**

* 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();

}

?>

相关阅读:

phpmailer发邮件常见的问题及解决方法汇总

PHP使用phpmailer发邮件示例

phpmailer中文乱码问题的解决方法

phpmailer通过Windows的SMTP发送邮件失败的解决方案

PHP使用PHPMailer发送邮件的简单示例

PHPMailer邮件发送的示例讲解

使用PHPMailer发送有附件的电子邮件

phpmailer发送邮件之后,获取收件人是否阅读了邮件的方法

thinkphp使用phpmailer发送邮件的方法

ThinkPHP利用PHPMailer发送邮件代码

phpmailer发送邮件示例(使用163smtp服务器)

PHPMailer发送邮件使用方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值