How to attach file to Laravel 5.2 mail from Base64

2020-03-30 07:17发布

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,…
     }
}

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']);
    });

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

1条回答
Bombasti
2楼-- · 2020-03-30 07:51

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

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']);

Silly me :)

查看更多
登录 后发表回答