In this article, we will see how to send an email with an attachment in laravel 8. As we all know mail functionalities are common in all projects but if you want to send a mail with an attachment then this post is for you here we will see how to attach files in Mail Laravel 8.
So, let's see how to send PDF files in email in laravel 8, send multiple attachments in Mail Laravel 8, and send images in the mail using laravel 8.
Step 1: Set Configuration for Sending Email with Attachments
Step 2: Add Routes
Step 3: Add Controller
Step 4: Create Blade File
First of all, we need to add send email configuration with mail driver, mail host, mail port, mail username, mail password, etc in your .env file
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=TLS
Now, we will add a route in your routes/web.php file.
Route::get('send/mail', [SendMailController::class, 'send_mail'])->name('send_mail');
Now, we will create SendMailController and copy the below code for the attached file in the mail.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class SendMailController extends Controller
{
public function send_mail(Request $request)
{
$data["email"] = "test@gmail.com";
$data["title"] = "techsolutionstuff.com";
$files = [
public_path('attachments/Laravel.jpeg'),
public_path('attachments/Laravel_8_mail_example.pdf'),
];
Mail::send('mail.Test_mail', $data, function($message)use($data, $files) {
$message->to($data["email"])
->subject($data["title"]);
foreach ($files as $file){
$message->attach($file);
}
});
echo "Mail send successfully !!";
}
}
Now, create a blade file in this file location resources\views\mail\Test_mail.blade.php for display messages in the mail
Hi<br/>
This is test mail with attachments.<br />
Thank you !!
Output :
You might also like: