phpmailer--发送邮件报错

4 篇文章 0 订阅
1 篇文章 0 订阅

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来实现。


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

2.常见问题

2-1 Mailer Error: Could not instantiate mail function

解决:(1).要求的 php 版本太高,下载低版本的phpmailer试一下;

(2).端口号有问题,例如163邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:

类型                 服务器名称 服务器地址 SSL协议端口号 非SSL协议端口号
收件服务器  POP       pop.163.com         995                          110
收件服务器  IMAP      imap.163.com         993                          143
发件服务器  SMTP      smtp.163.com      465/994                   25

2-2 Could not connect to SMTP host

2-2-1 因为fsockopen函数被禁用,PHPmailer发送Email依赖此函数。

解决:(1):修改:class.stmp.php

  $this->smtp_conn = fsockopen($host,    // the host of the server

改为:

  $this->smtp_conn = pfsockopen($host,    // the host of the server

 或者另一个函数stream_socket_client()。

stream_socket_client的参数与fsockopen有所不同,所以代码要修改为:

$this->smtp_conn= stream_socket_client("tcp://".$host.":".$port,$errno,  $errstr,  $tval);

或者

打开php.ini。有两个地方可能禁用此函数:
1、 allow_url_fopen = On选项是否为ON,为OFF时函数将被禁用
2、查看 disable_functions = ..........后有没有 fsockopen。如果有去掉

2-2-2 php --ini 一下,看使用的ini是否和作为apache模块运行的php一致.

wamp环境,应该是共用一份ini文件的.想再一次确定的话,php网页输出phpinfo(),看使用的ini文件,是否和这个一致.
命令:telnet smtp.gmail.com 465 ,确定能连接成功?
telnet都不行,肯定是你防火墙禁止了访问
解决:(2):修改:class.phpmailer.php

  public function IsSMTP() {
    $this->Mailer = 'smtp';
  }

改为:
  public function IsSMTP() {
    $this->Mailer = 'SMTP';
  }

2-2-3 Warning: stream_socket_enable_crypto

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /opt/lampp/htdocs/webmail_client_practise/class.smtp.php on line 344SMTP Error: Could not connect to SMTP host.

解决:(3):修改class.phpmailer.php

This is because you're running PHP 5.6 and it's verifying your certs, but your server is presenting invalid certs so it's failing. Both PHPMailer and PHP are correct in what they are doing - the code is not at fault. You can either fix your mail server, or do what it suggests in the troubleshooting guide, which is:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

或者修改class.smtp.php

public function connect($host, $port = null, $timeout = 30, $options = array()) {
       if (count($options) == 0) {
           $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
       }

2-2-4 SMTP -> ERROR: Failed to connect to server: Permission denied (13)

解决:(4):

对于解决这个问题 你可以查看一下linux下面的这个

sestatus -b | grep httpd

 

看见了吧...我的httpd_can_network_connect本来是off的

这是因为selinux拒绝httpd服务发送邮件, 可以修改selinux的规则。 步骤如下:

使用下面的命令查看selinux的权限:getsebool -a  | grep httpd_can_sendmail OR getsebool httpd_can_sendmail;

再使用下面的命令允许httpd发送邮件 :setsebool -P httpd_can_sendmail 1 or  /usr/sbin/setsebool-P httpd_can_sendmail on

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

2-3-1 标题内容等含中文的内容:

   修改class.phpmailer.php中的EncodeHeader函数,改为:

    public function EncodeHeader($str, $position = 'text', $pl = 0) {
       $x = 0;
       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)."?=";}
再改下使用这个函数的一段:
    if($this->Mailer != 'mail') {
          $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject),'text',1));
         }
if($this->Mailer != 'mail') { $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject),'text',1)); }
当然编码设置也不能少了:
    $mail->CharSet="utf-8";           $mail->Encoding = "base64";

2-3-2 从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'); }

2-4 Failed to connect to server: Connection timed out (110)

1.The following From address failed邮件发不了的问题之一可能是因为ipv6连接不上导致的。也跟php没什么关系,不是php的bug,因为我们这连接测试用talnet连接的,一样优先连接到的是ipv6,不是php中使用代码连的。

解决:在发送邮件的代码里把smtp host的地址设置为ipv4地址,如:

$mail ->Host       =  "173.194.79.108" ;       // sets GMAIL as the SMTP server  smtp.gmail.com

2-5 Language string failed to load:

1.

加到程序中试了一下(小插曲:一开始没改路径,发现还是不好使。这个玩意是需要改路径的)

3.有些方法可能起作用,本人遇到了2-4与2-2-3)


  • 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、付费专栏及课程。

余额充值