yii2发送邮件

config.php配置

mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'useFileTransport' => false,//false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.exmail.qq.com',  //每种邮箱的host配置不一样
        'username' => '[email protected]',
        'password' => 'swrtesz5563^',
        'port' => '25',
        'encryption' => 'tls',
    ],
    'messageConfig'=>[
        'charset'=>'UTF-8',
        'from'=>['[email protected]'=>'admin']
    ],
],
使用

$mail = Yii::$app->mailer->compose();
$mail->setTo('[email protected]');
$mail->setSubject("邮件测试");
//$mail->setTextBody('zheshisha ');   //发布纯文字文本
$mail->setHtmlBody("
问我我我我我"); //发布可以带html标签的文本 //$mail->attach($content) //带附件 if($mail->send()) echo "success"; else echo "failse";

你可能感兴趣的:(yii2发送邮件)