2017 © Pedro Peláez
 

yii2-extension 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!

image

skn/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!

  • Tuesday, June 5, 2018
  • by skn
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

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!, (*1)


Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist wadeshuler/yii2-sendgrid

or add, (*4)

"wadeshuler/yii2-sendgrid": "~1.0"

to the require section of your application's composer.json file., (*5)

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

'mailer' => [
    'class' => 'wadeshuler\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., (*7)

Usage

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-,<br><br>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 '<pre>' . print_r($mailer->getRawResponses(), true) . '</pre>';
} else {
    echo 'Error!<br>';
    echo '<pre>' . print_r($mailer->getErrors(), true) . '</pre>';
}

Batch Mailing

If you want to send to multiple recipients, you need to use the below method to batch send., (*8)

$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-,<br><br>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 '<pre>' . print_r($mailer->getRawResponses(), true) . '</pre>';
} else {
    echo 'Error!<br>';
    echo '<pre>' . print_r($mailer->getErrors(), true) . '</pre>';
}

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., (*9)


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

There are a few things left that I didn't get to:, (*10)

  • ASM
  • mail_settings
  • tracking_settings, (*11)

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

The Versions

05/06 2018

dev-master

9999999-dev https://github.com/wadeshuler/yii2-sendgrid-mailer

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!

  Sources   Download

MIT

The Requires

 

yii2 sendgrid yii2 mailer yii2 sendgrid yii2 sendgrid mailer

12/02 2018

v1.0.2

1.0.2.0 https://github.com/wadeshuler/yii2-sendgrid-mailer

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!

  Sources   Download

MIT

The Requires

 

yii2 sendgrid yii2 mailer yii2 sendgrid yii2 sendgrid mailer

20/05 2017

v1.0.1

1.0.1.0 https://github.com/wadeshuler/yii2-sendgrid-mailer

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!

  Sources   Download

MIT

The Requires

 

yii2 sendgrid yii2 mailer yii2 sendgrid yii2 sendgrid mailer

12/05 2017

v1.0.0

1.0.0.0 https://github.com/wadeshuler/yii2-sendgrid-mailer

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!

  Sources   Download

MIT

The Requires

 

yii2 sendgrid yii2 mailer yii2 sendgrid yii2 sendgrid mailer