如何将文件从Base64格式附加到Laravel 5.2邮件 [英] How to attach file to Laravel 5.2 mail from Base64 format

查看:129
本文介绍了如何将文件从Base64格式附加到Laravel 5.2邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将angular应用程序作为前端,将laravel 5.2作为api后端.我的角度应用程序向我的laravel控制器发送参数:

I have angular app as frontend and laravel 5.2 as api backend. My angular app send parameter to my laravel controller this:

{
     name: "My Name", 
     email: "example@email.com", 
     subject: "Hello", 
     message: "This My Message",
     attachment: {
           base64: /9j/4AAQSkZJRgABAQEASABIAAD/2wCEAAMCAgMC.....
           filetype: "application/rar", 
           filename: "example.rar", 
           filesize: 198141,…
     }
}

在我的laravel控制器中,我有这样的代码

And in my laravel controller i have code like this

$data = [
    'name' => $request->input('name'),
    'email' => $request->input('email'),
    'subject' => $request->input('subject'),
    'message' => $request->input('message'),
    'attachment' => $request->file('attachment'),
    'store' => 'Store Name',
];

Mail::send('emails.call-us', ['data' => $data],
    function ($m) use ($data) {
        $m->from($data['email'], $data['name'] . ' - ' . $data['store']);
        $m->attach($data['attachment'], [as => 'example.rar', ['mime' => 'application/rar']);
        $m->to(env('EMAIL_CONTACT_US'));
        $m->subject($data['subject']);
    });

从base64格式发送文件作为附件电子邮件的推荐方法是什么?谢谢

What is the recommended way of send file as attachment email from base64 format? Thanks

推荐答案

好,我解决了我的问题,这很容易...

Ok i solved my problem and it's easy ...

只需将附件更改为attachData

Just changed attach to attachData

$data = [
    'name' => $request->input('name'),
    'email' => $request->input('email'),
    'subject' => $request->input('subject'),
    'message' => $request->input('message'),
    'attachment' => $vAttachment,
    'store' => $vStore->name,
];

Mail::send('emails.call-us', ['data' => $data],
    function ($m) use ($data) {
        $m->from($data['email'], $data['name'] . ' - ' . $data['store']);
        $m->attachData(base64_decode($data['attachment']['base64']), $data['attachment']['filename'], ['mime' => $data['attachment']['filetype']]);
        $m->to(env('EMAIL_CONTACT_US'));
        $m->subject($data['subject']);

对不起,我:)

这篇关于如何将文件从Base64格式附加到Laravel 5.2邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆