Hello World, Welcome to projectsplaza.com. Today I am going to show you how to send email with attachment in laravel with Mailtrap. This is a very easy and simple tutorial. In this tutorial, I will send a simple email with attachment from my controller function. I have divided this tutorial into the following parts.
- Setup Mailtrap account
- Connect Mailtrap with laravel
- Email configuration in laravel
- Send email with attachment from the controller
I have assumed that you have basic knowledge of laravel.
SetUp Mailtrap account
- Create an account at Mailtrap website https://mailtrap.io/.
- Get the credentials from your demo inbox.
Connect Mailtrap with laravel
- Laravel give us provision to setup smtp in .env. Get the credentials from mailtrap account and setup in .env file.
Email configuration in laravel
- Run the following command to create Mailable class.
- php artisan make: mail SendNotification
- Open the above file app/Mai/SendNotifaction.php
- Change the build method code as following
return $this->view('view-file-name')
->attach('/path/to/attachment');
Send email from controller
- Add the following two classes in your controller before start
- use Illuminate\Support\Facades\Mail;
- use App\Mail\SendNotification;
- In Controller method, Add the following code to send mail
- Mail::to(‘reciever_email’)->send(new VendorNotification());
- Open the routes/web.php and add the following code in it.
- Route::get(‘/send-email’,’HomeController@send_email’);
- Open Browser and hit the URL with /send-email
- Now check Mailtrap inbox, you will receive a HTML email
Read more about Mail Functionality
I hope this tutorial helps you. Please add your feedback in the comment section. Thank you 🙂