首先配置
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'viewPath' => '@common/mail', //指定邮件模版路径
'useFileTransport' => false,//false:非测试状态,发送真实邮件而非存储为文件'transport'=>[ 'class' => 'Swift_SmtpTransport', 'host' =>'smtp.qq.com', //163-》smtp.163.com,qq->smtp.qq.com 'username' => '9***@qq.com', 'password' => '*****', //输入的是客户端的授权密码而不是邮箱密码 'port' => '465', 'encryption' => 'ssl',//ssl ], ],
在控制器中如下:
//发送邮件
public function actionSendEmail(){
// Yii::$app->mailer->compose()
// ->setFrom('****@163.com')
// ->setTo('****1@qq.com')
// ->setSubject('这是测试的')
// ->setTextBody('这是测试的内容')
// ->send();
//里面参数代表指定模版和传递的参数 /mail/layouts/html里面有模版了写主体就行了
Yii::$app->mailer->compose('email',['user'=>'张三'])
->setFrom('****71@qq.com')
->setTo('*****@163.com')
->setSubject('这是测试的')
//->setTextBody('这是测试的内容')
->send();
}
模板:
<?php
use yii\helpers\Html;
use yii\helpers\Url;
/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\BaseMessage instance of newly created mail message */
?>
<p>尊敬的:<b><?php echo $user; ?></b></p>
<h2><?php $url=Yii::$app->urlManager->createAbsoluteUrl(['site/reback','username'=>$user]); ?>
<p><a href="<?php echo $url; ?>"><?php echo $url; ?></a></p></h2>