Categories: LaravelProgramming

Laravel Send Email With PDF Attachment

In Laravel, you can send emails with PDF attachments using the built-in email sending functionality and the “dompdf” package for generating PDFs.

  1. Install the “dompdf/dompdf” package by running the command: composer require dompdf/dompdf
  2. In your controller, use the package to generate the PDF. For example:
use Dompdf\Dompdf;

class MyController extends Controller {
    public function sendEmailWithPDF() {
        $pdf = new Dompdf();
        $pdf->loadHtml('<h1>Hello World</h1>');
        $pdf->render();
        $pdf->stream("sample.pdf", array("Attachment" => false));
    }
}

3. In your controller, use Laravel’s built-in email sending functionality to send the email with the PDF attachment. For example:

use Mail;

class MyController extends Controller {
    public function sendEmailWithPDF() {
        $pdf = new Dompdf();
        $pdf->loadHtml('<h1>Hello World</h1>');
        $pdf->render();
        $pdf = $pdf->output();

        $attachment = chunk_split(base64_encode($pdf));

        Mail::send('emails.send', ['title' => 'Email With PDF Attachment'], function($message) use($attachment) {
            $message->to('recipient@example.com', 'Recipient');
            $message->subject('Email With PDF Attachment');
            $message->attachData($pdf, "sample.pdf");
            $message->from('sender@example.com','Sender');
        });
    }
}

Please ensure your email configurations are set correctly in .env and config/mail.php.

Marknepal

Recent Posts

Bisket jatra highlight

भक्तपुरमा नौ दिन आठ रातसम्म परम्परागत विधिअनुसार मनाइने विश्वप्रसिद्ध बिस्का: जात्रा आज चैत २७ गते…

4 weeks ago

Viennese Jewish – The Woman Who Invented WI-FI

The Woman Who Invented WI-FI. The most beautiful woman in the world in cinema and…

3 months ago

Sadhguru Quotes in nepali

“यस क्षण” कुनै सोच होइन । यो एक मात्र वास्तविकता हो तपाईं आफूलाई अरूसँग तुलना…

3 months ago

how to check See result with marksheet

Dear SEE Students: तपाईंको नतिजाको Grading system यस्तो रहनेछ ! कुनै पनि विद्यार्थी फेल हुने…

10 months ago

10 essential prompts to boost your

10 essential prompts to boost your productivity: 1. Summarize: "Summarize the text below into 500…

12 months ago

15 powerful ChatGPT prompts to save you 15 hours a week

15 powerful ChatGPT prompts to save you 15 hours a week: 1. Use the 80/20…

12 months ago

This website uses cookies.