sbrendtro / yii2-sendgrid
支持批量发送的 SendGrid 邮件扩展。此扩展基于 wadeshuler/yii2-sendgrid,并做了少量调整
v1.0.6
2022-08-08 20:46 UTC
Requires
- php: >=5.4.0
- sendgrid/sendgrid: ~5.4
- yiisoft/yii2: 2.0.*
README
支持批量发送的 SendGrid 邮件扩展。此扩展旨在取代所有其他扩展!您只需要此 Yii2 SendGrid 扩展!
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist sbrendtro/yii2-sendgrid
或者添加以下内容到您应用程序的 composer.json
文件的 require 部分中:
"sbrendtro/yii2-sendgrid": "~1.0"
然后按照如下方式配置您的 mailer
组件(高级模式下为 main-local.php
,基本模式下为 web.php
):
不要忘记将 apiKey
替换为您的 SendGrid API 密钥。它必须具有发送邮件的权限。
'mailer' => [
'class' => 'sbrendtro\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]',
],
使用
单次发送
批量发送
$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') // if ommited, replyTo defaults to From address
->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>';
}
如果您要向多个收件人发送邮件,则需要使用以下方法进行批量发送。
注意:SendGrid 支持最多 1,000 个收件人。这是发件人、BCC 和 CC 地址的总数。我建议使用 500
作为批量大小。这应该足够处理数千封电子邮件,而不会因意外违反 1,000 个收件人规则而引发错误。如果您没有使用任何 BCC 或 CC 地址,您可以将批量数量略微提高。理论上,您应该可以达到 1,000,但我可能会将其限制在 950,以留出一些余地。
$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>';
}
已知问题
addSection()
- SendGrid API 中存在一个问题,导致段落无法正常工作。
setSendAt()
- SendGrid API 中存在一个问题,使用send_at
时显示的时间是队列时间,而不是实际发送邮件的时间。setReplyTo()
- SendGrid PHP API 中存在一个问题,ReplyTo 地址只接受作为字符串的电子邮件地址。因此,您不能设置姓名。- 待办事项
还有一些事情我没有完成
我计划在以后处理它们。如果您能帮忙,请随时提出:)
- ASM
- mail_settings
- tracking_settings
我计划稍后处理它们。如果您能帮忙,请随时提出:)