Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 Send Email with PDF Attachment Tutorial

Last Updated on May 9, 2024 by

It has become very easy for users to send emails with attached PDF file with the help of Laravel 11 built-in methods and classes. In this post, I will give you example how to send email with attachments in Laravel 11.

Let’s start sending email by attaching a PDF file:

Step 1: Install Laravel

Start your terminal and run the following Composer command to install new laravel 11:

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

Step 2: Configure Email Settings

Navigate to the .env file in your Laravel project and configure your email settings according to your email provider; like the following:

MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_email@example.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@example.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3: Create a Mailable

To create a new Mailable class using Laravel’s Artisan command:

php artisan make:mail MailPDF

Step 4: Configure the Mailable

In the App\Mail\MailPDF.php file, define the email content and subject within the content() and add pdf in attachment() method:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class MailPDF extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(private string $title, private string  $body)
    {
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Laravel Email with Attachment itcodStuff.com',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.pdfView',
            with: [
                'title' => $this->title,
                'body' => $this->body,
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [
            Attachment::fromPath('/path/to/attachment.pdf')->filename('attachment.pdf'),
        ];
    }
}

Step 5: Create Email Template

By default email templates in the resources/views/emails folder. Create a pdfView.blade.php Blade template in it:

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <p>{{ $body }}</p>
</body>
</html>

Step 6: Create Controller and Handle Send Email

Create a new controller using the following Artisan command:

php artisan make:controller MailController

Define a method for sending the email in app/http/controllers/MailController.php file ; like the following:

<?php

namespace App\Http\Controllers;

use App\Mail\MailPDF;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function sendPDFMail()
    {
        $title = 'Send email with attachment ITCODSTUFF.COM';
        $body = 'This is pdf email example from itcodStuff.com';

        Mail::to('[email protected]')->send(new MailPDF($title, $body));

        return "PDF mail has been sent successfully!";
    }
}

Step 7: Define Routes

Open the routes/web.php file and add the following route definition:

use App\Http\Controllers\MailController;

Route::get('/send-pdf', [MailController::class, 'sendPDFMail']);

Step 8: Testing

To test the laravel 11 send email application, run this application by using php artisan serve command:

php artisan serve

And then trigger the route http://127.0.0.1:8000/send-pdf on browser that sends the email with pdf attachment. Check the recipient’s inbox for the email.

Leave a Comment