marketforce-info / yii2-sendgrid
Yii2 Sendgrid 邮件发送扩展
1.0.3
2023-11-07 12:11 UTC
Requires
- php: ^7.4 || ^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
README
描述
一个支持批量发送的 SendGrid 邮件发送扩展,适用于 Yii2。
推荐替代方案
该组件的一个可能的替代方案是以下组合
安装
$ 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 个收件人。这是收件人、密送和抄送地址的总数。我建议将批量大小设置为 500
。这应该足够处理数千封邮件,而不会因为意外违反 1,000 个收件人的规则而导致错误。如果你没有使用任何密送或抄送地址,你可以将批量数量略微提高。理论上,你应该能够达到 1,000,但我可能会将其最大设置为 950,以留出一些余地。
已知问题
addSection()
- 目前 SendGrid API 中存在一个问题,导致部分功能无法正常工作。setSendAt()
- 目前 SendGrid API 中存在一个问题,使用send_at
时显示的是队列时间,而不是实际发送邮件的时间。setReplyTo()
- 目前 SendGrid PHP API 中存在一个问题,回复地址仅接受字符串形式的电子邮件地址。因此,你不能设置名称。
待办事项
还有一些事情我没有完成
- ASM
- mail_settings
- tracking_settings
欢迎以问题或 PR 的形式做出贡献。
贡献者
此扩展最初由 https://www.github.com/wadeshuler 创建