首页 文章

使用SMTP连接PhpMailer时出现问题

提问于
浏览
0

我正在尝试使用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安全的 . 另外我得到这个meesage:“邮件错误:SMTP连接()失败 . ”

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

我在具有HTACCESS保护的开发服务器中工作

1 回答

  • 0

    好吧,我已经找到了解决问题的详细信息https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .

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

    This takes few minutes or an hour to active.

    我目前的代码:

    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

相关问题