去发现生活中的美好,记录生活中的点点滴滴

laravel内置邮件发送问题汇总

php admin 2162℃

1、laravel新增发件人配置:
在需要发送邮件的地方,加上以下配置

        //修改邮箱配置
        \Config::set('mail.from', ['address' => $data['email'], 'name' => $data['user_name']]);
        \Config::set('mail.username', $data['email']);
        \Config::set('mail.password', $data['password']);
      //发送邮件
        Mail::send('mailtemp.taskmail', $data, function($m) use($data){
            //from-发件人;to-收件人,cc-抄送人;attach-附件
            $m->to('ganyongmeng@qq.com')
                ->subject($data['title']);
            $m->attach('http://附件链接’);
        });

注:在设置Config的时候,需要注意,在同一个进程里,设置了config,是会影响全局config配置,但是新建一个队列进程,更改配置,不会影响原有config配置,因为已经属于不同的进程,故如果需要更改config配置,必须新起一个进程,再在这个新的进程里设置config,这样就能保证config互不影响,如下例子可以说明。

        //默认配置mail.username = a
        echo "step1:".\Config::get('mail.username');
        \Config::set('mail.username', "b");
        echo "step2:".\Config::get('mail.username');
        $job = (new \App\Jobs\Test1())->onQueue('abc');//新进程,在这个进程里面,会设置新的mail值,且在这个进程里面生效
        dispatch($job);
        echo "step3:".\Config::get('mail.username');
     //输出:step1:a,step2:b,step3:b;

启动队列进程:

php artisan queue:work --queue=abc --tries=1  

\App\Jobs\Test1 job:

    public function handle() {
        \Config::set('mail.username', "c");
        $res = \Config::get('mail.username');
        info('mail_username:'.$res);
        info('exec job...');
    }
//输出:c

2、对于发送到QQ邮件乱码问题:
使用laravel内置邮箱发送到QQ邮箱的时候,如果附件文件名小于6个中文字,会导致乱码,解决方案:
(1)、

                   preg_match_all("/([\x{4e00}-\x{9fa5}]){1}/u",$file_arr['basename'],$arrCh);
                    if(count($arrCh[0]) < 6 ){
                        $m->attach($data['file1'],['as' => "=?UTF-8?B?". base64_encode($file_arr['basename']) . "?="]); //$file_arr['basename']为文件名及后缀
                    }else{
                        $m->attach($data['file1']);
                    }

(2)、

            $file = \Swift_Attachment::fromPath('http://test.cn/20181211111206/中文名.xlsx');
            $file->setDisposition("attachment; filename* = " . "中文名.xlsx");
            $m->getSwiftMessage()->attach($file);

转载请注明:永盟博客 » laravel内置邮件发送问题汇总

喜欢 (1)

Warning: count(): Parameter must be an array or an object that implements Countable in E:\www\blog\wp-includes\class-wp-comment-query.php on line 405