【问题标题】:Getting trouble with SMTP connect with PHPMailer使用 PHPMailer 连接 SMTP 时遇到问题
【发布时间】:2017-03-11 08:43:15
【问题描述】:

我正在尝试使用 PHPMailer 发送带有附件的 SMTP 安全邮件。

我用 PHPMailer 库制作了这个函数

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){

    require getcwd() .'/lib/PHPMailerAutoload.php';             

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;      // Enable verbose debug output

    $mail->isSMTP();              // Set mailer to use SMTP

    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true;                             
    $mail->Username = 'tester@mydomain.com';                
    $mail->Password = '**************';                          
    $mail->SMTPSecure = 'ssl';                            
    $mail->Port = 465;                            

    $mail->setFrom($from_mail, $from_name);
    $mail->addAddress($mail_to);  
    $mail->addReplyTo($replyto, 'no reply');

    $mail->addAttachment($path);        
    $mail->isHTML(true);                                  

    $mail->Subject = $subject;

    $mail->Body    = $body;
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        echo  $error;
        } else {
            $sucess = 'Mail sent!';
            echo  $sucess;
            }
    }

现在如果我评论 $mail->isSMTP();它工作正常。但我认为它不是 SMTP 安全的。否则我会收到此消息:“邮件错误:SMTP 连接()失败。”

我搜索了这个问题,但没有得到我正在寻找的正确答案。请帮帮我。

我正在使用 HTACCESS 保护的开发服务器工作

【问题讨论】:

标签: php phpmailer


【解决方案1】:

我在https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 找到了解决问题的详细信息。

我刚刚从我的 gmail 帐户打开了不太安全的应用程序 https://www.google.com/settings/security/lesssecureapps

这需要几分钟或一个小时才能激活。

我当前的代码:

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
            require getcwd() .'/lib/PHPMailerAutoload.php'; 
            $mail = new PHPMailer();

            $mail->isSMTP();   
            $mail->Host = 'smtp.gmail.com'; 
            //$mail->SMTPDebug = 2;                               
            $mail->SMTPAuth = true;   
            $mail->SMTPSecure = 'tls';                            
            $mail->Port = 587;  

            $mail->Username = 'mymail@gmail.com';                
            $mail->Password = 'mypass';                                     


            $mail->setFrom($from_mail, $from_name);
            $mail->addAddress($mail_to);  
            $mail->addReplyTo($replyto, 'no reply');

            $mail->addAttachment($path);        
            $mail->isHTML(true);                                  

            $mail->Subject = $subject;

            $mail->Body    = $body;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            if(!$mail->Send()) {
                $error = 'Mail error: '.$mail->ErrorInfo; 
                echo $error;
            } else {
                $sucess = 'Mail sent!';
                echo  $sucess;
            }
        }

否则,您必须从 console.developers.google.com 设置应用

按照本指南:https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多