1.先在配置文件中 config/web.php 中修改配置文件
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => false, //'class' => 'yii\swiftmailer\Mailer', 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', 'username' => 'm18612193548@163.com', 'password' => 'n1234567890', 'port' => '25', 'encryption' => null, ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['m18612193548@163.com'=>'admin'] ], ],
2 。在控制器里加入以下代码进行测试
Yii::$app->mailer->compose() ->setFrom('m18612193548@163.com') ->setTo('1297192749@qq.com') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('<b>傻蛋</b>') ->send();