skn/yii2-sendgrid

支持批量邮件发送的SendGrid邮件扩展,用于Yii2。本扩展旨在取代所有其他扩展!您将需要的唯一一个Yii2 SendGrid扩展!

安装: 1

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 14

类型:yii2-extension

v1.0.2 2018-02-12 22:00 UTC

This package is not auto-updated.

Last update: 2024-09-19 18:22:09 UTC


README

支持批量邮件发送的SendGrid邮件扩展,用于Yii2。本扩展旨在取代所有其他扩展!您将需要的唯一一个Yii2 SendGrid扩展!

安装

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

运行以下命令之一:

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

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

将以下内容添加到应用程序的composer.json文件的require部分。

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

'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个收件人。这是包括收件人、密件抄送和抄送地址的总数。我建议使用500作为批量大小。这应该足够高效地处理数千封邮件,而不会因意外超过1,000个收件人的规则而引发错误。如果您没有使用任何密件抄送或抄送地址,您可以将批量数量提高一点。理论上,您应该能够做到1,000个,但我可能最多设置在950个,以留出一些余地。

已知问题

  • addSection() - 当前存在SendGrid API的问题,导致部分功能无法正常工作。
  • setSendAt() - 当前存在SendGrid API的问题,使用send_at时显示的是排队时间,而不是实际发送邮件的时间。
  • setReplyTo() - 当前存在SendGrid PHP API的问题,回复地址只接受字符串形式的电子邮件地址,因此不能设置名称。

待办事项

还有一些事情我没有完成

  • ASM
  • 邮件设置
  • 跟踪设置

我计划稍后处理这些问题。如果您能帮忙,请随时提供帮助:)