Laravel 11 Send Email with Attachment Example Tutorial

Laravel 11 Send Email with Attachment Example Tutorial

In Laravel 11, create a mailable class and attachment (pdf, txt, jpg, png, CSV) in it and then make a controller to send email with the attached file.

Here are steps to send email with attachments using SMTP and mailable class:

Step 1 – Setup New Laravel 11 Project

To set the new Laravel 11 application on the server you can use the given command:

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

Step 2 – Configure Mail Server

Open the .env file in the root directory of your Laravel application, and configure mail server details in it; Something like this:

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

To allow non-secure apps to access Gmail to send emails, you can go to your Gmail settings here and click Enable less secure apps.

Step 3 – Create PDF Mail Class

Run the following command to create a mail class:

php artisan make:mail PDFMail

Edit PDFMail.php file from app/mail folder and Attach the PDF file to this mail class:

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 PDFMail 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: 'Welcome on tutsmake.com',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.pdfMail',
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 4 – Define Route

Define a route in the web.php file to handle the request to send mail; Something like this:

use App\Http\Controllers\SendPdfController;

Route::get('send-email-pdf', [SendPdfController::class, 'sendEmailPDF']);

Step 5 – Create a Controller File

Run the following command to make a controller file to handle the email sending process:

php artisan make:controller SendPdfController

Open a SendPdfController.php controller file from app/http/controller folder and update the following code into it:

<?php

namespace App\Http\Controllers;

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

class SendPdfController extends Controller
{
public function sendEmailPDF()
{
$title = 'PDF Mail from Tutsmake.com';
$body = 'Here is attachement file from tutsmake.com';

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

return "Email with pdf has been sent successfully!";
}
}

Step 6 – Create an Email View

Create a pdfMail.blade.php file inside the resources/views/emails folder and add the following HTML code into it:

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

Step 7 – Test Application

Run artisan serve command to start the application server for testing:

php artisan serve

Open browser with URL http://127.0.0.1:8000/send-email-pdf to send mail with pdf from laravel 11 application.

Conclusion

That’s it! When you call the sendEmailPDF() method, it will send email the generated PDF file attached.

Recommended Guides

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *