Laravel 8 Send Email with PDF Attachment Tutorial - Yudhy Network

Share on:

Yudhy Network - Laravel 8 send pdf email. In this tutorial, you will learn how to send email with pdf attachment in laravel 8 app

Generate pdf using pdf library and send as attachment laravel 8. In this tutorial, we will show you how to send email with pdf attachment in laravel 8 app.

As well as learn how to generate or create pdf in laravel 8 app using PDF library.

Laravel 8 provide mail class for send email. So, we would like to show you send an email in laravel 8. As well as how to implement sending emails in laravel 8 using a mailable example.

Note that, you can use several SMTP drivers details (Mailgun, Postmark, Amazon SES, and sendmail) in .env file for sending email in laravel 8.

This tutorial will teach you to step by step on how to sending emails with pdf file in laravel 8.

Generate PDF and Send as Attachment Laravel 8

  • Step 1 – Install Laravel 8 App
  • Step 2 – Configuration SMTP in .env
  • Step 3 – Install PDF Library
  • Step 4 – Add Email Send Route
  • Step 5 – Create Directory And Blade View
  • Step 6 – Create Email Controller
  • Step 7 – Run Development Server

Step 1 – Install Laravel 8 App

In this step, use the following command to install or download laravel 8 application:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Configuration SMTP in .env

In this step, you need to configure smtp details in .env file like following:

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
Recommended:- How to fix Error: laravel.log could not be opened?

Step 3 – Install PDF Library

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
Recommended:- Laravel 8 Send Mail using Queue Tutorial

Step 4 – Add Send Email Route

In this step, open /web.php, so navigate to routes directory. And then add the following routes for send email:

use App\Http\Controllers\SendEmailController;

Route::get('send-email-pdf', [SendEmailController::class, 'index']);

Step 5 – Create Directory And Blade View

In this step, create directory name test inside resources/views directory. Then create an test.blade.php blade view file inside resources/views/ directory. And update the following code into it:

<!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> 
Recommended:- Laravel 8 QR Code Generator Tutorial Example

Step 6 – Create Send Email Controller

In this step, use the following command to create controller name SendEmailController:

php artisan make:controller SendEmailController

Then navigate to app/Http/Controllers directory and open SendEmailController.php. Then update the following code into it:

<?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'));
 }
}

Recommended:- How to Use Helper Function in Laravel 8

Step 7 – Run Development Server

In this step, use this PHP artisan serve command to start your server locally:

php artisan serve

Then open browser and fire the following URL on it:

http://127.0.0.1:8000/send-email-pdf

Conclusion

Laravel 8 send an emailwith pdf example, you have learned how to create a mailable class and send pdf with email in laravel 8. And using this class on how to send emails in laravel 8.

Recommended Laravel Posts

Recommended:- Laravel 8 Google Recaptcha V3 Example
Recommended:- Laravel 8 Generate Fake Data
Recommended:- Laravel 8 Multi Auth (Authentication) Tutorial
Recommended:- Laravel 8 Stripe Payment Gateway Integration Tutorial
Recommended:- Instamojo Payment Gateway Integration In Laravel 8
Recommended:- Laravel 8 Drag and Drop File Upload using Dropzone Tutorial
Recommended:- How to Install Ckeditor in Laravel 8
Recommended:- Laravel 8 Highcharts Example Tutorial
Recommended:- Laravel 8 JWT Rest API Authentication Example Tutorial
Recommended:- Laravel 8 JWT Rest API Authentication Example Tutorial
Recommended:- Laravel 8 Login with Linkedin Example Tutorial
Recommended:- Laravel 8 Livewire CRUD with Jetstream Tutorial
Recommended:- Laravel 8 Cron Job Task Scheduling Tutorial

If you have any questions or thoughts to share, use the comment form below to reach us.

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus