Hello Developer,
Today i have to send billing email to our office client client, so i have to send email with invoice , so i created this, Now i am going to share with you
“How can we send email with attachment”
Step 1. Install Laravel Application.
Step 2. Install the package which will required to generate PDF.
composer require barryvdh/laravel-dompdf
Step 3. Setup .env file for mail like below
MAIL_MAILER=smtp MAIL_HOST="in-v3.mailjet.com" MAIL_PORT=587 MAIL_USERNAME="818c68a3719aca963XXXXXXXXXXXX" MAIL_PASSWORD="8e9fXXXXXXXXXXXXXXXXXXXXXXXXX" MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="[email protected]" MAIL_FROM_NAME="${APP_NAME}"
Note– Use your email , user name and password.
Step 4. Create a controller in my case my controller name is “AdmReportController“.
<?php namespace App\Http\Controllers\Admin; use App\Models\AdmReportModel; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session; class AdmReportController extends Controller { public function sendinvoice($id) { $data["data"] = DB::table('impression') ->where("impression_.u_id",$id) ->get(); $data["email"] = DB::table('users') ->where("users.id",$id) ->get()->first(); $data["title"] = "Month of ".date('M-Y', strtotime('first day of last month')); $data["body"] = "Hi, <br> Your Monthly bill is generate please check the attached file."; $pdf = PDF::loadView('mail', $data); Mail::send('mail', $data, function ($message) use ($data, $pdf) { $message->to($data["email"], $data["email"]) ->subject($data["title"]) ->attachData($pdf->output(), date('M-Y', strtotime('first day of last month')).".pdf"); }); dd('Email has been sent successfully'); } }
Note– I am using this with cron to send report automatically, you can use as your project required.
“mail.blade.php”
<x-admin_header></x-admin_header> <link rel="stylesheet" type="text/css" href="{{$_ENV['APP_URL']}}assets/date-picker/DateTimePicker.css" /> <!-- Main Content --> <div id="content"> <!-- Begin Page Content --> <div class="container-fluid"> <div style="background: #FFFFFF; padding:15px"> <!-- Page Heading --> <div class="row"> <!-- invoice Header Chart --> <div class="col-xl-6 col-lg-6"> <div class="col-xl-12 col-lg-12"> <img src="{{$_ENV['APP_URL'].'assets/img/sureview_logo.png'}}" width="280"> </div> <div class="col-xl-12 col-lg-12"> <span style="font-weight: 600; font-size: 41px; color: black;">Tax Invoice</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">Invoice number: #{{$info->id}}</span> </div> </div> <div class="col-xl-6 col-lg-6"> <div class="col-xl-12 col-lg-12" style="text-align: end;"> <span style="font-weight: 500; font-size: 22px; color: black;">Sureview.tv</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">DTEC</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">Dubai Silicon Oasis</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">Dubai, Dubai 111981, AE</span> <br> </div> </div> <div class="col-xl-6 col-lg-6"> <div class="col-xl-12 col-lg-12"> <br> <span style="font-weight: 500; font-size: 20px; color: black;">Bill To</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">{{$info->name}}</span> <br> <span style="font-weight: 400; font-size: 14px; color: black;">{{$info->email}}</span> <br> </div> </div> </div> <div class="row"> <div class="col-sm-12"> <br> <table class="table tbl-responsive"> <thead> <td style="color: #000000;font-weight: 400;">Sr.</td> <td style="color: #000000;font-weight: 400;">Description</td> <td style="text-align:right;color: #000000;font-weight: 400;">Amount</td> </thead> <tr> <td style="color: #000000;font-weight: 300;">1</td> <td style="color: #000000;font-weight: 300;">Total Click 110</td> <td style="text-align:right;color: #000000;">$200</td> </tr> <tr> <td style="color: #000000;font-weight: 300;">2</td> <td style="color: #000000;font-weight: 300;">Total campaign view</td> <td style="text-align:right;color: #000000;">$300</td> </tr> <tr> <td colspan="3" style="text-align:right;color: #000000;">Total - $500</td> </tr> </table> </div> </div> <div class="row"> <div class="col-sm-12"> <center style="font-size: 10px;">Tel: +971 (4) 5013939, Cell: +971 (50) 5272554 DIGIMENA FZE, Office 126, DTEC, Dubai Silicon Oasis, Dubai, UAE. P.O.Box: 111981 - Dubai, UAE</center> </div> </div> </div> <br> </div> <!-- /.container-fluid --> </div> <!-- End of Main Content --> <x-admin_footer></x-admin_footer> <link href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" rel="stylesheet"/> <script src="{{$_ENV['APP_URL']}}assets/cdn/jquery.dataTables.min.js"></script> <script> $(document).ready(function() { $('.alert-success').fadeIn().delay(10000).fadeOut(); }); </script>
Step 5. Set Route for mail
Route::get('send-invoice/{id}', [AdmReportController::class, 'sendinvoice']);
Now run and test your project.