简体   繁体   English

使用Google的邮件服务器通过PHPMailer发送邮件失败

[英]Sending mail with PHPMailer using Google's mailservers fails

I am using Google's mail servers to send emails. 我正在使用Google的邮件服务器发送电子邮件。 For email sending I am using PHPMailer. 对于电子邮件发送,我正在使用PHPMailer。 This is the snippet I am using on my example.com/contactus site: 这是我在example.com/contactus网站上使用的代码段:

        $mail             = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPDebug  = 2;
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail-> Host      = 'ssl://smtp.gmail.com';
        $mail->Port       = 465;                   // set the SMTP port for the GMAIL server

        $mail->Username   = "myprivateaddress@gmail.com";  // GMAIL username
        $mail->Password   = "myprivatepassword";            // GMAIL password
        $mail->SetFrom($form_email, $form_username);
        $mail->AddAddress("support@example.com", "Example Support");
        $mail->AddReplyTo($form_email, $form_username);

        $mail->Subject    = $subject2;
        $mail->Body       = $message2;           

        $mail->CharSet     = 'UTF-8';
        $mail->Encoding    = '8bit';
        $mail->ContentType = 'text/html; charset=utf-8\r\n';
        $mail->WordWrap = 70;

        if(!$mail->Send()) {
          echo "Mailer Error: " . $mail->ErrorInfo;
        } else {
          echo "Message sent!";
        }

The code sends an email to me (support@example.com) whose header is: 该代码向我发送了一封电子邮件(support@example.com),其标题为:

from:    user1 <myprivateaddress@gmail.com>
reply-to:    user1 <user1@gmail.com> (this is $form_email)
to:  Example Support <support@example.com>

The problem is when I use a different address for authentication as Username and Password the message is not sent. 问题是当我使用其他地址作为用户名和密码进行身份验证时,未发送消息。 The error is: 错误是:

2013-11-02 14:46:18 SERVER -> CLIENT: 220 mx.google.com ESMTP u46sm21298429eep.17 - gsmtp 2013-11-02 14:46:18   CLIENT -> SERVER: EHLO www.example.com 2013-11-02 14:46:18  SERVER -> CLIENT: 250-mx.google.com at your service, [2a01:4f8:140:71ed::2] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250 CHUNKING 2013-11-02 14:46:18   CLIENT -> SERVER: AUTH LOGIN 2013-11-02 14:46:18    SERVER -> CLIENT: 334 VXNlcm5hbWU6 2013-11-02 14:46:18  CLIENT -> SERVER: ZGFwcHdhbGxAZ21haWwuY29t 2013-11-02 14:46:18  SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2013-11-02 14:46:18  CLIENT -> SERVER: U3BvdGxpZ2h0OA== 2013-11-02 14:46:18  SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787 534 5.7.14 54 u46sm21298429eep.17 - gsmtp 2013-11-02 14:46:18 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787 534 5.7.14 54 u46sm21298429eep.17 - gsmtp 2013-11-02 14:46:18  CLIENT -> SERVER: QUIT 2013-11-02 14:46:18  SERVER -> CLIENT: 221 2.0.0 closing connection u46sm21298429eep.17 - gsmtp SMTP connect() failed. Mailer Error: SMTP connect() failed.

I have no idea why is it working with my private gmail account that I use every day and why isn't it with other gmail accounts I have. 我不知道为什么它可以与我每天使用的私人gmail帐户一起使用,为什么不能与我拥有的其他gmail帐户一起使用。 It should be a setting in gmail maybe but which one? 也许应该是gmail中的设置,但是哪一个呢?

Well, I couldn't find out why it was not working. 好吧,我找不到原因。 At the end, I changed to SendGrid. 最后,我更改为SendGrid。 They provided an easy API and I setup the email sending in an hour with my basic skills. 他们提供了一个简单的API,我用自己的基本技能设置了一个小时内发送的电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM