PHPackages                             liwenyu/yii2-phpmailer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. liwenyu/yii2-phpmailer

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

liwenyu/yii2-phpmailer
======================

将 PHPMailer 封装成 Yii2 邮件发送组件，支持使用 Yii::$app-&gt;mail-&gt;send() 方式发送邮件

v1.1.1(6mo ago)018↓33.3%MITPHPPHP &gt;=7.0

Since Nov 5Pushed 6mo agoCompare

[ Source](https://github.com/liwenyu/yii2-phpmailer)[ Packagist](https://packagist.org/packages/liwenyu/yii2-phpmailer)[ RSS](/packages/liwenyu-yii2-phpmailer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (6)Used By (0)

Yii2 PHPMailer 组件
=================

[](#yii2-phpmailer-组件)

将 `phpmailer/phpmailer` 封装成 Yii2 邮件发送组件，支持使用 `Yii::$app->mail->send()` 方式发送邮件。

📁 文件结构
------

[](#-文件结构)

```
yii2-phpmailer/
├── src/
│   ├── Mailer.php      # Mailer 组件类
│   └── Message.php     # Message 消息类
├── config-example.php   # 配置示例
├── example.php         # 使用示例
└── README.md           # 说明文档

```

🚀 快速开始
------

[](#-快速开始)

### 1. 配置组件

[](#1-配置组件)

在 Yii2 应用配置中添加邮件组件：

```
'components' => [
    'mail' => [
        'class' => 'liwenyu\phpmailer\Mailer',
        'useMicrosoft365' => true,  // 使用 Microsoft 365
        'microsoft365Config' => [
            'clientId' => 'your-client-id',
            'clientSecret' => 'your-client-secret',
            'tenantId' => 'your-tenant-id',
            'username' => 'your-email@yourdomain.com',
        ],
    ],
],
```

### 2. 使用方式

[](#2-使用方式)

```
use Yii;

// 发送简单邮件
Yii::$app->mail->compose()
    ->setFrom('sender@example.com')
    ->setTo('recipient@example.com')
    ->setSubject('邮件主题')
    ->setHtmlBody('邮件内容')
    ->send();

// 发送带附件的邮件
Yii::$app->mail->compose()
    ->setFrom('sender@example.com')
    ->setTo('recipient@example.com')
    ->setSubject('带附件的邮件')
    ->setHtmlBody('这是一封带附件的邮件')
    ->attach('/path/to/file.pdf')
    ->send();
```

⚙️ 配置选项
-------

[](#️-配置选项)

### Microsoft 365 配置

[](#microsoft-365-配置)

```
'mail' => [
    'class' => 'liwenyu\phpmailer\Mailer',
    'useMicrosoft365' => true,
    'microsoft365Config' => [
        'clientId' => 'your-client-id',
        'clientSecret' => 'your-client-secret',
        'tenantId' => 'your-tenant-id',
        'username' => 'your-email@yourdomain.com',
    ],
],
```

### 传统 SMTP 配置

[](#传统-smtp-配置)

```
'mail' => [
    'class' => 'liwenyu\phpmailer\Mailer',
    'useMicrosoft365' => false,
    'phpmailerConfig' => [
        'host' => 'smtp.office365.com',
        'port' => 587,
        'encryption' => 'tls',
        'username' => 'your-email@yourdomain.com',
        'password' => 'your-password',
        // 'SMTPAuth' => true,  // 默认为 true，可省略
    ],
],
```

📧 功能特性
------

[](#-功能特性)

- ✅ 完全兼容 Yii2 Mailer 接口
- ✅ 支持 Microsoft 365（推荐）
- ✅ 支持传统 SMTP 发送
- ✅ 支持 HTML 和纯文本邮件
- ✅ 支持附件和嵌入图片
- ✅ 支持抄送、密送、回复地址
- ✅ OAuth 2.0 自动令牌管理

🎯 API 使用
--------

[](#-api-使用)

### 基本方法

[](#基本方法)

```
// 创建消息
$message = Yii::$app->mail->compose();

// 设置发件人
$message->setFrom('sender@example.com');
$message->setFrom('sender@example.com', '发件人名称');

// 设置收件人
$message->setTo('recipient@example.com');
$message->setTo(['user1@example.com', 'user2@example.com']);

// 设置抄送和密送
$message->setCc('cc@example.com');
$message->setBcc('bcc@example.com');

// 设置回复地址
$message->setReplyTo('reply@example.com');

// 设置主题和内容
$message->setSubject('邮件主题');
$message->setTextBody('纯文本内容');
$message->setHtmlBody('HTML 内容');

// 添加附件
$message->attach('/path/to/file.pdf');
$message->attachContent('附件内容', ['fileName' => 'file.txt']);

// 嵌入图片
$cid = $message->embed('/path/to/image.png');
$message->setHtmlBody('');

// 发送邮件
Yii::$app->mail->send($message);
```

🔧 技术实现
------

[](#-技术实现)

### Mailer 类

[](#mailer-类)

- 继承自 `yii\mail\BaseMailer`
- 支持两种发送方式：
    - Microsoft 365（使用客户端凭据流）
    - 传统 SMTP

### Message 类

[](#message-类)

- 实现 `yii\mail\MessageInterface`
- 封装 PHPMailer 消息对象
- 提供链式调用接口

📝 注意事项
------

[](#-注意事项)

1. **Microsoft 365 方式**：

    - 使用客户端凭据流（Client Credentials Flow）
    - 不需要用户交互
    - 自动管理访问令牌
2. **SMTP 方式**：

    - 需要配置 SMTP 服务器信息
    - 支持 XOAUTH2（需要用户授权）
3. **配置要求**：

    - Azure 应用需要 `Mail.Send` 权限
    - 管理员需要同意权限

🎉 使用示例
------

[](#-使用示例)

查看 `example.php` 文件获取完整的使用示例。

📚 相关文档
------

[](#-相关文档)

- [PHPMailer 文档](https://github.com/PHPMailer/PHPMailer)
- [Yii2 Mailer 文档](https://www.yiiframework.com/doc/guide/2.0/en/tutorial-mailing)
- [Microsoft Graph API 文档](https://docs.microsoft.com/en-us/graph/api/resources/mail-api-overview)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance68

Regular maintenance activity

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

188d ago

Major Versions

v0.3 → v1.0.02025-11-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/32881584b4218be963ef7863b7b35de612c2c6e934ef945091fbf18f6c06e51e?d=identicon)[liwenyu](/maintainers/liwenyu)

---

Top Contributors

[![liwenyu](https://avatars.githubusercontent.com/u/6909144?v=4)](https://github.com/liwenyu "liwenyu (1 commits)")

---

Tags

mailemailyii2smtpgraph-apiphpmailer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/liwenyu-yii2-phpmailer/health.svg)

```
[![Health](https://phpackages.com/badges/liwenyu-yii2-phpmailer/health.svg)](https://phpackages.com/packages/liwenyu-yii2-phpmailer)
```

###  Alternatives

[rmrevin/yii2-postman

Mail module for Yii2.

2612.3k](/packages/rmrevin-yii2-postman)[boundstate/yii2-mailgun

Mailgun integration for the Yii framework

28160.6k](/packages/boundstate-yii2-mailgun)[yarcode/yii2-mailgun-mailer

Mailgun mailer implementation for Yii2

1576.0k](/packages/yarcode-yii2-mailgun-mailer)[tigrov/yii2-mailqueue

Yii2 mail queue component for yii2-swiftmailer.

186.1k](/packages/tigrov-yii2-mailqueue)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
