通过PHPmailer发送邮件/获取身份验证错误/

时间:2018-10-02 02:37:08

标签: php

我正在尝试发送邮件。我已经尝试过PHPMailer和PHP自己的mail()函数,但是mail()仅从gmails发送和接收。并且PHPMailer给了我身份验证错误,我已经打开了不太安全的应用,但仍然收到错误

    require_once(__DIR__ . '/PHPMailer/src/Exception.php');
    require_once(__DIR__ . '/PHPMailer/src/PHPMailer.php');
    require_once(__DIR__ . '/PHPMailer/src/SMTP.php');
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);          
    try {
        $mail->SMTPDebug = 2;                                 
        $mail->isSMTP();                                      
        $mail->Host = 'smtp.gmail.com';               
        $mail->SMTPAuth = true;                               
        $mail->Username = '**********@gmail.com';                 
        $mail->Password = '**********';                           
        $mail->SMTPSecure = 'ssl';                            
        $mail->Port = 587;                                    

        //Recipients
        $mail->setFrom('**********@yahoo.com', 'Mailer');
        $mail->addAddress('**********@gmail.com', 'Joe User');
        $mail->addReplyTo('**********@gmail.com', 'Information');

        //Content
        $mail->isHTML(true);                                  
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }
}    

这是代码...

0 个答案:

没有答案