控制器里面 直接加载yii\debug\model\search\Mailer;
$mail= Yii::$app->mailer->compose();
$mail->setTo('[email protected]');
$mail->setSubject("邮件测试");
$mail->setTextBody('呜呜呜呜呜呜'); //发布纯文字文本
// $mail->setHtmlBody("<br>问我我我我我"); //发布可以带html标签的文本
if($mail->send())
echo "<script>alert('已发送成功')</script>";
else
echo "<script>alert('发送失败')</script>";
die();
在common文件下打开config里面的main_local,然后把这些代码放到components里面
例如:'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=ecshop1',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' =>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.qq.com',
'username' => '[email protected]',
'password' => '********', //授权码
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['[email protected]'=>'admin']
],
],