In this article, we will see laravel 10 send emails with attachments. Here, we will learn about how to send emails with an attachment in laravel 10 and laravel 11. As we all know mail functionalities are common in all projects but if you want to send a mail with an attachment in some cases then this post is for you here we will see how to attach the pdf file and images in an email in laravel 10 and laravel 11.
So, let's see how to send an email with an attachment in laravel 10, how to send a PDF to email in laravel 10/11, and send multiple attachments in Mail Laravel 10.
Also, you can send any attachment format or files like a PNG file, excel file, CSV file, and much more and for sending mail we will use Mailtrap for testing purposes. So, you use it as per the requirement.
Step 1: Install Laravel 10/11
Step 2: Setup Mail Configuration
Step 3: Add Route
Step 4: Add Controller
Step 5: Create Blade File
Step 6: Run Laravel 10 Application
In this step, we will install the laravel 10 application using the following command.
composer create-project --prefer-dist laravel/laravel laravel_10_email_with_attachment_example
Now, we will add email configuration with mail driver, mail host, mail port, mail username, mail password, etc in the .env file.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_user_name
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=TLS
Now, add a route in the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendMailController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('send/mail', [SendMailController::class, 'sendMailWithAttachment']);
In this step, we will create SendMailController and add the below code in the controller.
app/Http/Controllers/SendMailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class SendMailController extends Controller
{
public function sendMailWithAttachment(Request $request)
{
$data["email"] = "test@gmail.com";
$data["title"] = "Techsolutionstuff";
$data["body"] = "This is test mail with attachment";
$files = [
public_path('attachments/test_image.jpeg'),
public_path('attachments/test_pdf.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, we will create a test_mail.blade.php file.
resources\views\mail\test_mail.blade.php
Hi, Techsolutionstuff <br/>
This is Test Mail.<br />
Thank you...!!
Now, run this laravel 10 send an email with an attachment application using the artisan command.
php artisan serve
You might also like: