How to Send PDF Attachment using Email in Laravel
PDF Attachment using Email in Laravel 10
PDF Attachment using Email in this tutorial will guide you through the process of sending emails with PDF attachments in Laravel, utilizing the Laravel PDF package.
For Laravel developers keen on learning how to construct a controller to facilitate sending PDF attachments via email in Laravel, this guide is designed specifically for you.
To accomplish this task, several elements are needed: a Laravel application, a mail trap for sending and testing emails with PDF attachments, a controller, routes, and a view file.
For sending PDF attachment please follow following step-
Laravel 10 Send PDF Attachment with Email Example
- Step 1: Install PDF Package
- Step 2: Set Up Laravel PDF
- Step 3: Register SMTP in Env
- Step 4: Set Up Send Email Controller
- Step 5: Create Send PDF Attachment View
- Step 6: Build New Routes
- Step 7: Run Application Server
1. Install PDF Package
To mail PDF attachments in Laravel is made possible by the laravel-pdf package. This library does the core work for us; you have to install it to take advantage.
composer require niklasravnsborg/laravel-pdf
2.Set Up Laravel PDF
We have to move into the app.php file that you can see in the config/ directory.
You must register the service provider and facades in providers and aliases, respectively.
'providers' => [
/* Application Service Providers */
niklasravnsborg\LaravelPdf\PdfServiceProvider::class
],
'aliases' => Facade::defaultAliases()->merge([
'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class
])->toArray(),
We are now going to run the command; our purpose is to publish the library config file to the config folder.
php artisan vendor:publish
3.Register SMTP in Env
In order to send a mail with pdf, you have to add the credentials inside the .env config file. You can look for this file inside your laravel project.
MAIL_DRIVER=smtp
MAIL_HOST=your_smtp_host
MAIL_PORT=587
MAIL_USERNAME= USER_NAME_GOES_HERE
MAIL_PASSWORD= PASSWORD_GOES_HERE
MAIL_ENCRYPTION=tls
4.Set Up Send Email Controller
You need to execute the command to create a new controller. This file will contain all the code and logic required to implement the desired feature.
php artisan make:controller PdfAndMailController
After invoking the above command, a new file is created; hence, you have to open the app/Http/Controllers/PdfAndMailController.php file further and add the given code to the file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class PdfAndMailController extends Controller
{
public function index(Request $request){
{
$data["email"] = "abc@abc.com"; // Change this
$data["title"] = "Abc"; // Change this
$data["body"] = "Send pdf in Email"; // Change this
$files = [
public_path('attachments/demo.jpeg'),
public_path('attachments/tariff_rates.pdf'),
];
Mail::send('mail.pdf_mail', $data, function($message)use($data, $files) {
$message->to($data["email"])
->subject($data["title"]);
foreach ($files as $file){
$message->attach($file);
}
});
echo "Mail successfully sent!";
}
}
}
Next, we will generate a mail template using the Laravel Blade file.
Ensure to create the resources\views\mail\demo_mail.blade.php
file and insert the provided code into it.
5.Create Send PDF Attachment View
Additionally, you need to configure the view file. Navigate to the resources/views
directory, create the home.blade.php
file, and insert the provided code into it.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Email PDF Attachment Example</title>
</head>
<body class="antialiased">
<h2>A mail sent </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam non ligula ligula. </p>
</body>
</html>
6. Build New Routes
Proceed to locate the resources/
folder. Inside this directory, you will find the web.php
file. Be sure to define the route that will facilitate sending the email with a PDF attachment.
Route::get('send-pdf-via-mail', [PdfAndMailController::class, 'index']);
7.Run Application Server
We have successfully created the functionality; now you need to run your application.
Thank you for reaching out! If you have any specific questions or topics in mind, please feel free to share them, and I’ll do my best to assist you. Whether you’re curious about a particular technology, scientific concept, literary work, or anything else, I’m here to provide information, advice, or engage in a discussion. Don’t hesitate to let me know how I can help you further!
I do not even know how I ended up here but I thought this post was great I dont know who you are but definitely youre going to a famous blogger if you arent already Cheers.