Author: Manuel Lemos
Updated on: 2022-11-11
Posted on: 2022-10-31
Viewers: 209 (October 2022 until August 2023)
Last month viewers: 1 (August 2023)
Package: MIME E-mail message sending
In 2022 Google made the use of Gmail as a means to send email messages via Gmail SMTP server more secure by requiring an OAuth access token instead of the traditional password of the Gmail user account.
This requirement made many developers update their PHP applications to use OAuth tokens and continue to use the Gmail SMTP server for free.
Read this article to learn how to use OAuth access tokens to send email messages from PHP applications using the Gmail SMTP server.
In this article you can learn about:
1. Why You Need to Use OAuth Based Authentication to Send Email via Gmail SMTP Server
2. Whats is the OAuth Protocol
3. How do You Send Email Messages using Gmail Free Accounts and Use OAuth Based Protocol
3.1 How to Get the OAuth Access Token to Connect to Gmail SMTP Server
3.2 How to Use the gmail_smtp_mail Function to Send Messages Using the Gmail SMTP Server
4. How Can You Install the PHP MIME Message, SASL, SMTP and OAuth Packages Using PHP Composer or Download the Packages Separately
5. Next: How Can You Make the OAuth Token Renew Automatically Without Human Intervention
6. Suggestions for Other Articles about the PHP MIME Message Package
1. Why You Need to Use OAuth Based Authentication to Send Email via Gmail SMTP Server
Gmail is a popular email system used by many users for free. It also allows users to send email messages using the Gmail SMTP server, entering the user's email address and password to control access to the SMTP server.
Since May 30, 2022, this form of authentication is no longer allowed for free Gmail users. If you are a GSuite user that pays for a subscription, you can still use the email and password authentication form for a while.
Free Gmail users need to switch the authentication form to OAuth tokens instead of the user password.
This change is more secure because applications do not need to use the user password to authenticate. The OAuth tokens are also only valid for 1 hour. After that period, applications need to get a new OAuth token to continue to send email messages for free using the Gmail SMTP server.
Here you have some documentation on the access to Google services from less secure applications. If you prefer to read an article in Portuguese, you can read about the access to Gmail services after these security changes.
2. Whats is the OAuth Protocol
OAuth is a protocol that allows sites to obtain the permission of users to send API requests on behalf of a given user and execute certain operations.
For instance, you can use the OAuth protocol with an application that you can develop to send email messages using the Gmail API on behalf of Gmail users that give your application API access permissions.
You can use he OAuth protocol to retrieve an API access token that the Gmail SMTP server accepts to allow sending email on behalf of a given user that granted access to your application to use the Gmail API.
If you want to access a OAuth server from PHP, you use a PHP OAuth Client package.
3. How do You Send Email Messages using Gmail Free Accounts and Use OAuth Based Protocol
The process to send email messages using Gmail and a OAuth token is very similar to send message to any SMTP server that requires a user name an a password.
The main difference is that instead of a password you need to pass a OAuth token that you need to previously obtain using the OAuth dialog process.
All this can be done in PHP. Since the actual code to do the process in pure PHP is a bit complex, I have developed a wrapper around the SMTP sub-class of the MIME message that you can use as replacement for the mail function that comes built-in PHP.
This way you only need to replace mail function calls with calls to a function with named gmail_smtp_mail that takes the same parameters as the mail function.
3.1 How to Get the OAuth Access Token to Connect to Gmail SMTP Server
After creating the application, you need to have a Web page that starts the OAuth token retrieval process. That page redirects the user browser to a Google site that requests permission from the current user to allow your application access to the Gmail API.
When that page finishes with the OAuth authorization process, you can get the OAuth access token to configure your email delivery code and allow your code to authenticate with the Gmail SMTP server.
A ready-to-use script simplifies your effort to work as a Web page, starts the OAuth process, and shows the OAuth access token at the end.
3.2 How to Use the gmail_smtp_mail Function to Send Messages Using the Gmail SMTP Server
Once you have the OAuth access token, you can use it directly in your code that sends the email messages to the Gmail SMTP server.
The MIME message package comes with a script named test_smtp_gmail.php that can be configured to send email messages using the Gmail SMTP server using the OAuth SMTP token retrieved in the previous step.
You can run the test_smtp_gmail.php script without a Web server by using the PHP CLI version. For instance you can run it from the command line interface shell on a Linux, MacOS or Windows machine.
4. How Can You Install the PHP MIME Message, SASL, SMTP and OAuth Packages Using PHP Composer or Download the Packages Separately
The MIME message package requires several other packages to send via the Gmail SMTP server.
One of those packages is the SMTP client class. It performs the actual connection and transmission of the email message data.
Another package that is necessary is the SASL package. It dialogs with the SMTP servers. It implements different types of authentication, including XOAUTH2, which is the name of the authentication mechanism that requires the OAuth access tokens.
You also need the OAuth client class package to implement a Web page that can retrieve the OAuth authorization process to retrieve the OAuth access tokens.
If you prefer, you can download the code of all these packages by going to the Download tab of the MIME Message package.
However, if you already use the PHP Composer tool, you can insert a few configuration lines in your application composer.json file. You can get a sample of those configuration lines in the Download tab of the MIME Message package and then click in the Install with Composer button.
5. Next: How Can You Make the OAuth Token Renew Automatically Without Human Intervention
The process described in this article to send email messages via Gmail SMTP server works well.
Still, it has one limitation. The OAuth access tokens issued by Google to access Gmail API are only valid for 1 hour. There is a way to renew those tokens automatically. I will leave that possibility for an article specifically about that topic that you can read in the page about How Can PHP Send Email Using Gmail SMTP Server for Free Using OAuth Tokens That Renew Automatically .
6. Suggestions for Other Articles about the PHP MIME Message Package
If you like articles like this and want more articles to teach other aspects about sending email messages, please post a comment below so I can plan more articles to address your interests.
You need to be a registered user or login to post a comment
1,611,081 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
1. A working example, please - This One (2022-10-31 13:30)
PHP SMTP Gmail Replacement... - 1 reply
Read the whole comment and replies