wadeshuler / yii2-sendgrid
该软件包已被放弃,不再维护。没有建议的替代软件包。
为SendGrid提供批处理邮件支持的Yii2邮件扩展。此扩展旨在替代所有其他扩展!您需要的唯一Yii2 SendGrid扩展!
v1.0.4
2022-04-02 23:26 UTC
Requires
- php: >=5.4.0
- sendgrid/sendgrid: ~5.4
- yiisoft/yii2: ~2.0.11
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
- mail_settings
- tracking_settings
我计划稍后解决这些问题。如果您能帮忙,请随时提供帮助 :)
捐赠
如果您觉得我的代码有用,请考虑捐赠。