首页 > 代码库 > yii2 邮件发送

yii2 邮件发送

修改配置文件mail-local.php

 

‘mailer‘ => [
‘class‘ => ‘yii\swiftmailer\Mailer‘,
‘useFileTransport‘ =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
‘transport‘ => [
‘class‘ => ‘Swift_SmtpTransport‘,
‘host‘ => ‘smtp.163.com‘, //每种邮箱的host配置不一样
‘username‘ => ‘y982044126‘,
‘password‘ => ‘shx1996‘,
‘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();

yii2 邮件发送