Disclosure: This post may contain affiliate links, meaning I get a commission if you decide to make a purchase through my links, at no cost to you. Please read my disclosure for more info.
Hi Guys welcome back to Coding Of Cents. Today we will learn how to send email with attachment with Laravel.
In my previous blog, I had written how to send email with laravel in general.
Before follow you follow this step make sure you have already installed Laravel on your computer and you read my post about how to set up configuration before send the email.
Here is useful link that you can read that related to this topic
- In this tutorial I use Laragon as local server, check my post about how to install laragon on windows.
- Maybe you also need to know how to install composer on windows.
- You can also test send email with basic configuration.
Before we can send an email with attachment with laravel, first we need to do is create a mail class that handles the process to send the email. we can create mail class with this command.
php artisan make:mail InvoiceWithAttachment
We use the attach method to send an email with an attachment with Laravel. If we need to send many attachment we can pass array then loop it in build method.
After that, create an email template that used to display our email content.
Create sendInvoice.blade.php in resources/views/mail folder and edit like bellow.
Next, we need to do is create a controller that can handle our logic. To create controller we can use PHP artisan command.
php artisan make:controller InvoiceAttachmentController
To send an email with an attachment with laravel we need to add an array that includes some file that we want to attach.
Next after creating the logic, we need to call the controller on route so we can send an email with the attachment. Add a line in route and add this code
Route::get("email", "[email protected]");
That’s all step to send emails with attachments, hope you enjoy that. Thank you for reading, keep trying!