Attach a file in an email

Viewed 233

I want to send a file as an attachement with the email sent from my laravel application. this is how I tried to attach the file

  $path = Storage::url($this->newsletter["file"]);

  Mail::send(new NewsLetterEmail($this->newsletter), [], function ($message) use ($path) {
        $message->attach($path);
  });

But the email is being sent with no attachment. How can I attach a file to the email?

1 Answers

You need to use storage_path() that will point to the file in storage path, then you can do so

 Mail::send(new NewsLetterEmail($this->newsletter), [], function ($message) use ($path) {
    $message->attach($path);
 });

if you need to change the attached file name or add the file mime type you can add

 Mail::send(new NewsLetterEmail($this->newsletter), [], function ($message) use ($path) {
    $message->attach($path, [
       'as' => 'Attached file name',
       'mime' => 'Attached file mime type',
    ]);
 });
Related