PHPackages                             perfectpanel/extlib-yii2-sendgrid - 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. perfectpanel/extlib-yii2-sendgrid

ActiveYii2-extension[Mail &amp; Notifications](/categories/mail)

perfectpanel/extlib-yii2-sendgrid
=================================

Yii2 Mailer extension for SendGrid with batch mailing support. This extension is designed to replace them all! The only Yii2 SendGrid extension you will need!

v2.1(3y ago)04.0k1MITPHPPHP &gt;=5.4.0

Since Aug 29Pushed 3y agoCompare

[ Source](https://github.com/perfectpanel/extlib-yii2-sendgrid)[ Packagist](https://packagist.org/packages/perfectpanel/extlib-yii2-sendgrid)[ Docs](https://github.com/perfectpanel/yii2-sendgrid-mailer)[ RSS](/packages/perfectpanel-extlib-yii2-sendgrid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

yii2-sendgrid
=============

[](#yii2-sendgrid)

Yii2 Mailer extension for SendGrid with batch mailing support. This extension is designed to replace them all! The only Yii2 SendGrid extension you will need!

---

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist perfectpanel/yii2-sendgrid

```

or add

```
"perfectpanel/yii2-sendgrid": "1.0"
```

to the require section of your application's `composer.json` file.

Then configure your `mailer` component in your `main-local.php` (advanced) or `web.php` (basic) like so:

```
'mailer' => [
    'class' => 'shulyak\sendgrid\Mailer',
    'viewPath' => '@common/mail',
    // send all mails to a file by default. You have to set
    // 'useFileTransport' to false and configure a transport
    // for the mailer to send real emails.
    'useFileTransport' => false,
    'apiKey' => '[YOUR_SENDGRID_API_KEY]',
],

```

Do not forget to replace `apiKey` with your SendGrid API key. It must have permissions to send emails.

Usage
-----

[](#usage)

### Single Mailing

[](#single-mailing)

```
$user = \common\models\User::find()->select(['id', 'username', 'email'])->where(['id' => 1])->one();

$mailer = Yii::$app->mailer;
$message = $mailer->compose()
    ->setTo([$user->email => $user->username])      // or just $user->email
    ->setFrom(['alerts@example.com' => 'Alerts'])
    ->setReplyTo('noreply@example.com')
    ->setSubject('Hey -username-, Read This Email')
    ->setHtmlBody('Dear -username-,My HTML message here')
    ->setTextBody('Dear -username-,\n\nMy Text message here')
    //->setTemplateId('1234')
    //->addSection('%section1%', 'This is my section1')
    //->addHeader('X-Track-UserType', 'admin')
    //->addHeader('X-Track-UID', Yii::$app->user->id)
    //->addCategory('tests')
    //->addCustomArg('test_arg', 'my custom arg')
    //->setSendAt(time() + (5 * 60))
    //->setBatchId(Yii::$app->mailer->createBatchId())
    //->setIpPoolName('7')
    //->attach(Yii::getAlias('@webroot/files/attachment.pdf'))
    ->addSubstitution('-username-', $user->username)
    ->send();

if ($message === true) {
    echo 'Success!';
    echo '' . print_r($mailer->getRawResponses(), true) . '';
} else {
    echo 'Error!';
    echo '' . print_r($mailer->getErrors(), true) . '';
}

```

### Batch Mailing

[](#batch-mailing)

If you want to send to multiple recipients, you need to use the below method to batch send.

```
$mailer = Yii::$app->mailer;
//$batchId = Yii::$app->mailer->createBatchId();
//$sendTime = time() + (5 * 60);      // 5 minutes from now

foreach (User::find()->select(['id', 'username', 'email'])->batch(500) as $users)
{

    $message = $mailer->compose()
        ->setFrom(['alerts@example.com' => 'Alerts'])
        ->setReplyTo('noreply@example.com')
        ->setSubject('Hey -username-, Read This Email')
        ->setHtmlBody('Dear -username-,My HTML message here')
        ->setTextBody('Dear -username-,\n\nMy Text message here');
        //->setTemplateId('1234')
        //->addSection('%section1%', 'This is my section1')
        //->addHeader('X-Track-UserType', 'admin')
        //->addHeader('X-Track-UID', Yii::$app->user->id)
        //->addCategory('tests')
        //->addCustomArg('test_arg', 'my custom arg')
        //->setSendAt($sendTime)
        //->setBatchId($batchId)
        //->setIpPoolName('7')
        //->attach(Yii::getAlias('@webroot/files/attachment.pdf'));

    foreach ( $users as $user )
    {
        // A Personalization Object Helper would be nice here...
        $personalization = [
            'to' => [$user->email => $user->username],      // or just `email@example.com`
            //'cc' => 'cc@example.com',
            //'bcc' => 'bcc@example.com',
            //'subject' => 'Hey -username-, Custom message for you!',
            //'headers' => [
            //    'X-Track-RecipId' => $user->id,
            //],
            'substitutions' => [
                '-username-' => $user->username,
            ],
            //'custom_args' => [
            //    'user_id' => $user->id,
            //    'type' => 'marketing',
            //],
            //'send_at' => $sendTime,
        ];
        $message->addPersonalization($personalization);
    }

    $result = $message->send();
}

if ($result === true) {
    echo 'Success!';
    echo '' . print_r($mailer->getRawResponses(), true) . '';
} else {
    echo 'Error!';
    echo '' . print_r($mailer->getErrors(), true) . '';
}

```

**NOTE:** SendGrid supports a max of 1,000 recipients. This is a total of the to, bcc, and cc addresses. I recommend using `500` for the batch size. This should be large enough to process thousands of emails efficiently without risking getting errors by accidentally breaking the 1,000 recipients rule. If you are not using any bcc or cc addresses, you *could* raise the batch number a little higher. Theoretically, you should be able to do 1,000 but I would probably max at 950 to leave some wiggle room.

---

Known Issues
------------

[](#known-issues)

- `addSection()` - There is currently an issue with the SendGrid API where sections are not working.
- `setSendAt()` - There is currently an issue with the SendGrid API where using `send_at` where the time shows the queued time not the actual time that the email was sent.
- `setReplyTo()` - There is currently an issue with the SendGrid PHP API where the ReplyTo address only accepts the email address as a string. So you can't set a name.

---

TODO
----

[](#todo)

There are a few things left that I didn't get to:

- ASM
- mail\_settings
- tracking\_settings

I plan to get to them at a later date. Feel free to help out if you can :)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% 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 ~6 days

Total

3

Last Release

1335d ago

Major Versions

v1.0 → v2.02022-09-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/c70f1c5a9eadef73fdce57d8929188c6f0df681dcf4a73bd4af5a208d8be511c?d=identicon)[Gorchel567](/maintainers/Gorchel567)

---

Top Contributors

[![Gorchel567](https://avatars.githubusercontent.com/u/75777865?v=4)](https://github.com/Gorchel567 "Gorchel567 (3 commits)")[![shtiher-pp](https://avatars.githubusercontent.com/u/98802205?v=4)](https://github.com/shtiher-pp "shtiher-pp (2 commits)")

---

Tags

sendgridyii2yii2 maileryii2 sendgridyii2 sendgrid mailer

### Embed Badge

![Health badge](/badges/perfectpanel-extlib-yii2-sendgrid/health.svg)

```
[![Health](https://phpackages.com/badges/perfectpanel-extlib-yii2-sendgrid/health.svg)](https://phpackages.com/packages/perfectpanel-extlib-yii2-sendgrid)
```

###  Alternatives

[loveorigami/yii2-notification-wrapper

This module for renders a message from session flash (with ajax, pjax support and etc.)

77199.7k5](/packages/loveorigami-yii2-notification-wrapper)[bryglen/yii2-sendgrid

Sendgrid Mailer for Yii 2

1353.5k](/packages/bryglen-yii2-sendgrid)

PHPackages © 2026

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