Author Topic: PHP mail through Gmail servers  (Read 10532 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14335
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
PHP mail through Gmail servers
« on: May 14, 2017, 11:25:38 PM »
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: [Select]
$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';
        }
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

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3539
  • Karma: +46/-18
Re: PHP mail through Gmail servers
« Reply #1 on: May 15, 2017, 12:21:42 AM »
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
This signature intentionally left blank.

Mike

  • Jackass In Charge
  • Posts: 11270
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP mail through Gmail servers
« Reply #2 on: May 15, 2017, 01:40:37 AM »
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

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: PHP mail through Gmail servers
« Reply #3 on: May 15, 2017, 02:26:03 AM »
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.
« Last Edit: May 15, 2017, 02:32:14 AM by micah »
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11270
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP mail through Gmail servers
« Reply #4 on: May 15, 2017, 08:38:57 AM »
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.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: PHP mail through Gmail servers
« Reply #5 on: May 15, 2017, 01:41:37 PM »
I used to use mandrill when it was free for low usage. It was developed by mailchimp but when they started charging for all tiers, they started branding the mailchimp aspect more.  That's when I switched to mailgun which I like much better.
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14335
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP mail through Gmail servers
« Reply #6 on: May 16, 2017, 09:00:08 AM »
Thanks guys.  There's a reason I post here before anywhere else.  You guys are awesome.  Fucking StackOverflow assholes just gave me a bunch of grief and basically posted the same example code that I posted as an 'answer'.

I'm going to give Mailgun a shot.  I'll see how that goes.  THANK YOU!

hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3539
  • Karma: +46/-18
Re: PHP mail through Gmail servers
« Reply #7 on: May 16, 2017, 11:50:26 AM »
There's also SendGrid if I remember right. Not sure what they've been up to since the Mandrill deal a few years back. I think they tried to offer something similar but at the time I wasn't as impressed by their API. It may have gotten better by now though.

Also, that's one of the reasons I don't find SO as useful these days. Way to over moderated. I get what they're trying to do but I just don't agree with it. I often find my answers through other means these days. It's hard to actually get anything answered there without it getting flagged as a duplicate, off topic, or whatever.
This signature intentionally left blank.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: PHP mail through Gmail servers
« Reply #8 on: May 16, 2017, 02:02:55 PM »
If you're interested, here's my code that sends to mailgun

It assumes some existing variables (like $subject and $message) and constants like MAILGUN_API_Key and MAILGUN_Base_URL that I set elsewhere, plus it is set up to accepts attachemets as well but you can leave that out.

Code: [Select]
$mail_array = array('from' => 'someone@mysite.com,
'to' => array("to"=> 'someone@somewhere.com',"bcc"=>"someoneelse@somewhere.com"),
                  'subject' => $subject,
                  //'text' => 'plain text message';
                  'html' => '<html>'.$message.'</html>');

if (count($attachments) > 0) {
$i = 1;
foreach ($attachments as $attachment) {
$mail_array['attachment[' . $i . ']'] = curl_file_create($attachment);
$i++;
}
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.MAILGUN_API_Key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, rtrim(MAILGUN_Base_URL,"/").'/messages');
curl_setopt($ch, CURLOPT_ENCODING, 'multipart/form-data');
curl_setopt($ch, CURLOPT_POSTFIELDS, $mail_array);
           
$result = json_decode(curl_exec($ch));
curl_close($ch);
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14335
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP mail through Gmail servers
« Reply #9 on: May 16, 2017, 09:06:06 PM »
How long did it take for your DNS/MX records to update?  And thanks for the code.  I'll use that.  I started down the path of the PHP library but my project isn't PSR-7.  :(

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14335
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP mail through Gmail servers
« Reply #10 on: May 16, 2017, 09:38:08 PM »
I got the sandbox functional at least and it's sending like it should.  Once the DNS/MX is good to go, I should be able to swap everything to this.  Thanks again for your help!

Mike

  • Jackass In Charge
  • Posts: 11270
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP mail through Gmail servers
« Reply #11 on: May 17, 2017, 03:16:07 PM »
How long did it take for your DNS/MX records to update?  And thanks for the code.  I'll use that.  I started down the path of the PHP library but my project isn't PSR-7.  :(
What's the TTL for your DNS records?  You can plan on about 24 hours as upstream DNS will often cache it without respecting the TTL.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14335
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP mail through Gmail servers
« Reply #12 on: May 21, 2017, 10:27:18 AM »
I think TTL was 14400.  I'm all set now though. Got it all sorted and swapped all the mail calls last night.