如何在 yii 2.0.3 高级应用程序模板中设置注册激活链接的自动电子邮件以及如何链接回登录页面 在这个新版本中?
在数组commonconfigmain.php components
配置 Swiftmailer:
用于使用 swift mailer 发送邮件
'mailer' => [
'class' => 'yiiswiftmailerMailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'your_host_name',
'username' => 'enter_your_username_here',
'password' => 'enter_your_password',
'port' => '25',
],
],
在此之后,您可以使用以下代码发送邮件:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from@domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
contact/html
是模板名称,其中有公共/邮件文件夹contactForm
具有值$form
变量传递给该模板。
您可以将上述代码放在控制器中。但是我建议对它进行通用功能,并在任何地方使用它。希望对您有所帮助。