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 do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer to send emails with a Yahoo account?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to support Polish characters?
- How do I configure PHPMailer to use SSL?
- How do I use PHPMailer with OAuth2 authentication for Microsoft accounts?
- How do I use PHPMailer with IMAP?
- How can I use PHPMailer to send emails through Yandex?
- How can I use PHPMailer with XAMPP on a localhost?
See more codes...