yii 邮件发送

来源:互联网 发布:新网域名转入万网 编辑:程序博客网 时间:2024/05/27 09:46

1.在配置文件中 config下main.php 找到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' => 'tiedanzi@163.com',
                 'password' => '163密码',
                 'port' => '25',
                 'encryption' => 'tls',
             ],
             'messageConfig'=>[
                 'charset'=>'UTF-8',
                 'from'=>['tiedanzi@163.com'=>'tiedanzi']
             ],
 ],
2.在控制器中写入方法  添加方法内容

        $mail = \YII::$app->mailer->compose();//加载模板这样写:
        $mail->setSubject("邮件");        
        $mail->setTextBody(****);//发布纯文字文本  
        //$mail->setHtmlBody("htmlbody");//发布可以带html标签的文本  
        $mail->send     //发送

3.在显示页面接收判断一下就可以了



//以上内容仅供参考


0 0