PHP MAIL Authentication for Gmail Workspace account

This page summarizes the projects mentioned and recommended in the original post on /r/PHPhelp

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • PHPMailer

    The classic email sending library for PHP

  • '); //Replace the plain text body with one created manually $mail->AltBody = 'alternate body text only'; //Attach an image file //$mail->addAttachment('images/phpmailer_mini.png'); //rem'd out for testing //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { die('Message sent!'); //Section 2: IMAP //Uncomment these to save your message in the 'Sent Mail' folder. if (save_mail($mail)) { echo "Message saved!"; } } //Section 2: IMAP //IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php //Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php //You can use imap_getmailboxes($imapStream, '/imap/ssl', '*' ) to get a list of available folders or labels, this can //be useful if you are trying to get this working on a non-Gmail IMAP server. function save_mail($mail) { //You can change 'Sent Mail' to any other folder or tag $path = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail'; //Tell your server to open an IMAP connection using the same username and password as you used for SMTP $imapStream = imap_open($path, $mail->Username, $mail->Password); $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage()); imap_close($imapStream); return $result; } And here is server log from the GOOD send: 2021-06-04 19:16:49 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w23sm2309597pfi.220 - gsmtp 2021-06-04 19:16:49 CLIENT -> SERVER: EHLO localhost 2021-06-04 19:16:49 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [98.148.179.208]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2021-06-04 19:16:49 CLIENT -> SERVER: STARTTLS 2021-06-04 19:16:49 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2021-06-04 19:16:49 CLIENT -> SERVER: EHLO localhost 2021-06-04 19:16:49 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [98.148.179.208]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2021-06-04 19:16:49 CLIENT -> SERVER: AUTH LOGIN 2021-06-04 19:16:49 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2021-06-04 19:16:49 CLIENT -> SERVER: [credentials hidden] 2021-06-04 19:16:49 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2021-06-04 19:16:49 CLIENT -> SERVER: [credentials hidden] 2021-06-04 19:16:50 SERVER -> CLIENT: 235 2.7.0 Accepted 2021-06-04 19:16:50 CLIENT -> SERVER: MAIL FROM:[email protected] 2021-06-04 19:16:50 SERVER -> CLIENT: 250 2.1.0 OK w23sm2309597pfi.220 - gsmtp 2021-06-04 19:16:50 CLIENT -> SERVER: RCPT TO:[email protected] 2021-06-04 19:16:50 SERVER -> CLIENT: 250 2.1.5 OK w23sm2309597pfi.220 - gsmtp 2021-06-04 19:16:50 CLIENT -> SERVER: DATA 2021-06-04 19:16:50 SERVER -> CLIENT: 354 Go ahead w23sm2309597pfi.220 - gsmtp 2021-06-04 19:16:50 CLIENT -> SERVER: Date: Fri, 4 Jun 2021 19:16:48 +0000 2021-06-04 19:16:50 CLIENT -> SERVER: To: You [email protected] 2021-06-04 19:16:50 CLIENT -> SERVER: From: Me [email protected] 2021-06-04 19:16:50 CLIENT -> SERVER: Reply-To: Me [email protected] 2021-06-04 19:16:50 CLIENT -> SERVER: Subject: PHPMailer GMail SMTP test 2021-06-04 19:16:50 CLIENT -> SERVER: Message-ID: LkOADjc1betdlidLdPJObOQDB9WwRw8zw67DopEInbo@localhost 2021-06-04 19:16:50 CLIENT -> SERVER: X-Mailer: PHPMailer 6.3.0 (https://github.com/PHPMailer/PHPMailer) 2021-06-04 19:16:50 CLIENT -> SERVER: MIME-Version: 1.0 2021-06-04 19:16:50 CLIENT -> SERVER: Content-Type: multipart/alternative; 2021-06-04 19:16:50 CLIENT -> SERVER: boundary="b1_LkOADjc1betdlidLdPJObOQDB9WwRw8zw67DopEInbo" 2021-06-04 19:16:50 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: This is a multi-part message in MIME format. 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: --b1_LkOADjc1betdlidLdPJObOQDB9WwRw8zw67DopEInbo 2021-06-04 19:16:50 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: Welcome here 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: --b1_LkOADjc1betdlidLdPJObOQDB9WwRw8zw67DopEInbo 2021-06-04 19:16:50 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: Here is some text in RED 2021-06-04 19:16:50 CLIENT -> SERVER: Here is some text in Green 2021-06-04 19:16:50 CLIENT -> SERVER: Here is some text in Blue 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: --b1_LkOADjc1betdlidLdPJObOQDB9WwRw8zw67DopEInbo-- 2021-06-04 19:16:50 CLIENT -> SERVER: 2021-06-04 19:16:50 CLIENT -> SERVER: . 2021-06-04 19:16:51 SERVER -> CLIENT: 250 2.0.0 OK 1622834211 w23sm2309597pfi.220 - gsmtp 2021-06-04 19:16:51 CLIENT -> SERVER: QUIT 2021-06-04 19:16:51 SERVER -> CLIENT: 221 2.0.0 closing connection w23sm2309597pfi.220 - gsmtp From my local script: Message sent! Then, same attempt using my paid account, here is the SERVER output: 2021-06-04 19:47:40 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w142sm2436849pff.154 - gsmtp 2021-06-04 19:47:40 CLIENT -> SERVER: EHLO localhost 2021-06-04 19:47:40 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [98.148.179.208]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2021-06-04 19:47:40 CLIENT -> SERVER: STARTTLS 2021-06-04 19:47:41 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2021-06-04 19:47:41 CLIENT -> SERVER: EHLO localhost 2021-06-04 19:47:41 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [98.148.179.208]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2021-06-04 19:47:41 CLIENT -> SERVER: AUTH LOGIN 2021-06-04 19:47:41 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2021-06-04 19:47:41 CLIENT -> SERVER: [credentials hidden] 2021-06-04 19:47:41 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2021-06-04 19:47:41 CLIENT -> SERVER: [credentials hidden] 2021-06-04 19:47:41 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials w142sm2436849pff.154 - gsmtp 2021-06-04 19:47:41 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 w142sm2436849pff.154 - gsmtp SMTP Error: Could not authenticate. 2021-06-04 19:47:41 CLIENT -> SERVER: QUIT 2021-06-04 19:47:41 SERVER -> CLIENT: 221 2.0.0 closing connection w142sm2436849pff.154 - gsmtp So, I've double double double checked the UN and PW and it is correct in the script. Thanks in advance for any and all help

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts