yii2下使用自带类发送邮件
1.在配置文件main-local.php components=>[]里面配置
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'username' => '15618380091@163.com',
'password' => '*******',
'port' => '25',
'encryption' => 'tls',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['15618380091@163.com'=>'admin']
],
],
- controller控制器中 代码:
<?php
$mail= Yii::$app->mailer->compose();
$mail->setTo('***********@qq.com');
$mail->setSubject("邮件测试");
//$mail->setTextBody('zheshisha '); //发布纯文字文本
$mail->setHtmlBody("<br>问我我我我我"); //发布可以带html标签的文本
if($mail->send())
echo "success";
else
echo "failse";
die();
?>
ok,这样就可以发送邮件了
如需加载模板 把$mail= Yii::$app->mailer->compose();
修改成 $mail= Yii::$app->mailer->compose('xiaoma',['aa'=>222]);
注:aa是想xiaoma.php里面传递的参数。
邮件模板 xiaoma.php里面的代码 :
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $user common\models\User */
$resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $aa]);
?>
< a href="#" ><?php echo $resetLink ?></a>
加载模板的邮件代码:
$mail= Yii::$app->mailer->compose('xiaoma',['aa'=>222]);
$mail->setTo('1401619705@qq.com');
$mail->setSubject("邮件测试");
$mail->setTextBody('zheshisha ');
if($mail->send())
echo "success";
else
echo "failse";
die();