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' => '[email protected]', 'password' => 'n1234567890', 'port' => '25', 'encryption' => null, ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['[email protected]'=>'admin'] ], ],
2 。在控制器里加入以下代碼進行測試
Yii::$app->mailer->compose() ->setFrom('[email protected]') ->setTo('[email protected]') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('<b>傻蛋</b>') ->send();