1.打开main-local.php文件添加:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.sina.com', //每种邮箱的host配置不一样
'username' => 'gg24155@sina.com', //发件人邮箱
'password' => 'gghw4089225', //授权码
'port' => '25',
'encryption' => 'tls',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['gg24155@sina.com'=>'nickname'] //发件人昵称
],
],
2.控制器demo:
//对称加密+发送邮箱
public function actionEncrypt(){
header("Content-type:text/html;charset=utf-8");
$msg = "张三";
//$pwd = "123";
$re = Yii::$app->security->encryptByKey($msg,'');
$arr = urlencode($re);
$mail = Yii::$app->mailer->compose();
$mail->setTo('xxxxxxx@qq.com'); //接收人邮箱
$mail->setSubject("test"); //邮件标题
$mail->setHtmlBody("http://www.hanwei.com/day_exercise/frontend/web/index.php?r=test/get_email&name=".$arr); //发送内容(可写HTML代码)
if ($mail->send()){
echo "成功";
}else{
echo "失败";
}
}
//接受邮箱信息
public function actionGet_email(){
header("Content-type:text/html;charset=utf-8");
$re3 = urldecode($_GET['name']);
$re1 = Yii::$app->security->decryptByKey($re3,'');
echo $re1;