使用SMTP连接PHPMailer时出现问题

时间:2016-10-28 12:42:11

标签: php phpmailer

我正在尝试使用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保护的开发服务器中工作

1 个答案:

答案 0 :(得分:0)

我已找到解决此问题的详细信息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