Yii2 常用功能模块【文件下载 邮件发送 UrlManager】

文件下载

Yii::$app->response->sendFile("F://form.doc")->send();  

邮件服务器配置:

     //以aliyun企业邮为例,在config/web.phpd的'components'里面配置
     'urlManager' => [
          ....
      ],
      'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // 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.mxhichina.com', 
                'username' => 'support@XXX.com', 
                'password' => 'XXX', 
                'port' => '25', 
                'encryption' => 'tls', 
            ], 
            'messageConfig'=>[ 
                'charset'=>'UTF-8', 
                'from'=>['support@XXX.com'=>'发送人'] 
            ], 
        ],

邮件发送代码

date_default_timezone_set('PRC');
        $mail= Yii::$app->mailer->compose();   
        $mail->setTo("XXX@qq.com");  
        $mail->setSubject("邮件测试");  
        //$mail->attach('F:\\form.doc'); //发送附件方法1
        //$mail->attachContent('Attachment content', ['fileName' => 'attach.txt', 'contentType' => 'text/plain']); // 发送附件方法2
        //$mail->setTextBody('zheshisha ');   //发布纯文字文本
        $mail->setHtmlBody("<br>问我我我我我");    //发布可以带html标签的文本
        if($mail->send())return true;
        return false;   

UrlManager 路由美化

/* 以下配置可以分别通过 
     /index
     /模块名/controllerName
     来访问,如localhost/web/index或localhost/index(取决于你的yii2 web目录的位置)
     还有,可以配置extraPatterns 来调用你的自定义action
     如
     ['class' => 'yii\rest\UrlRule', 'pluralize' => false,
                                     'controller' => ['v1/user'],
                                     'extraPatterns' => [ 
                                            'GET downloadform' => 'downloadform',
                                     ],
     ],
*/     

      'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'pluralize' => false,
                                                'controller' =>[
                                                    'index' => 'site',
                                                    '模块名/controllerName',

                                                ],

                ],
            ]
         ]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值