Technical & Scientific > Programming

PHP mail through Gmail servers

(1/3) > >>

ober:
I am really hoping one of you can help me.  I've been pulling my hair out for about 2 hours with this.  One of the accounts on my client's email servers got compromised and Google start blocking attempts for us to send emails.  So I am trying to switch from the basic PHP mail command to PHPMailer and send through Google's SMTP server.  This client uses Google Pro for all of his accounts.

I am using the following:

--- Code: ---$mail             = new PHPMailer();
               
        $mail->isSMTP(); // telling the class to use SMTP
        $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $mail->SMTPSecure = "tls";                 // sets the prefix to the server
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
        $mail->Username   = "no-reply@americanbeautytools.com";  // GMAIL username
        $mail->Password   = "*********";            // GMAIL password
       
        $mail->SetFrom('no-reply@americanbeautytools.com', 'American Beauty Tools');
       
        $mail->Subject    = $this->subject;
       
        $body             = $this->message;
        //$body             = eregi_replace("[\]",'',$body);           
       
        $mail->MsgHTML($body);
       
        $mail->AddAddress($this->to, 'bob');

        if(!$mail->Send()) {
            error_log("Mailer Error: " . $mail->ErrorInfo);
            echo '<br>Fail';
        } else {
            error_log("Message sent!");
            echo '<br>Pass';
        }

--- End code ---
I know the credentials are correct because I can login with them.  I've done all kinds of shit to this script and the account it is trying to use to send:

1) Change from TLS to SSL
2) 'unblock captcha' in the account
3) disabled 2 factor auth (it was already off)
4) enabled 'less secure apps'

I'm not sure what else to do.  This is the error I keep getting:

2017-05-15 03:12:30   CLIENT -> SERVER: EHLO americanbeautytools.com 2017-05-15 03:12:30   CLIENT -> SERVER: STARTTLS 2017-05-15 03:12:30   CLIENT -> SERVER: EHLO americanbeautytools.com 2017-05-15 03:12:30   CLIENT -> SERVER: AUTH LOGIN 2017-05-15 03:12:30   CLIENT -> SERVER: bm8tcmVwbHlAYW1lcmljYW5iZWF1dHl0b29scy5jb20= 2017-05-15 03:12:30   CLIENT -> SERVER: YW1lcjFjQG4zOGVAdFlfYW0= 2017-05-15 03:12:32   SMTP ERROR: Password command failed: 535 Incorrect authentication data 2017-05-15 03:12:32   SMTP Error: Could not authenticate. 2017-05-15 03:12:32   CLIENT -> SERVER: QUIT 2017-05-15 03:12:32   SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

The red part is the part I don't get.  How can that be wrong when I login with that email account just fine?

hans:
Some Googling points to perhaps the host not allowing outbound access to SMTP perhaps? What are you running this off of? Maybe need to SELinux something or enable something for SMTP?

Along the lines of this thread maybe: http://stackoverflow.com/questions/35368239/phpmailer-authentication-issue

Mike:
You need to use their relay service and not their smtp service: https://support.google.com/a/answer/2956491?hl=en

Might I offer another suggestion?  Set up a small postfix server and have PHP deliver to that.  Then you have that server use Google's relay service.  A big problem I've run into with PHPMailer and Gmail is that Gmail will (on occasion) do a temporary reject and you'll lose the email as PHPMailer has no retry ability.  This caused us to setup an internal only mail server that can then retry if it gets a temporary reject.

micah:
FWIW, I use https://www.mailgun.com/ for transactional e-mails.  My main side-gig client sends about 2,000+ emails a month through it.  Its owned/operated by rackspace and is free for the first 10,000 emails you send each month. 

The API is super simple, no SDK required and you don't need sendmail either. Just a couple lines to cURL your credentials and an array w/ the  "to," "from," "subject" and "body" fields.

edit: plus it keeps logs of everything so you can see what e-mails were sent and when they were received or if/why they were bounced, dropped, rejected, etc.

Mike:
We use mandrill (now part of mailchimp) for our production emails much the same reason.  But we use their SMTP interface so there was no code changes needed.

Navigation

[0] Message Index

[#] Next page

Go to full version