evermos/yii2-sendgrid
Composer 安装命令:
composer require evermos/yii2-sendgrid
包简介
Yii2 Mailer extension for SendGrid with batch mailing support. This extension is designed to replace them all! The only Yii2 SendGrid extension you will need!
README 文档
README
Yii2 Mailer extension for SendGrid with batch mailing support. This extension is designed to replace them all! The only Yii2 SendGrid extension you will need!
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist wadeshuler/yii2-sendgrid
or add
"wadeshuler/yii2-sendgrid": "~1.0"
to the require section of your application's composer.json file.
Then configure your mailer component in your main-local.php (advanced) or web.php (basic) like so:
'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]',
],
Do not forget to replace apiKey with your SendGrid API key. It must have permissions to send emails.
Usage
Single Mailing
$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>';
}
Batch Mailing
If you want to send to multiple recipients, you need to use the below method to batch send.
$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>';
}
NOTE: SendGrid supports a max of 1,000 recipients. This is a total of the to, bcc, and cc addresses. I recommend using 500 for the batch size. This should be large enough to process thousands of emails efficiently without risking getting errors by accidentally breaking the 1,000 recipients rule. If you are not using any bcc or cc addresses, you could raise the batch number a little higher. Theoretically, you should be able to do 1,000 but I would probably max at 950 to leave some wiggle room.
Known Issues
addSection()- There is currently an issue with the SendGrid API where sections are not working.setSendAt()- There is currently an issue with the SendGrid API where usingsend_atwhere the time shows the queued time not the actual time that the email was sent.setReplyTo()- There is currently an issue with the SendGrid PHP API where the ReplyTo address only accepts the email address as a string. So you can't set a name.
TODO
There are a few things left that I didn't get to:
- ASM
- mail_settings
- tracking_settings
I plan to get to them at a later date. Feel free to help out if you can :)
evermos/yii2-sendgrid 适用场景与选型建议
evermos/yii2-sendgrid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.67k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sendgrid」 「yii2」 「yii2 mailer」 「yii2 sendgrid」 「yii2 sendgrid mailer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 evermos/yii2-sendgrid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 evermos/yii2-sendgrid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 evermos/yii2-sendgrid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SendGrid plugin for CakePHP 4 - Send emails using SendGrid API
A custom URL rule class for Yii 2 which allows to create translated URL rules
A collection of monolog handlers using PSR-18 http client
Creates new sare mail transport for laravel applications
Componente para integração com serviços de mensageria com APIs externas como Twilio, SendGrid, AWS SES, etc.
A drop-in mailer replacement for SendGrid
统计信息
- 总下载量: 2.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-11