Hello, laravel web developers! In this article, we'll see how to generate a PDF file and send an email in laravel 11. In laravel 11, we'll send an email with a PDF attachment. In many cases, we are required to send emails with attached documents like PDF, Excel, and CSV for reports.
So, we'll generate a PDF file using the DomPDF composer package. After that, we'll send an email using Gmail SMTP.
Laravel 11 Generate PDF and Send Email Example
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel example-app
Next, we'll install barryvdh/laravel-dompdf composer package using the following command.
composer require barryvdh/laravel-dompdf
Then, we'll define the configuration for sending email using Gmail SMTP.
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=test@123
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Next, we'll create a mailable class using the following artisan command.
php artisan make:mail PDFMailExample
app/Mail/PDFMailExample.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Attachment;
class PDFMailExample extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: $this->mailData['title'],
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.pdfMail',
with: $this->mailData
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [
Attachment::fromData(fn () => $this->mailData['pdf']->output(), 'Report.pdf')
->withMime('application/pdf'),
];
}
}
Now, we'll define routes into the web.php file
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
Route::get('send-email-pdf', [PDFController::class, 'index']);
Next, we'll create a controller and define the logic of create a pdf and sending mail.
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\PDFMailExample;
use PDF;
use Mail;
class PDFController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data["email"] = "test@gmail.com";
$data["title"] = "From Techsolutionstuff";
$data["body"] = "Send PDF Attachment";
$pdf = PDF::loadView('emails.pdfMail', $data);
$data["pdf"] = $pdf;
Mail::to($data["email"])->send(new PDFMailExample($data));
dd('Mail sent successfully');
}
}
Then, we'll create a blade file for a PDF file.
resources/views/emails/pdfMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Generate PDF and Send Email - Techsolutionstuff</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>{{ $body }}</p>
<p>Thank you</p>
</body>
</html>
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like :