Laravel 9 Generate PDF And Attach To Email Example
Teams applying “Laravel 9 Generate PDF And Attach To Email Example” in a production project can use the linked resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi Dev,
This example is focused on laravel 9 generate PDF and attach to email example. We will look at example of how to generate PDF and attach to email example in laravel 9. I explained simply about laravel 9 pdf with attach to email. This article will give you simple example of laravel 9 pdf. Follow bellow tutorial step of how to generate PDF.
In this article I’m going to share how to generate PDF and attach it to the email.
I will give you step by step implementation of how to use laravel 9 generate PDF and attach to email example.So let's follow bellow step.
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Install Package & Config
At first we need to install barryvdh/laravel-dompdf package. Run this composer command to install the package:
composer require barryvdh/laravel-dompdf
Now open .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 Controller
Create a test controller and make a function to send email:
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"] = "[email protected]";
$data["title"] = "Welcome to NiceSnippets.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 4: Add PDF Blade File
Now we’ll create the PDF view which will be attached to the email. Go to resources>views folder and create mail.blade.php file. Then paste this simple PDF view:
resources/views/mail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 Generate PDF And Attach To Email Example - NiceSnippets.com</title>
</head>
<body>
<h3>{{ $title }}</h3>
<p>{{ $body }}</p>
<p>
Regards,<br/>
NiceSnippets.com
</p>
</body>
</html>
Step 5: 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']);
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/send-email
I hope it will help you..
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial