Wednesday, April 12, 2017

Laravel 5.2: attach file with email, how to catch the file extension?

Trying to create email witch will has two type of attachments (PDF, DOC\s).

Now this what I have done in my controller

public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
            'job' => 'required',
            'file' => 'mimes:pdf,doc|max:10000'
        ]);

        $view = 'emails.joinUs';
        $data = array(
            'subject' => $request->name . ' Ask to join our team',
            'name' => $request->name,
            'phone' => $request->phone,
            'email' => $request->email,
            'job' => $request->job,
            'file' => $request->file,
        );

        Mail::send($view, $data, function ($message) use ($data) {
            $message->from($data['email'], $data['name']);
            $message->to('yousef@dtegypt.com')->subject($data['subject']);
            $message->attach($data['file'], ['as'=>'CV.pdf']);
        });

        if (count(Mail::failures()) > 0) {

            foreach (Mail::failures as $email_address) {
                echo "$email_address <br />";
            }
        }
        return Redirect::back()->with('mailStates', 'thanks for contacting us');
    } 

now coming to the point

if I leave attachment like this

$message->attach($data['file']

without the file extension I receive email with

file.dat

if I set the file extension

$message->attach($data['file'], ['as'=>'CV.pdf']);

I receive

CV.pdf

but what if the uploaded file is doc or docs? is there a way to leave the extension to the dedicated extension from the attached file?



via Yousef Altaf

Advertisement