How to attach file to Laravel 5.2 mail from Base64 format

Deadly 提交于 2019-12-08 04:26:15

问题


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:


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 :)



来源:https://stackoverflow.com/questions/43948309/how-to-attach-file-to-laravel-5-2-mail-from-base64-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!