yii发送邮件
邮件发送配置:
打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php)
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', 'useFileTransport' => false, //这里一定要改成false,不然邮件不会发送 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', 'username' => 'xianan_huang@163.com', 'password' => '*********', //如果是163邮箱,此处要填授权码 'port' => '25', 'encryption' => 'tls', //'port' => '994', //如果tls发不了可以换ssl协议 //'encryption' => 'ssl', ], ],
/** * @param action setFrom(['email'=>'name']) 发件人email=>发件人name * @param action setTo('email') 收件人email * @param action $setSubject email标题 */ $mail = \Yii::$app->mailer->compose() ->setFrom(['xianan_huang@163.com' => 'Yii 中文网']) ->setTo('391430388@qq.com') ->setSubject('邮件发送配置') //->setTextBody('Yii中文网教程真好 www.yii-china.com') //发布纯文字文本 ->setHtmlBody("<br>Yii中文网教程真好!www.yii-china.com") //发布可以带html标签的文本 ->send(); if($mail) echo 'success'; else