dongpao9165 2019-01-11 11:59
浏览 150

从Gmail商家帐户发送电子邮件[重复]

I'm trying to send emails from a Gmail business account, But it's not working.

Here is the code:

//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;
}

The result:

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

I had a similar issue with a personal account, I fixed it by allowing low secured apps, But I don't know how to fix this one.

I tried both ssl and tls, But still not working.

How to fix this issue?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?