Yii框架怎么发送邮件?
首先使用Composer安装“yiisoft/yii2-swiftmailer”扩展;
php composer require --prefer-dist yiisoft/yii2-swiftmailer
然后配置mailer中参数;
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'yii\\swiftmailer\\Mailer', 'viewPath' => '@common/mail', // 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. 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', 'username' => '***@163.com', 'password' => '******', 'port' => '25', 'encryption' => 'tls', ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['***@163.com'=>'白狼栈'] ], ], ], ];
接着调用该代码“Yii::$app->mailer”获取到mailer对象;
$mail= Yii::$app->mailer->compose();
最后设置邮件信息即可。
$mail= Yii::$app->mailer->compose(); $mail->setTo('***@qq.com'); //要发送给那个人的邮箱 $mail->setSubject("邮件主题"); //邮件主题 $mail->setTextBody('测试text'); //发布纯文字文本 $mail->setHtmlBody("测试html text"); //发送的消息内容 var_dump($mail->send());
推荐教程:《PHP》