xeois / yii2-sendgrid
为 Yii 2 的 Sendgrid 邮件发送器
4.0.6
2020-04-14 09:47 UTC
Requires
- sendgrid/sendgrid: ~7
- yiisoft/yii2: 2.0.34
Requires (Dev)
- codeception/codeception: ^2.4
README
为 Yii 2 的 Sendgrid 邮件发送器
从以下地址分叉:bryglen/yii2-sendgrid
3.1.0 更新
之前返回 \SendGrid\Mail\EmailAddress
或包含此类型的数组的方法,现在返回纯文本电子邮件字符串或 ['email' => 'name']
数组或包含此值的数组。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist xeois/yii2-sendgrid "~4"
或者在您的 composer.json
文件的 require 部分添加:
"xeois/yii2-sendgrid": "~4"
使用方法
扩展安装后,只需在代码中通过以下方式使用:
要使用邮件发送器,您应该在应用程序配置中配置它,如下所示:
'components' => [ ... 'sendGrid' => [ 'class' => 'xeois\sendgrid\Mailer', 'apiKey' => 'your api key', ] ... ],
要发送电子邮件,您可以使用以下代码:
$sendGrid = Yii::$app->sendGrid; $message = $sendGrid->compose('contact/html', ['contactForm' => $form]) $message->setFrom('from@domain.com') ->setTo($form->email) ->setSubject($form->subject) ->send($sendGrid); //also you can use sendgrid substitutions ->setSendGridSubstitution('template id', [ ':var1' => 'var1value', ':var2' => 'var2value', ]);