定制 jlorente/yii2-brevo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jlorente/yii2-brevo

最新稳定版本:1.0.2

Composer 安装命令:

composer require jlorente/yii2-brevo

包简介

Yii2 integration for Brevo (Sendinblue) mailer over brevo-php SDK

README 文档

README

Yii2 mailer connector for Brevo (formerly Sendinblue).
Provides a seamless integration of the official brevo-php SDK into Yii2.

This component lets you send either full custom HTML emails or Brevo transactional templates, and you can deliver them through Brevo’s Transactional REST API (via the official SDK) or through Brevo’s SMTP relay.

Features include dynamic template parameters, custom subjects, attachments, CC/BCC, default sender configuration, and sandbox mode for safe testing.

Installation

Package installation

To install, either run

$ php composer.phar require jlorente/yii2-brevo "*"

or

$ composer require jlorente/yii2-brevo "*"

or add

...
    "require": {
        // ... other configurations ...
        "jlorente/yii2-brevo": "*"
    }

to the require section of your composer.json file and run the following command from your project directory.

$ composer update

Component configuration

Then configure the component in your Yii2 application (for example in config/main.php or common/config/main.php):

...
    'components' => [
        // ... other configurations ...
        'mailer' => [
            'class' => \jlorente\brevo\Mailer::class,
            'apiKey' => env('BREVO_API_KEY'),
            'defaultSender' => [
                'email' => 'noreply@your-domain.com',
                'name'  => 'My App',
            ],
            'sandboxEmail' => 'dev@your-domain.com', // or an array of addresses
            'guzzleConfig' => [
                'timeout' => 10.0,
                // add proxy or other Guzzle options if needed
            ],
            'useFileTransport' => false,
            'viewPath' => '@app/mail',
        ],
    ],
...

Basic Usage

    Yii::$app->mailer->compose()
        ->setTo('user@example.com')
        ->setSubject('Hello from Brevo')
        ->setHtmlBody('<h1>Hello</h1><p>HTML body</p>')
        ->setTextBody('Hello — plain text body')
        ->send();

Using Brevo Templates

    Yii::$app->mailer->compose()
        ->setTo('user@example.com')
        ->setTemplateId(123456)
        ->setParams([
            'firstName' => 'John',
            // other template parameters
        ])
        ->send();

Attachments

Attach a file from disk:

    Yii::$app->mailer->compose()
        ->setTo('user@example.com')
        ->setSubject('Report attached')
        ->setHtmlBody('<p>Please find the report attached</p>')
        ->attach('/path/report.pdf', [
            'fileName' => 'report.pdf',
            'contentType' => 'application/pdf',
        ])
        ->send();

Attach content from memory:

    $binary = file_get_contents('/path/generated.pdf');
    Yii::$app->mailer->compose()
        ->setTo('user@example.com')
        ->setSubject('In-memory attachment')
        ->setHtmlBody('<p>See attached file</p>')
        ->attachContent($binary, [
            'fileName' => 'generated.pdf',
            'contentType' => 'application/pdf',
        ])
        ->send();

Inline / Embed Notes

  • Methods embed() and embedContent() exist to satisfy the Yii2 MessageInterface.
  • Warning: the Brevo transactional REST API does not support true inline (CID) images.
    Calling these methods will log a warning and the resource will be sent as a regular attachment, not as an inline MIME part.

Sandbox Mode

If sandboxEmail (string or array) is configured, every outgoing email is redirected to that address or list of addresses, ignoring the original recipients.
This is useful for development, staging, or automated tests to prevent accidental delivery.

Technical Notes

  • Uses the official getbrevo/brevo-php SDK.
  • A default sender (defaultSender) is applied if no From address is set on the message.
  • guzzleConfig lets you customize the underlying Guzzle HTTP client (timeouts, proxy, etc.).
  • After sending, you can access the Brevo response via Message::getLastResponse() or check exceptions with Message::getLastException().
  • Calls to embed() or embedContent() will log a Yii warning.

Contributing

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature/my-feature.
  3. Add tests if possible.
  4. Submit a pull request with a clear description.

License

Copyright © 2025 José Lorente Martín jose.lorente.martin@gmail.com.

Licensed under the MIT license. See LICENSE.txt for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固