ad5jp / laravel-gmail
Laravel Mail Driver for Gmail API via Service Account
Installs: 3 761
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.2|^8.0
- google/apiclient: ^2.0
- google/apiclient-services: >=0.200.0
- laravel/framework: ^9.0|^10.0|^11.0
README
This package allows you to use the Gmail API as a mailer in Laravel. You can send emails from Laravel via the Gmail API without modifying your existing code.
Instead of using OAuth, this package uses a service account for sending emails. This eliminates the need for user authentication processes and access token management, while ensuring the method does not fall under LSA (Less Secure Apps).
Installation
composer require ad5jp/laravel-gmail
Specify the version according to your environment:
- v4.x ... Laravel 9 and later
- v3.x ... Laravel 7 - 8
- v2.x ... Laravel 6 and earlier
- v1.x ... Laravel 6 and earlier with google/apiclient-service before v0.200.0
Usage
1. Create a Service Account on Google Cloud Platform
- Create a project in the Google Cloud Platform console.
- Enable the Gmail API.
- Create a service account, generate a key, and download it in JSON format.
- In the Google Workspace admin console, set up "Domain-wide Delegation". Assign the scope
https://www.googleapis.com/auth/gmail.send
to the service account created above.
2. Place the Key
Place the JSON key downloaded in step 2 within your application. Ensure it is excluded from source control using .gitignore or similar.
3. Configure .env
Change the value of MAIL_MAILER
in your .env file to gmail
. (Or change the 'default' value in config/mail.php)
For Laravel 6 and earlier, change the value of MAIL_DRIVER
in your .env file to gmail
. (Or change the 'driver' value in config/mail.php)
Add the following lines to your .env file:
GMAIL_FROM_ADDRESS={Sender's email address}
GMAIL_SERVICE_ACCOUNT_KEY={Path to the JSON key placed in step 2}
The sender's email address must be an email address within the Google Workspace organization set up in step 1 (4).