Home > Mobile >  Why don't I see the uploaded attachment in the sent email?
Why don't I see the uploaded attachment in the sent email?

Time:04-10

I'm not sure why I can't see the uploaded attachment in the email that's being sent successfully. Instead, I'm seeing file: /tump/php/Du460K instead of the actual attachment.

I thought my foreach() inside Mail:: would've done the trick but it hasn't.

What am I doing wrong?

Here's MailController.php

public function support(Request $request) {
    $name        = $request->get('name');
    $email       = $request->get('email');
    $file        = $request->file('file');
    $messageText = $request->get('message');

    $data = [
        'name' => $name,
        'email' => $email,
        'file' => $file,
        'message' => $messageText
    ];

    Mail::send('email.support.support', $data, function($message) use($file) {
        $fileName = $file->getClientOriginalName();
        $mimeType = $file->getMimeType();

        $message->to('[email protected]');

        $data = [
            'as' => $fileName,
            'mime' => $mimeType
        ];

        if(!empty($file)) {
            foreach($file as $userFile) {
                $message->attach($userFile->getRealPath(), $data);
            }
        }

    });

    view('email.support.support', compact('name', 'email', 'file', 'messageText'));

    return Redirect::back()->with('success', 'Successfully submitted!');
}

Here's support.blade.php

<div >
    <p>Name: {{ $fullName ?? "" }}</p>
    <p>Email: {{ $email ?? "" }}</p>
    <p>Message: {{ $messageText ?? "" }}</p>
    <p>file: {{ $file ?? "" }}</p>
</div>

CodePudding user response:

you should store the file on a specific disk and return the URL to attach() method

check this

https://laravel.com/docs/9.x/filesystem#file-uploads

I hope it's helpful

CodePudding user response:

first store the image and then use the stored image path.

$filePath = $file->store('imagePathDirectory');
 if(!empty($file)) {
     foreach($file as $userFile) 
         $message->attach($userFile->$filePath, $data);
     }
 }
  • Related