drv16821 2017-06-28 11:24
浏览 1348
已采纳

PHPMailer- Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting?

I'm trying to use the phpmailer in my project, but the SMTP settings for gmail doesn't seem to be right. I referred a few questions asked here before, but none of the solutions seem to work for me.

<?php
    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $mail->IsSMTP();                                
    $mail->Host = 'smtp.gmail.com';                   
    $mail->Port = 587;                          
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;                       
    $mail->Username = '';  //gmail address                
    $mail->Password = ''; // gmail password
    $mail->From = 'user@domain.com';
    $mail->FromName = 'John Doe';
    $mail->AddAddress('yadayada@gmail.com', 'Nick');  // Add a recipient
    $mail->IsHTML(true);                             // Set email format to HTML
    $mail->Subject = 'subject';
    $mail->Body    = 'message body';
    $mail->AltBody = 'This is a plain-text message body';
    $mail->addAttachment('images/apache_pb.png');
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>
  • 写回答

2条回答 默认 最新

  • douliu1092 2017-06-28 11:47
    关注

    You must to have installed php_openssl.dll, if you use wampserver it's pretty easy, search and apply the extension for PHP.

    In the example change this:

       //Set the hostname of the mail server
        $mail->Host = 'smtp.gmail.com';
    
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
        $mail->Port = 465;
    
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'ssl';
    

    and then you recived an email from gmail talking about to enable the option to Less Safe Access Applications here https://www.google.com/settings/security/lesssecureapps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件