YII2 MAIL官方文档:https://www.yiiframework.com/doc/guide/2.0/zh-cn/tutorial-mailing
首先添加邮箱配置
'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.exmail.qq.com',
'username' => '*****@qq.com',
'password' => '*****',
'port' => 465,
'encryption' => 'ssl',
],
'messageConfig' => [
'charset' => 'UTF-8',
'from' => ['*****@qq.com' => '管理员']
],
],
发送邮件
$mail = \Yii::$app->mailer->compose();
$mail->setTo('test@qq.com')
->setSubject('邮件标题')
->attach('/data/test.xlsx', ['contentType' => 'application/vnd.ms-excel'])
->send();