phpmailerHow do I configure PHPMailer to use Gmail OAuth authentication?
To configure PHPMailer to use Gmail OAuth authentication, you will need to have the latest version of PHPMailer installed.
You will also need to have the OAuth 2.0 Client library installed.
Once these are installed, you can use the following code to set up PHPMailer to use Gmail OAuth authentication:
require 'vendor/autoload.php';
$mail = new PHPMailerOAuth;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->AuthType = 'XOAUTH2';
$mail->oauthUserEmail = '[email protected]';
$mail->oauthClientId = 'your_client_id';
$mail->oauthClientSecret = 'your_client_secret';
$mail->oauthRefreshToken = 'your_refresh_token';
Code explanation
require 'vendor/autoload.php';
: this line is used to include the autoload file from the OAuth 2.0 Client library.$mail = new PHPMailerOAuth;
: this line creates a new instance of the PHPMailerOAuth class.$mail->isSMTP();
: this line sets the mailer to use SMTP.$mail->SMTPDebug = 0;
: this line sets the SMTP debugging level.$mail->Host = 'smtp.gmail.com';
: this line sets the host to Gmail's SMTP server.$mail->Port = 587;
: this line sets the SMTP port.$mail->SMTPSecure = 'tls';
: this line sets the SMTP encryption type.$mail->AuthType = 'XOAUTH2';
: this line sets the authentication type to OAuth 2.0.$mail->oauthUserEmail = '[email protected]';
: this line sets the Gmail address to use for authentication.$mail->oauthClientId = 'your_client_id';
: this line sets the OAuth client ID.$mail->oauthClientSecret = 'your_client_secret';
: this line sets the OAuth client secret.$mail->oauthRefreshToken = 'your_refresh_token';
: this line sets the OAuth refresh token.
No output is produced.
Helpful links
More of Phpmailer
- How can I use PHPMailer without SMTP secure?
- How do I use PHPMailer to attach a ZIP file?
- How do I use PHPMailer with OAuth2 authentication for Microsoft accounts?
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How can I configure PHPMailer to ignore TLS certificate errors?
- How do I configure PHPMailer to use a proxy?
- How can I use PHPMailer to connect to a POP3 server?
- How do I get the message ID of a sent email using PHPMailer?
- How to use PHPMailer with PHP 7.4?
See more codes...