PHP Classes

How Can PHP Send Email Using Gmail SMTP Server for Free Using OAuth Tokens That Renew Automatically - MIME E-mail message sending package blog

Recommend this page to a friend!
  All package blogs All package blogs   MIME E-mail message sending MIME E-mail message sending   Blog MIME E-mail message sending package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can PHP Send Emai...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 248

Last month viewers: 2

Package: MIME E-mail message sending

The Gmail SMTP server has required OAuth-based authentication to send email messages since May 2022 for Gmail users of free accounts.

The OAuth process obtains token string values from the Gmail OAuth server. These token strings are a sort of password to access the Gmail SMTP server.

The token strings are valid for a period that currently is 1 hour. After that period, applications that use the Gmail SMTP server will have to renew the tokens using the OAuth process again.

The MIME Message package uses the SASL package to perform authorization. The XOAUTH2 driver class of the SASL package can renew the OAuth token automatically when necessary.

Please read this article to learn with example code how to use the PHP MIME Message, SMTP Client, SASL, and OAuth client packages to send email messages using PHP with the Gmail SMTP server in a way that does not require human intervention to renew the OAuth tokens automatically after they expire.





Loaded Article

In this article you can learn about:

1. Why You Need to Renew the OAuth Tokens When You Send Email Messages via the Gmail SMTP Server

2. What Are OAuth Refresh Tokens

3. How do You Send Many Email Messages using Gmail Free Accounts and Use OAuth Refresh Tokens Automatically

4. How Can You Install the PHP MIME Message, SASL, SMTP and OAuth Packages Using PHP Composer or Download the Packages Separately

5. Suggestions for Other Articles about the PHP MIME Message Package


1. Why You Need to Renew the OAuth Tokens When You Send Email Messages via the Gmail SMTP Server

As I have explained in the previous article about How to Implement a PHP SMTP Gmail Replacement for the PHP mail Function that Works with the Gmail OAuth Authentication, you can send email messages using the Gmail SMTP server using free accounts as long as you use the OAuth authentication process.

This process requires that you obtain an OAuth access token first. That token will be used as a sort of password instead of the regular user password when you use the XOAUTH2 authentication mechanism.

The access tokens are valid for a given period. Usually, that period is 1 hour. After that, it is necessary to renew the access token.

2. What Are OAuth Refresh Tokens

When an application that retrieves a OAuth token from an API server, like for instance the Gmail API server, the access token may come together with a OAuth refresh token.

The refresh token is necessary to obtain a new access token when the old access token expires.

The OAuth API server verifies if the refresh token is valid and returns a new access token together with a new refresh token.

So when the new access token expires, it is necessary to repeat the process to obtain another access token and another refresh token.

This way, you can use the OAuth access token refresh process to always be able to get a new access token when it expires.

3. How do You Send Many Email Messages using Gmail Free Accounts and Use OAuth Refresh Tokens Automatically

You may realize that the OAuth protocol is a bit complex if you read the article sections above.

Fortunately, you do not need to learn how to implement it in detail because there is a PHP OAuth Client package that you can use to implement it.

There is also the SASL package that takes care of the authentication of users when they access networking servers, like the SMTP server that Gmail provides for you to send your email messages.

The PHP SMTP Client package uses the SASL package to implement several authentication protocols, such as XOAUTH2. That is the name of the protocol based on the OAuth protocol that SMTP servers use, like the Gmail SMTP server.

Finally, there is also the PHP MIME Message package that can compose email messages that may include a text part, HTML part, images, attached files, and other types of files you can send using email messages.

If you have a PHP application that uses the mail() function built-in PHP, the PHP MIME message package provides an alternative function named gmail_smtp_mail() that is equivalent to the mail() function. It uses the Gmail SMTP server instead of the email system that PHP configures to send your messages.

3.1 How to Use the gmail_smtp_mail function to Send Email Messages via the Gmail SMTP Server Using OAuth Access Tokens that Refresh Automatically When They Expire

The PHP MIME message package provides an example script named test_smtp_gmail_with_oauth_token_file.php that you can use to try sending email messages with the OAuth tokens that refresh automatically.

You can find that script on the PHP MIME message package page. It would be best if you changed a few values in this script to adapt and make it work with your Gmail account.

You can also adapt this script to work with other SMTP servers supporting the OAuth protocol authentication mechanism.

Let me show you an example of the changes that you need to make:

// The path of a local file that will store OAuth token data obtained from the Gmail API OAuth server

$message_object->smtp_token_file = 'Gmail-oauth-token.json';


// Your Gmail account email address

$message_object->smtp_user = "your-Gmail-email-address@gmail.com";


// The XOAUTH2 authentication mechanism

$message_object->smtp_authentication_mechanism = 'XOAUTH2';


// The name of the driver of the OAuth client package that Gmail API uses

$message_object->smtp_authentication_server = 'Google';


// The URL of the script that you need to use to obtain the first OAuth access token
// Read the previous article on how to get the URL of this script

$message_object->smtp_authentication_redirect_uri = 'https://www.your-web-server.com/path/to/get_gmail_oauth_access_token.php


// The client id of your Gmail API application
// Read the previous article on how to get this client id value
$message_object->smtp_authentication_client_id = 'your Gmail API application client id';

// The client secret of your Gmail API application
// Read the previous article on how to get this client secret value
$message_object->smtp_authentication_client_secret = 'your Gmail API application client secret';

// The Gmail API scope list
$message_object->smtp_authentication_scope = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://mail.google.com/';
The rest of the test_smtp_gmail_with_oauth_token_file.php script you can leave as it is. Let me show you the commented version of the end of the script.

// This is your Gmail account email address defined above

$from = $message_object->smtp_user;


// This is the message recipient email address.
// You can change or leave as it is to send the message to me.

$to = "mlemos@acm.org";


// The subject of the message
$subject = "Testing gmail_smtp_mail function";


// The message body text

$message = "Hello,\n\nThis message is just to let you know that the smtp_mail() function is working fine as expected.\n\n$from";


// Additional headers of the message

$additional_headers = "From: $from";


// Additional parameters of the message
$additional_parameters = "-f ".$from;


// Call the gmail_smtp_mail() function checking of there any errors

if(gmail_smtp_mail($to,$subject,$message,$additional_headers,$additional_parameters))
  echo "Ok.";
else
  echo "Error: ".$message_object->error."\n";

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. 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,614,687 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   MIME E-mail message sending MIME E-mail message sending   Blog MIME E-mail message sending package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can PHP Send Emai...