2ship-connect/yii2-sendgrid

支持批量发送的 SendGrid 邮件扩展,为 Yii2。这个扩展旨在替代所有其他同类扩展!这是您唯一需要的 Yii2 SendGrid 扩展!

安装: 17

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 0

分支: 14

类型:yii2-extension

v1.0.4 2022-04-02 23:26 UTC

This package is auto-updated.

Last update: 2024-09-18 13:30:56 UTC


README

支持批量发送的 SendGrid 邮件扩展,为 Yii2。这个扩展旨在替代所有其他同类扩展!这是您唯一需要的 Yii2 SendGrid 扩展!

安装

安装此扩展的首选方法是通过 composer

运行以下命令

composer require 2ship-connect/yii2-sendgrid

到您的应用程序的 composer.json 文件的 require 部分。

然后在您的 main-local.php(高级)或 web.php(基本)中配置您的 mailer 组件,如下所示

'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]',
],

不要忘记将 apiKey 替换为您的 SendGrid API 密钥。它必须具有发送电子邮件的权限。

用法

单次邮件发送

$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>';
}

批量邮件发送

如果您想向多个收件人发送邮件,您需要使用以下方法进行批量发送。

$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>';
}

注意:SendGrid 支持的最大收件人数量为 1,000。这是 to、bcc 和 cc 地址的总和。我建议使用 500 作为批量大小。这应该足够处理数千封电子邮件,而不会因意外违反 1,000 个收件人规则而引发错误。如果您没有使用任何 bcc 或 cc 地址,您 可以 将批量数量稍微提高一些。理论上,您应该能够做到 1,000,但我可能会将上限设置为 950,以留出一些余量。

已知问题

  • addSection() - 目前 SendGrid API 中存在一个问题,导致 sections 无法正常工作。
  • setSendAt() - 目前在 SendGrid API 中,使用 send_at 时显示的时间是队列时间,而不是实际发送电子邮件的时间。
  • setReplyTo() - 目前在 SendGrid PHP API 中,ReplyTo 地址仅接受字符串形式的电子邮件地址。因此,您不能设置名称。

待办事项

还有一些事情我没有完成

  • ASM
  • mail_settings
  • tracking_settings

我计划稍后处理它们。如果您能帮忙的话,请随时帮忙 :)

捐赠

如果您觉得我的代码有用,请考虑捐赠。

PayPal Donate