Laravel
175
Admin
31-12-2020
Laravel 8 send email with pdf. In this example, i will show you how to send email with pdf attachment in laravel 8 app. And You will learn step by step how to configure email smtp detail and send mails/emails.
Laravel has supported many API and drivers like SMTP, SendMail, Mailgun, Sendgrid, Mandrill, Amazon SES, SmartPost, Gmail etc for send mail/email for laravel web applications.
When you are working with Laravel web application. So you have to send mail to users in many ways. Like sometimes the user has to send email verification email. Or send an invoice in the mail. Or other types of information need to be sent in email.
First Step, you need to download or install laravel web application new setup on your local machine. you can use the following command to download or install laravel application new setup:
composer create-project laravel/laravel blog –-prefer-dist
If you want to install a specific version of laravel, you can follow the following tutorial
https://w3alert.com/laravel-tutorial/laravel-install-on-windows-ubuntu-with-composer
This is the first step, go to .env file and set SMTP details in .env file.
You could do like:
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=Add your user name here MAIL_PASSWORD=Add your password here MAIL_ENCRYPTION=tls
The above email SMTP details are default and necessary. You can also set the following things:
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
The above are used by Laravel web applications when you will send emails/mails to users. The from email address and name part of the email that is displayed to users.
In next step, execute the following command on terminal to install PDF package to generate PDF. In this tutorial i will use niklasravnsborg/laravel-pdf pckage to generate pdf. So run following command to install it:
composer require niklasravnsborg/laravel-pdf
To start using Laravel, add the Service Provider and the Facade to your config/app.php
:
config/app.php
'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ] 'aliases' => [ // ... 'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class ]
Now, execute the following command on terminal to publish package's config file to your config directory by using following command:
php artisan vendor:publish
In this step, i will create a controller for sending emails/mails with pdf file.
Just open your command prompt (CMD) and go to your project root directory. Then type the below command for creating a new controller.
php artisan make:controller SendEmailController
After that, You need to create one method in your controller. So go to the => app/Http/Controllers/SendEmailController.php And update the below given code into your controller:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use PDF; class SendEmailController extends Controller { public function sendmail(Request $request){ $data["email"]=$request->get("email"); $data["client_name"]=$request->get("client_name"); $data["subject"]=$request->get("subject"); $pdf = PDF::loadView('test', $data); try{ Mail::send('mails.mail', $data, function($message)use($data,$pdf) { $message->to($data["email"], $data["client_name"]) ->subject($data["subject"]) ->attachData($pdf->output(), "invoice.pdf"); }); }catch(JWTException $exception){ $this->serverstatuscode = "0"; $this->serverstatusdes = $exception->getMessage(); } if (Mail::failures()) { $this->statusdesc = "Error sending mail"; $this->statuscode = "0"; }else{ $this->statusdesc = "Message sent Succesfully"; $this->statuscode = "1"; } return response()->json(compact('this')); } }
Next step, here you need to create one route and one blade view file.
So go to routes/web.php and update the following route in your file:
use App\Http\Controllers\SendEmailController; Route::get('send-email-pdf', [SendEmailController::class, 'index']);
After that, go to your resources/views/ and create a new blade view file name test.blade.php and update the below code into your file:
<!DOCTYPE html> <html> <head> <title>Laravel 8 Send Email Example</title> </head> <body> <h1>This is test mail from Tutsmake.com</h1> <p>Laravel 8 send email example</p> </body> </html>
You can use the following command artisan command to start the development server:
php artisan serve
Hit the following URL in your browser and test:
http://127.0.0.1:8000/send-email-pdf