phpmailer--发送邮件报错

    xiaoxiao2021-03-25  247

    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)

    转载请注明原文地址: https://ju.6miu.com/read-226.html

    最新回复(0)