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

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

PHPMailer邮件类使用错误分析

一,没有定义发送邮箱$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邮件,如下:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  4. <title>PHPMailer邮件测试</title>
  5. </head>
  6. <body>
  7. <div>PHPMailer邮件类使用错误分析</div>
  8. </body>
  9. </html>

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

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

==========================================================================


这几天做mail群发,碰到不少问题。一些常见的错误网上很多但没有答案,靠自己不断的尝试终于OK了~这里把几个常见的问题列出来做为工作笔记!
     要做发送邮件功能,首先要明白邮件收发的原理,引用网友一段话比较容易懂:

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来实现。  
在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    = "******";     
$mail->Username = "smtp@163.com"; // SMTP server username $mail->Password = "******";


    2.Could not execute: /usr/sbin/sendmail
    这是因为
   

Java代码 复制代码
  1. $mail->IsSendmail();  // tell the class to use Sendmail   
$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)."?=";}   
public function EncodeHeader($str, $position = 'text', $pl = 0) { $x = 0; 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.      }  
if($this->Mailer != 'mail') { $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject),'text',1)); }


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

Php代码 复制代码
  1. $mail->CharSet="utf-8";   
  2.        $mail->Encoding = "base64";  
$mail->CharSet="utf-8"; $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.     
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代码 复制代码
  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.      
<?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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用PHPMailer发送异步邮件的示例代码: ```php // 引入PHPMailer类 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // 异步发送邮件函数 function sendMailAsync($to, $subject, $body) { // 新建一个PHPMailer对象 $mail = new PHPMailer(true); try { // 配置SMTP服务器 $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'username'; $mail->Password = 'password'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; // 配置邮件内容 $mail->setFrom('from@example.com', 'From Name'); $mail->addAddress($to); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); return true; } catch (Exception $e) { // 发送失败,记录错误日志 error_log($e->getMessage()); return false; } } // 调用异步发送邮件函数 $to = 'recipient@example.com'; $subject = 'Test Subject'; $body = 'Test Body'; sendMailAsync($to, $subject, $body); ``` 在这个示例中,我们使用PHPMailer类来发送邮件。我们首先引入这个类,然后定义了一个`sendMailAsync`函数来异步发送邮件。该函数接受收件人地址、邮件主题和邮件正文作为参数。在函数内部,我们新建了一个PHPMailer对象,并使用SMTP服务器配置了邮件发送选项。然后,我们配置了邮件的内容,并使用`send`方法来实际发送邮件。如果发送成功,函数返回`true`,否则返回`false`并记录错误日志。最后,我们调用异步发送邮件函数来发送邮件。 请注意,这个示例中的`sendMailAsync`函数并没有使用PHP的异步机制。如果您想使用PHP的异步机制来发送邮件,您可以使用Swoole扩展或其他类似的工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值