vulcandigital / silverstripe-sendgrid
一个模块,帮助开发者通过SendGrid发送模板邮件
1.3.1
2018-05-03 05:57 UTC
Requires
- sendgrid/sendgrid: ~6.0
- silverstripe/framework: ^4.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: 3.*
README
silverstripe-sendgrid
一个模块,帮助开发者通过SendGrid发送模板邮件
要求
- silverstripe/framework: ^4.0
安装
composer require vulcandigital/silverstripe-sendgrid
配置
mysite/_config/sendgrid.yml
Vulcan\SendGrid\SendGrid: api_key: 'REPLACE-WITH-YOUR-API-KEY'
用法
$sendGrid = \Vulcan\SendGrid\SendGrid::create(); $sendGrid->setSubject("We have a sale for you!"); $sendGrid->setFrom('marketing@example.com'); $sendGrid->setFromName('My Site'); $sendGrid->setReplyTo('sales@example.com'); $sendGrid->addRecipient('reece@vulcandigital.co.nz', 'Reece Alexander', [ ':salutation' => 'Mr', ':button_link' => 'https://example.com/store/offer?id=aASdGdjnklashewjk12321hjkasd213' ]); $sendGrid->setBody("<p>We thought you'd like this awesome t-shirt!</p>"); $sendGrid->setTemplateId('your-template-id'); $sendGrid->addAttachment(Image::get()->first()); $sendGrid->send();
您可以添加任意多的收件人。
替换和自定义参数
替换和自定义参数实际上是相同的东西,唯一的区别是自定义参数会全局应用,不受收件人影响,而替换是变量替换,可能因收件人而异。
替换将始终覆盖任何自定义参数
替换
替换是针对每个收件人可以替换的变量
$sendGrid->addRecipient('john@doe.com', 'John Doe', [ ':salutation' => 'Mr', ':first_name' => 'John', ':last_name' => 'Doe' ]); $sendGrid->addRecipient('jane@doe.com', 'Jane Doe', [ ':salutation' => 'Mrs', ':first_name' => 'Jane', ':last_name' => 'Doe' ]);
自定义参数
自定义参数将在所有收件人上全局应用,除非替换已覆盖它
$sendGrid->addCustomArg(':year', DBDatetime::now()->Year());
附件
您可以添加任意多的附件,总大小不超过30 MB。附件必须是File
对象或其子类,例如自身或Image
。
$file = Image::get()->first(); $sendGrid->addAttachment($file, $filename = null, $forcePublish = false);
或者您可以使用文件绝对路径
$sendgrid->addAttachment('/public_html/path/to/image.png')); $sendgrid->addAttachment(Controller::join_links(Director::baseFolder(), '/path/to/image2.png'));
如果您提供了$filename
,请确保也提供了正确的扩展名,以防止出现任何错误
如果提供的文件是File
对象且$forcePublish
设置为true
并且您提供的File
尚未发布,它将被强制发布。
调度
您可以安排在稍后的日期发送电子邮件
$sendGrid->setScheduleTo(DBDatetime::now()->getTimestamp() + 3600); // Schedule to send in 1 hour
重要:请确保您在SendGrid账户设置中指定了正确的时间区域,否则可能会出现意外的结果。
您的数据库时间区域也应与您账户中指定的时区相匹配。有关如何修改数据库使用的时间区域的信息,请参阅核心环境变量。
建议在处理SilverStripe中的日期和时间时,使用它提供的功能,如上面示例所示。
沙盒模式
$sendGrid->setSandboxMode(true);
如果一切正常,$sendGrid->send()将返回true,否则将抛出错误。