我已经以这种方式配置了邮件程序
在组件中
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'gmailaccount',
'password' => 'gmailpassword',
'port' => '587',
'encryption' => 'tls',
],
],
],
],
在我的控制器中我有
public function actionTestmail(){
return \Yii::$app->mailer->compose('testmail')
->setFrom([Yii::$app->params['supportEmail']]) //this is set in params
->setTo("mysecondmail@gmail.com")
->setSubject('Testing yii2 mailer ')
->send();
}
以上总是返回设置未知属性的错误:yii \ swiftmailer \ Mailer :: mailer,可能出错,
以上配置是来自yii2网站的复制粘贴,但它无法正常工作
答案 0 :(得分:1)
您在组件配置中重复了两次mailer
。
这是正确的配置:
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'gmailaccount',
'password' => 'gmailpassword',
'port' => '587',
'encryption' => 'tls',
],
],
],