Yii2.0 发送邮件

配置

return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
        ],
    ],
];

发送

$mailer = \Yii::$app->mailer->compose();

$mailer->setFrom($from);
$mailer->setTo($to);
$mailer->setSubject($subject);
$mailer->setTextBody($body);//text
$mailer->setHtmlBody($body);//html
$mailer->send();

e.g

配置

'mailer' => [  

  'class' => 'yii\swiftmailer\Mailer',  
  'useFileTransport' =>false,//必填!! false表示发送邮件, true表示存在runtime文件包下

  'transport' => [  
    'class' => 'Swift_SmtpTransport',  
    'host' => 'smtp.163.com',  //SMTP config
    'username' => 'Boy.Lee@163.com', //SMTP config
    'password' => '*******',//SMTP config
    'encryption' => 'tls',  //SMTP config
    'port' => '25',  //SMTP config
                   
  ], 

  'messageConfig'=>[  
    'charset'=>'UTF-8',  
    'from'=>['Boy.Lee@163.com'=>'Boy.Lee']  
  ],  
],

发送

'mailer' => [  

  'class' => 'yii\swiftmailer\Mailer',  
  'useFileTransport' =>false,//必填!! false表示发送邮件, true表示存在runtime文件包下

  'transport' => [  
    'class' => 'Swift_SmtpTransport',  
    'host' => 'smtp.163.com',  //SMTP config
    'username' => 'xucanjia@163.com', //SMTP config
    'password' => '*******',//SMTP config
    'encryption' => 'tls',  //SMTP config
    'port' => '25',  //SMTP config
                   
  ], 

  'messageConfig'=>[  
    'charset'=>'UTF-8',  
    'from'=>['Boy.Lee@163.com'=>'Boy.Lee']  
  ],  
],

Yii 2.0 使用MailGun API 发送邮件(转)




导航