Hello artisan,
In this brand new tutorial, I will show you laravel livewire send attachment with mail example. From this example, you will learn how to send multiple attachment in laravel livewire with mail. You can send any type of file with laravel mail with livewire. So if you don't know how to send multiple attachments in laravel mail then this example is for you.
I will show you step by step how to send email in laravel livewire with multiple attachments. First, we simply upload our multiple files to the storage directory then we will send that attachment with email in laravel livewire. So let's see the example code of laravel attach file to email from storage in livewire.
App\Http\Livewire\ComposeEmail.php
namespace App\Http\Livewire;
use App\Models\Compose;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
class ComposeEmail extends Component
{
use WithFileUploads;
public $to = '';
public $subject = '';
public $body = '';
public $file;
public function sendEmail()
{
$this->validate([
'to' => 'required|email',
'subject' => 'required',
'body' => 'required|min:10'
]);
$data['name'] = auth()->user()->name;
$data['email'] = $this->to;
$data['subject'] = $this->subject;
$data['mgs'] = $this->body;
$data['contact_mail'] = $this->to;
$files = [];
if($this->file) {
foreach ($this->file as $photo) {
$files[] = $photo->store('attachment');
}
}
Mail::send('email.mail', $data, function($message) use ($data,$files) {
$message->to($data['contact_mail'], $data['name'])->subject($data['subject']. $data['name']);
$message->from(auth()->user()->email, $data['name']);
foreach ($files as $file){
$attachment = Storage::path($file);
$message->attach($attachment);
}
});
}
public function render()
{
return view('livewire.compose-email');
}
}
Now update the blade file like:
resources/views/livewire/compose-email.blade.php
Now update the blade file for mail like:
resources/views/email/mail.blade.php
{{ $mgs }}
Read also: Laravel 9 Livewire Multiple Image/File Upload Tutorial
Hope it can hep you.
#laravel #laravel-9x #laravel-mail