wowmaking / yii2-sendgrid
Yii2 Sendgrid 邮件发送扩展
dev-main
2024-04-02 17:05 UTC
Requires
- php: ^7.2 || ^8
- ext-json: *
- sendgrid/sendgrid: ^7.0
- yiisoft/yii2: ^2
Requires (Dev)
- phpunit/phpunit: ^9
- roave/security-advisories: dev-latest
- symplify/easy-coding-standard: ^11
- yidas/yii2-composer-bower-skip: ^2.0
This package is not auto-updated.
Last update: 2024-10-02 17:49:18 UTC
README
描述
支持批量发送的 Yii2 SendGrid 邮件发送扩展。
推荐替代方案
此组件的可能替代方案如下组合
安装
$ composer require marketforce-info/yii2-sendgrid
然后在您的 main-local.php
(高级)或 web.php
(基本)中配置 mailer
组件,如下所示
'mailer' => [ 'class' => \MarketforceInfo\SendGrid\Mailer::class, '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 密钥。它必须具有发送邮件的权限。
使用方法
基本
$mailer = Yii::$app->mailer; $message = $mailer->compose() ->setTo('user@example.com') ->setFrom(['alerts@example.com' => 'Alerts']) ->setReplyTo('noreply@example.com') ->setSubject('Example Email Subject') ->setTextBody('Example email body.') ->send();
单次发送
$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]) ->setFrom(['alerts@example.com' => 'Alerts']) ->setReplyTo('noreply@example.com') ->setSubject('Example Email Subject') ->setHtmlBody('Dear -username-,<br><br>Example email HTML body.') ->setTextBody('Dear -username-,\n\nExample email text body.') ->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; 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-, Example Email Subject') ->setHtmlBody('Dear -username-,<br><br>Example email HTML body.') ->setTextBody('Dear -username-,\n\nExample email text body.') 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 中存在一个问题,导致部分功能无法正常工作。setSendAt()
- 目前在使用send_at
时,SendGrid API 的问题导致显示的是队列时间而不是实际发送邮件的时间。setReplyTo()
- 目前 SendGrid PHP API 的问题导致回复地址仅接受字符串形式的电子邮件地址。因此,您不能设置名称。
待办事项
还有一些我没有完成的事情
- ASM
- mail_settings
- tracking_settings
欢迎以问题或 PR 的形式贡献。
归属
此扩展最初由 https://www.github.com/wadeshuler 创建。