Search code examples
phplaravel-5mailer

Why Laravel 5 mail, I attached a .tmp file?


I am trying to attach a file in an email that is sent from a form, the problem is that the file is coming to me .tmp

My Controller

public function choiceAnalyst(Request $request){

    $userSelect = $request->input('user');
    $data = User::where('id', '=', $userSelect)->first();
    $data->attach = $request->file('document')->getRealPath();

    Mail::to('eaquino@spi.com.ve')->send(new AnalystMonth($data));

    return redirect()->route('home', ['message' => 'Correo enviado correctamente']);
}

My Class

class AnalystMonth extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mails.analystMonth')->attach($this->user->attach);
    }
}

I think I got .tmp by the getRealPath() method that I include when I pick up the file, but I'm not sure. How do I get my file to reach the .tmp extension?


Solution

  • You are receiving a .tmp file because the file has never been uploaded to the server before you attach it to your mail.

    $data->attach = storage_path('app/public/' . $request->file('document')->store('folder', 'public'));
    

    You can achieve the same result with the Storage facade:

    $data->attach = Storage::disk('public')->path($request->file('document')->store('folder', 'public'));