To send email with attachment Laravel 10/9; Through this tutorial, i am going to show you how to send email with attachment (pdf, word, txt etc file) in Laravel 10/9 apps.
Laravel 10/9 Send Email with Attachment Example
Follow the below given steps to send email with attachment in Laravel 10/9 apps:
- Step 1 – Install Laravel 10/9 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 10/9 App
Run following command on command prompt to install or download Laravel 10/9 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
Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.
Once less secure apps are enabled; now you can use your Gmail for sending the emails.
Step 3 – Install PDF Library
Run the following command on terminal to install PDF package to generate PDF:
composer require niklasravnsborg/laravel-pdf
To start using Laravel, add the Service Provider and the Facade to your config/app.php
:
'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ] 'aliases' => [ // ... 'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class ]
Run the following command on terminal to publish package’s config file to your config directory by using following command:
php artisan vendor:publish
Step 4 – Add Send Email Route
Visit to routes directory and open web.php file. 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
To 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 10/9 Send Email with Pdf Example</title> </head> <body> <h1>This is test mail from Laratutorials.com</h1> <p>Laravel 10/9 send email with Pdf example</p> </body> </html>
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')); } }
Step 7 – Run Development Server
Run PHP artisan serve command on command prompt 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 10/9 send an email with attachment example, you have learned how to create a mailable class and send pdf with email in Laravel 10/9.
Be First to Comment