zoukankan      html  css  js  c++  java
  • 【PHPmailer】发送邮件 出现无法连接服务器、函数 fsockopen()被禁用 解决办法 Subject 和 收件人重复

    网上资料

    ------------------------------------------------------------------------------------------------------

    发现使用PHPmailer发送邮件不成功,提示“不能连接SMTP服务器.”(Error: Could not connect to SMTP host)。

    找了很多的方法花费了很多的时间都是出现Could not connect to SMTP host 很是郁闷,所以一直也没有回复大家的评论!~后来找到了一种解决办法就是 服务器发送邮件出现Could not connect to SMTP host错误 解决办法 后来证明是错误的!

    我发现并不是因为修改了smtp为SMTP之后却能够发送邮件,这个并不是因为有些邮件服务器不能接受smtp的原因,而并不是使用了smtp来发送邮件,PHPmailer里有一个判断的函数,
    public function IsSMTP() {
    $this->Mailer = 'SMTP';
    }

    switch($this->Mailer) {
    case 'sendmail':
    return $this->SendmailSend($header, $body);
    case 'smtp'://由于SMTP和smtp不相等 所以选择的是下面MailSend发送邮件 并不是使用smtp发送邮件
    return $this->SmtpSend($header, $body);
    default:
    return $this->MailSend($header, $body);

    虽然可以连接上服务器了,但是邮件一直被QQ当作是垃圾邮件屏蔽了!  主题重复了两次,收件人也是重复了两次!

    image

    很是郁闷,由是一番的捣鼓!百度发现了问题所在

    Linux主机禁用了fsockopen()函数

    国内有很多idc禁用了fsockopen函数。据说是出于安全原因。
    不过,就目前收集到的资料来看,fsockopen函数在安全上隐患只出现在Windows平台上,或许对其他平台也有影响,还没有找到fsockopen函数在Linux主机安全方面的利用方法。

    解决方案:

    用pfsockopen()函数直接替换掉 fsockopen()
    如果pfsockopen函数被禁用的话,换其他可以操作Socket函数来代替, 如stream_socket_client()

    class.smtp.php 中@fsockopen 改成 @pfsockopen

    $this->smtp_conn = @fsockopen($host,    // the host of the server
                                     $port,    // the port to use
                                     $errno,   // error number if any
                                     $errstr,  // error message if any
                                     $tval);   // give up after ? secs
        // verify we connected properly

    改成

    $this->smtp_conn = @pfsockopen($host,    // the host of the server
                                     $port,    // the port to use
                                     $errno,   // error number if any
                                     $errstr,  // error message if any
                                     $tval);   // give up after ? secs
        // verify we connected properly

    ------------------------------------------------------------------------------------------------------

    自己 解决 Subject 和 收信人重复的问题

    class.phpmailer.php

    1. 下面 To: 的内容加到 head变量里面的注销掉

    /*

        // To be created automatically by mail()
        if($this->Mailer != 'mail') {
          if ($this->SingleTo === true) {
            foreach($this->to as $t) {
              $this->SingleToArray[] = $this->AddrFormat($t);
            }
          } else {
            if(count($this->to) > 0) {
              $result .= $this->AddrAppend('To', $this->to);
            } elseif (count($this->cc) == 0) {
              $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
            }
          }
        }

    */

    2.  下面 Subject: 的内容加到 head变量里面的注销掉

    /*

    // mail() sets the subject itself
        if($this->Mailer != 'mail') {
          $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
        }

    */

  • 相关阅读:
    【Linux高频命令专题(24)】grep
    【PSR规范专题(6)】PSR-7 HTTP消息接口【转】
    【redis专题(10)】KEY设计原则与技巧
    【redis专题(9)】事务
    【redis专题(8)】命令语法介绍之通用KEY
    linux 之 jq
    PHP Rabbitmq 报错Broken pipe
    Laravel 跨框架队列交互
    解决Linux下编译.sh文件报错 unexpected operator Syntax error: word unexpected
    安装QConf 报错及解决方案
  • 原文地址:https://www.cnblogs.com/zhiqixue/p/2721469.html
Copyright © 2011-2022 走看看