Send Email with Attachment in Laravel
This post will give you example of how to send email with attachment in laravel.In this article, we will implement sending attachment in mail in laravel. Alright, let’s dive into the steps.
This tutorial of sending mail with attachment can be implemented in following versions of laravel:- laravel 6,7,8.
Step 1: Install Laravel
I am going to explain step by step from scratch so, we need to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run below command:
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Make Configuration
In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel will use those sender details on email. So you can simply add as like following.
.env
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=MAIL_ADDRESS MAIL_PASSWORD=PASSWORD MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=MAIL_ADDRESS MAIL_FROM_NAME="${APP_NAME}" |
Step 3: Add Route
In this is step we need to create routes for items listing. so open your “routes/web.php” file and add following route.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PDFController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('send-email-pdf', [PDFController::class, 'index']); |
Step 4: Add Controller
Here,we require to create new controller PDFController that will manage generatePDF method of route.
make sure you have “files” folder in public with following files.
So let’s put below code.
app/Http/Controllers/PDFController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Http\Controllers; use PDF; use Mail; class PDFController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $data["email"] = "MAIL_ADDRESS"; $data["title"] = "Sending Attachment in mail"; $data["body"] = "This is Demo"; $files = [ public_path('files/160031367318.pdf'), public_path('files/1599882252.png'), ]; Mail::send('emails.myTestMail', $data, function($message)use($data, $files) { $message->to($data["email"], $data["email"]) ->subject($data["title"]); foreach ($files as $file){ $message->attach($file); } }); dd('Mail sent successfully'); } } |
Step 5: Create View File
In Last step, let’s create myTestMail.blade.php(resources/views/emails/myTestMail.blade.php) for layout of pdf file and put following code:
resources/views/emails/myTestMail.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>meghrajtechnosoft.com</title> </head> <body> <h1>{{ $title }}</h1> <p>{{ $body }}</p> <p>Thank you</p> </body> </html> |
Now you can run and check example.
It will send you email, let’ see.
Run Project:
1 |
php artisan serve |
Now hit URL as given below or you can run according to your routes:
1 |
localhost:8000/send-email-pdf |