mathsgod / phpmailer-gmail
1.0.0
2025-05-27 10:37 UTC
Requires
- league/oauth2-google: ^4.0
- phpmailer/phpmailer: ^6.10
README
This project provides a GmailMailer
class for sending emails via Gmail SMTP using PHPMailer and OAuth2 authentication.
Features
- Send emails through Gmail SMTP securely
- Uses OAuth2 (no need for less secure app access)
- Simple integration with your PHP projects
Installation
-
Clone or download this repository.
-
Install dependencies using Composer:
composer install
Usage
-
Obtain your Gmail address, Google OAuth2 client ID, client secret, and refresh token.
-
Use the
GmailMailer
class in your PHP script:require 'vendor/autoload.php'; use PHPMailer\GmailMailer; $gmail = 'your-email@gmail.com'; $clientId = 'YOUR_GOOGLE_CLIENT_ID'; $clientSecret = 'YOUR_GOOGLE_CLIENT_SECRET'; $refreshToken = 'YOUR_REFRESH_TOKEN'; $mailer = new GmailMailer(true, $gmail, $clientId, $clientSecret, $refreshToken); $mailer->addAddress('recipient@example.com'); $mailer->Subject = 'Test Email'; $mailer->Body = 'This is a test email sent via Gmail SMTP with OAuth2.'; if ($mailer->send()) { echo "Message sent!"; } else { echo "Mailer Error: " . $mailer->ErrorInfo; }
How to Get OAuth2 Credentials
For detailed instructions on obtaining OAuth2 credentials and generating a refresh token, please refer to the thephpleague/oauth2-google GitHub repository.
License
MIT License.