orange-soft/laravel-gmail-mailer
Composer 安装命令:
composer require orange-soft/laravel-gmail-mailer
包简介
Laravel mail transport using Gmail API with OAuth2 authentication
README 文档
README
A Laravel mail transport using Gmail API with OAuth2 authentication. This package provides a secure alternative to SMTP for sending emails through Gmail.
Requirements
- PHP 8.0.2+
- Laravel 9.0, 10.0, 11.0, or 12.0
- A Google Cloud project with Gmail API enabled
Installation
Install the package via Composer:
composer require orange-soft/laravel-gmail-mailer
The package will auto-register its service provider.
Publish Configuration
php artisan vendor:publish --tag=gmail-mailer-config
Google Cloud Setup
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Gmail API:
- Navigate to "APIs & Services" > "Library"
- Search for "Gmail API" and enable it
- Create OAuth2 credentials:
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth client ID"
- Select "Desktop app" as the application type
- Download the credentials JSON file
- Save the credentials file:
- Default location:
storage/app/gmail/credentials.json - Or set a custom path via
GMAIL_CREDENTIALS_PATH
- Default location:
Setup OAuth Token
Run the setup command to authenticate with your Google account:
php artisan os:gmail-mailer:setup
This will:
- Display an authorization URL
- Ask you to open the URL in your browser
- Prompt you to enter the authorization code
- Save the OAuth token for future use
Configuration
Environment Variables
Add to your .env file:
MAIL_MAILER=gmail-oauth2 # Optional: Custom paths for credentials and token GMAIL_CREDENTIALS_PATH=/path/to/credentials.json GMAIL_TOKEN_PATH=/path/to/token.json # Optional: Application name shown in Google OAuth consent GMAIL_APP_NAME="My Application" # Optional: Default test recipient for the test command GMAIL_TEST_RECIPIENT=test@example.com # Optional: Logging GMAIL_LOGGING_ENABLED=true GMAIL_LOG_CHANNEL=stack
Mail Configuration
Add the Gmail mailer to your config/mail.php:
'mailers' => [ // ... other mailers 'gmail-oauth2' => [ 'transport' => 'gmail-oauth2', ], ],
Usage
Send Email via Laravel Mail Facade
use Illuminate\Support\Facades\Mail; use App\Mail\WelcomeEmail; // Send using the default mailer (when MAIL_MAILER=gmail-oauth2) Mail::to('user@example.com')->send(new WelcomeEmail()); // Or explicitly use the Gmail mailer Mail::mailer('gmail-oauth2') ->to('user@example.com') ->send(new WelcomeEmail());
Direct Service Usage
use Orangesoft\GmailMailer\Services\GmailService; $gmail = app(GmailService::class); $gmail->sendEmail( to: 'recipient@example.com', subject: 'Hello World', body: '<h1>Welcome!</h1><p>This is a test email.</p>', options: [ 'from' => 'sender@gmail.com', 'fromName' => 'My App', 'cc' => ['cc@example.com'], 'bcc' => ['bcc@example.com'], 'replyTo' => 'reply@example.com', ] );
Commands
Setup OAuth
php artisan os:gmail-mailer:setup
# With custom paths
php artisan os:gmail-mailer:setup --credentials=/path/to/credentials.json --token=/path/to/token.json
Test Email
# Send a test email php artisan os:gmail-mailer:test user@example.com # With custom subject php artisan os:gmail-mailer:test user@example.com --subject="Custom Subject" # Check token status only (no email sent) php artisan os:gmail-mailer:test --check-only
Token Refresh
The package automatically refreshes expired OAuth tokens. If the refresh token becomes invalid, re-run:
php artisan os:gmail-mailer:setup
Troubleshooting
Token Expired Error
If you see "Gmail OAuth token has expired and cannot be refreshed", run:
php artisan os:gmail-mailer:setup
Credentials Not Found
Ensure your credentials file exists at the configured path:
ls -la storage/app/gmail/credentials.json
Permission Denied
Check file permissions:
chmod 600 storage/app/gmail/token.json chmod 600 storage/app/gmail/credentials.json
Check Configuration
Verify your setup:
php artisan os:gmail-mailer:test --check-only
Security
- OAuth tokens are stored with restricted permissions (0600)
- Credentials and tokens should be added to
.gitignore - Never commit credential or token files to version control
Add to your .gitignore:
storage/app/gmail/credentials.json
storage/app/gmail/token.json
License
This package is open-sourced software licensed under the MIT license.
orange-soft/laravel-gmail-mailer 适用场景与选型建议
orange-soft/laravel-gmail-mailer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「mailer」 「oauth2」 「laravel」 「transport」 「gmail」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 orange-soft/laravel-gmail-mailer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 orange-soft/laravel-gmail-mailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 orange-soft/laravel-gmail-mailer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
Creates new sare mail transport for laravel applications
Email Toolkit Plugin for CakePHP
Mail libraries used by Zimbra Api
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-24