修改配置文件mail-local.php
'mailer' => [
'class' => 'yiiswiftmailerMailer',
'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'username' => 'xxxxxxx',
'password' => 'xxxxx',
'port' => '25',
'encryption' => 'tls',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['y982044126@163.com'=>'admin']
],
],
控制器页面
$email=yii::$app->request->get('email'); //接收要发送的邮箱
$mail= Yii::$app->mailer->compose();
$mail->setTo("$email");
$mail->setSubject("邮件测试");
//$mail->setTextBody('zheshisha '); //发布纯文字文本
$mail->setHtmlBody("恭喜你,邮箱已通过验证!); //发布可以带html标签的文本
if($mail->send())
echo "success";
else
echo "failse";
die();