Hi dev,
Today, i show you laravel 10 E-Mail send with attachment. This article will give you simple example of laravel 10 E-Mail send with attachment. you will laravel 10 E-Mail send with attachment. In this article, we will implement a laravel 10 E-Mail send with attachment.
So, let’s follow few steps to create example of laravel 10 E-Mail send with attachment.
Step 1: Laravel 10 Install
Follow This Command And Install Laravel
composer create-project laravel/laravel example-email
Step 2: Install Package & Config
First, we need to install barryvdh/laravel-dompdf package. Run this composer command to install the package:
composer require barryvdh/laravel-dompdf
Now open the .env file and set your SMTP credentials:
.env
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}"
Step 3: Add Route
routes/web.php
<?php use App\Http\Controllers\TestController; use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | 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', [TestController::class, 'sendMailWithPDF']);
Step 4: Add Controller
app/Http/Controllers/TestController.php
<?php namespace App\Http\Controllers; use PDF; use Mail; class TestController extends Controller { /** * Write code on Method * * @return response() */ public function sendMailWithPDF() { $data["email"] = "test@gmail.com"; $data["title"] = "Welcome to codeplaners.com"; $data["body"] = "This is the email body."; $pdf = PDF::loadView('mail', $data); Mail::send('mail', $data, function ($message) use ($data, $pdf) { $message->to($data["email"], $data["email"]) ->subject($data["title"]) ->attachData($pdf->output(), "test.pdf"); }); dd('Email has been sent successfully'); } }
Step 5: Add PDF Blade File
resources/views/mail.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 10 E-Mail Send With Attachment - codeplaners.com</title> </head> <body> <h3>{{ $title }}</h3> <p>{{ $body }}</p> <p> Regards,<br/> codeplaners.com </p> </body> </html>
Step 6: Run Laravel:
php artisan serve
Now, you have to open the web browser, type the given URL and view the app output:
http://localhost:8000/send-email
I hope it will assist you…