christian_H's avatar

How to validate .eml files using the mimes validation rule?

hello there, I've got a file upload form that uses the mimes validation rule to validate the type of file being uploaded. I'd would like to update this to allow email files with the .eml extension, but I can't get them to pass validation. The MimeTypeExtensionGuesser.php file has "eml" in the array which makes me think it should work. Here's my validation code.

$validator = Validator::make($request->all(), [
            'fileName' => 'required',
            'file' => 'required|mimes:pdf, doc, docx, rtf, txt, eml'
        ]);

Can anyone who's made validation for this type of file before tell me what I'm doing wrong?

Alternatively, I've considered the possibility that laravel is guessing the wrong mime type based on the file. Is it possible to see what mime type laravel has determined the file to be through the Validator facade?

I'm using laravel 5.2, any help is very appreciated!

0 likes
5 replies
Cronix's avatar

in MimeTypeExtensionGuesser, it's defining the mime-type for .eml as message/rfc822

So I guess the next step is to see whether your .eml file mimetype is actually message/rfc822

1 like
Cronix's avatar

if you dd($request->fileField) before validation, the mimetype should be listed as a property of the UploadedFile instance.

1 like
christian_H's avatar

@Cronix Thanks for the tip. Its saying the mimetype is message/rfc822 so I guess there's something else going wrong

toby's avatar
toby
Best Answer
Level 31

Does uploading a PDF or Word document work correctly?

Maybe try to leave off the "spaces" after a comma:

$validator = Validator::make($request->all(), [
    'fileName' => 'required',
    'file' => 'required|mimes:pdf,doc,docx,rtf,txt,eml'
]);
1 like
christian_H's avatar

@toby Thanks, that fixed it! For some weird reason, it was working fine with the spaces before I tried to add the eml validation. I'm not complaining though, it works! Thanks again!

Please or to participate in this conversation.