yii 收发邮件
----------------------------------------------------------------------------------------------
首先
在qq邮箱里面的账户把smtp端口开启,生成的有验证码,这个验证就是password里面的值
将下面的代码复制到 backend/frontend 里的 config/main-local.php 里面
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
//'viewPath' => '@common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
//'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'host' => 'smtp.qq.com', //每种邮箱的host配置不一样
'username' => '178***530@qq.com',
'password' => '***********',
'port' => '465',
'encryption' => 'ssl',
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
//'viewPath' => '@common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
//'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'host' => 'smtp.qq.com', //每种邮箱的host配置不一样
'username' => '178***530@qq.com',
'password' => '***********',
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['17******530@qq.com'=>'admin']
],
------------------------------------------------------------------------------------------
在控制器中 加载下边的类
use yii;
use yii\base\Security;
use yii\base\Security;
定义一个控制器中的方法
//邮箱
public function actionEmail()
{
$mail= Yii::$app->mailer->compose();
$mail->setTo('11******71@qq.com'); //发送给***
$mail->setSubject("邮件测试");
public function actionEmail()
{
$mail= Yii::$app->mailer->compose();
$mail->setTo('11******71@qq.com'); //发送给***
$mail->setSubject("邮件测试");
$lock = new Security();
//[小月月] 是要发送的内容 [123] 是以123的方式加密
$str = $lock->encryptByKey('小月月','123');
//echo $str;die;
$str = base64_encode($str);
$str = $lock->encryptByKey('小月月','123');
//echo $str;die;
$str = base64_encode($str);
$mail->setTextBody($str.'秘钥123'); //发布纯文字文本
//$mail->setHtmlBody("<br>问我我我我我"); //发布可以带html标签的文本
if($mail->send())
echo "success";
else
echo "failse";
die();
}
/*
base64 加密
*/
public function actionKey()
{
$lock = new Security();
$str = $lock->encryptByKey('小月月','123');
//echo $str;die;
$str = base64_encode($str);
//echo $str;die;
$str = base64_decode($str);
//echo $str;die;
$str = $lock->decryptByKey($str,'123');
echo $str;
}
public function actionKey()
{
$lock = new Security();
$str = $lock->encryptByKey('小月月','123');
//echo $str;die;
$str = base64_encode($str);
//echo $str;die;
$str = base64_decode($str);
//echo $str;die;
$str = $lock->decryptByKey($str,'123');
echo $str;
}