从Gmail企业帐户发送电子邮件

时间:2019-01-11 11:59:54

标签: php email smtp gmail phpmailer

我正在尝试从Gmail企业帐户发送电子邮件,但是它不起作用。

代码如下:

//Including PHPMailer files
require_once('phpmailer/src/phpmailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');

//Emails list
$recipients = array('user@mydomain.com', 'help@mydomain.com', 'desk@mydomain.com');

//Initializing PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer();                              // Passing `true` enables exceptions

try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'tls://smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Username = 'admin@mydomain.com';                 // SMTP username
    $mail->Password = '*******';                          // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('admin@mydomain.com', 'Admin');
    $mail->addReplyTo('admin@mydomain.com', 'Admin');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Subject';
    $mail->Body    = 'Hello World!';
    $mail->AltBody = 'Hello World';

    foreach ($recipients as $recipient) {
        $mail->addAddress($recipient);
        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $recipient) . ')<br />';
        }
        // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();
    }

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

结果:

2019-01-02 16:19:05 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP h2sm20095348itk.0 - gsmtp
2019-01-02 16:19:05 CLIENT -> SERVER: EHLO mydomain.com
2019-01-02 16:19:05 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2607:1b00:93b2:e42c::914a]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-01-02 16:19:05 CLIENT -> SERVER: STARTTLS
2019-01-02 16:19:05 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-01-02 16:19:05 CLIENT -> SERVER: EHLO mydomain.com
2019-01-02 16:19:05 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2607:1b00:93b2:e42c::914a]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-01-02 16:19:05 CLIENT -> SERVER: AUTH LOGIN
2019-01-02 16:19:05 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-01-02 16:19:05 CLIENT -> SERVER: <credentials hidden>
2019-01-02 16:19:05 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-01-02 16:19:05 CLIENT -> SERVER: <credentials hidden>
2019-01-02 16:19:05 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials h2sm20095348itk.0 - gsmtp
2019-01-02 16:19:05 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials h2sm20095348itk.0 - gsmtp
SMTP Error: Could not authenticate.
2019-01-02 16:19:05 CLIENT -> SERVER: QUIT
2019-01-02 16:19:05 SERVER -> CLIENT: 221 2.0.0 closing connection h2sm20095348itk.0 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error (user@example.com) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我的个人帐户有一个类似的问题,我通过允许安全性较低的应用程序来解决此问题,但是我不知道该如何解决。

我同时尝试了ssltls,但仍然无法正常工作。

如何解决此问题?

0 个答案:

没有答案