【问题标题】:Steps to send mail using sendgrid mailer function in yii2?yii2中使用sendgrid mailer功能发送邮件的步骤?
【发布时间】:2016-09-07 14:53:52
【问题描述】:

我正在尝试在 Yii 2 中使用 sendgrid 获取邮件,但它似乎不起作用。谁能告诉我在 yii2 中 sendgrid 的步骤。

【问题讨论】:

  • 您的问题质量很低。请添加更多信息。您正在使用哪个扩展程序,如何设置它,如何调用它,您明确得到哪个错误?

标签: yii2 sendgrid


【解决方案1】:

您可以使用https://github.com/bryglen/yii2-sendgrid#yii-2-bryglen-sendgrid

安装:

composer require --prefer-dist bryglen/yii2-sendgrid "*"

common/config/main.php

'components' => [
    ...
    'sendGrid' => [
        'class' => 'bryglen\sendgrid\Mailer',
        'username' => 'your_user_name',
        'password' => 'your password here',
        //'viewPath' => '@app/views/mail', // your view path here
    ],
    ...
],

要发送电子邮件,您可以使用以下代码:

$sendGrid = Yii::$app->sendGrid;
$message = $sendGrid->compose('contact/html', ['contactForm' => $form]);
$message->setFrom('from@domain.com')
    ->setTo($form->email)
    ->setSubject($form->subject)
    ->send($sendGrid);

【讨论】:

  • 最好用'class' => 'bryglen\sendgrid\Mailer' 配置mailer 组件,因为在Yii 中使用了“约定优于配置”范式
猜你喜欢
  • 2019-08-08
  • 2021-06-18
  • 2018-10-16
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多