orange-soft/laravel-gmail-mailer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Gmail API:
    • Navigate to "APIs & Services" > "Library"
    • Search for "Gmail API" and enable it
  4. 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
  5. Save the credentials file:
    • Default location: storage/app/gmail/credentials.json
    • Or set a custom path via GMAIL_CREDENTIALS_PATH

Setup OAuth Token

Run the setup command to authenticate with your Google account:

php artisan os:gmail-mailer:setup

This will:

  1. Display an authorization URL
  2. Ask you to open the URL in your browser
  3. Prompt you to enter the authorization code
  4. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 orange-soft/laravel-gmail-mailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-24