zoukankan      html  css  js  c++  java
  • linux下phpmailer发送邮件出现SMTP ERROR: Failed to connect to server: (0)错误

    转自:https://www.cnblogs.com/raincowl/p/8875647.html

    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;
    //Set the hostname of the mail server
    $mail->Host = 'smtp.qq.com';
    //$mail->Host = 'smtp.163.com';
    //Set the SMTP port number - likely to be 25, 465 or 587
    $mail->Port = 465;
    $mail->SMTPSecure = "ssl";
    //$mail->SMTPAuth = false;
    //$mail->SMTPSecure = false;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = 'xxxxxxx@qq.com';
    //$mail->Username = 'raincowl@163.com';
    //Password to use for SMTP authentication
    //$mail->Password = 'lingshuan008';
    $mail->Password = 'password';
    //Set who the message is to be sent from
    //$mail->setFrom('raincowl@163.com', 'fromuser');
    $mail->setFrom('xxxxxxx@qq.com', 'fromuser');
    //Set an alternative reply-to address
    //$mail->addReplyTo('raincowl@163.com', 'First Last');
    $mail->addReplyTo('xxxxxxx@qq.com', 'First Last');
    //Set who the message is to be sent to
    $mail->addAddress('yyyyyyy@qq.com', 'John Doe');
    //Set the subject line
    $mail->Subject = 'PHPMailer SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    //$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
    $mail->msgHTML('hello,body!');
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';
    //Attach an image file
    //$mail->addAttachment('images/phpmailer_mini.png');
    
    //send the message, check for errors
    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message sent!';
    }

    首先本地windows下,发送成功,很happy,那就放上linux上试下,一运行,出现

    SMTP ERROR: Failed to connect to server: (0)
    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
    Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

    然后各大技术论坛查资料;首先检查linux服务器上的openssl和sockets已经开启,fsockopen函数也没有禁用,邮箱的smtp服务也已经开启,依然还是不行。至于有一种说法将smtp换成SMTP,是能够发送成功,后来查看下源码,发现这种只是通过sendmail发送的,不是smtp。然后,直接在linux上ping smtp.qq.com,telnet  smtp.qq.com 465都没什么问题,可是依然发送报上述的错误,后来查看官网资料,发现下面一段代码

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

    将这段代码加上之后,邮件发送成功。

  • 相关阅读:
    JVM 常用参数设置(针对 G1GC)
    Java 字符串常量池 及 intern 方法的使用
    JDK 1.8 Metaspace 详解
    JDK 1.8 MetaSpace(元空间)介绍及调优
    Git 统计代码行数
    王永庆传-读书笔记2
    王永庆传-读书笔记1
    董明珠:女人真想干点事,谁也拦不住
    esxi5.5安装nvme驱动
    nvme ssd的一些相关知识点
  • 原文地址:https://www.cnblogs.com/rxbook/p/11281697.html
Copyright © 2011-2022 走看看