在配置文件components中添加mailer配置
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'timeZone'=>'Asia/Chongqing',
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=root',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'encryption' => 'tls',
//qq邮箱的是'smtp.qq.com,别的上网可以自己查询
'host' => 'smtp.qq.com',
//这里我记得是587和另外一个值,587不行的话可以自己百度
'port' => '587',
//你自己的QQ号
'username' => '1414935835',
//这里的参数需要申请,点击qq邮箱界面的设置-》账户-》找到(POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务)-》开启(POP3/SMTP服务)-》会有一个密钥填入就行
'password' => 'aaaa',
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
'cachePath' => '@runtime/cache2',
],
'assetManager' => [
'forceCopy' => true,
'appendTimestamp' => true,
],
],
];
在发送邮件的类中调用
$mail= Yii::$app->mailer->compose();
$mail->setFrom('发送者的邮箱');
$mail->setTo('接收者的邮箱');
$mail->setSubject('意向客户名单');
$mail->setTextBody('Plain text content');
$messages="
<p>公司:".$data['company']."</p>
<p>姓名:".$data['name']."</p>
<p>邮箱:".$data['mails']."</p>
<p>电话:".$data['phone']."</p>
<p>省:".$data['province']."</p>
<p>市:".$data['city']."</p>
<p>留言内容:".$data['content']."</p>
";
$mail->setHtmlBody($messages);
$mail->send();