Send attachment in mail in laravel 10

Send attachment in mail in laravel 10

14-Sep-2024
| |
Image Carousel

Hello developers in this tutorial we will discuss about how to send attachment-pdf , image with email in laravel 

Table of Contents

S.no Contents-topics
1 Install PDF package
2 Setup PDF in laravel
3 Setup smtp driver in .env
4 Create pdf view blade
5 Setup EmailController
6 Setup routes.php
7 Sending email with attachment

1:Install PDF package

PDF means Portable Document Format that is used for exchanging documents  , to mail PDF attachments in Laravel is made possible by the laravel-pdf package , for installing package hit the command below on terminal.
code for pdf package : 
Copy

composer require niklasravnsborg/laravel-pdf

2:Setup PDF in laravel

For setting up PDF in laravel go to config/app.php register pdf provider and alias in app.php file 
Copy for creating artisan commandCopy

  'providers' => [
         /* Application Service Providers */
         niklasravnsborg\LaravelPdf\PdfServiceProvider::class
        ],
        'aliases' => Facade::defaultAliases()->merge([
         'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class
        ])->toArray(),

Now we have to publish the library 
code for publish 
Copy

php artisan vendor:publish

3:Setup smtp driver in .env

For sending email using SMTP driver we have to setup mail driver in .env file
smtp mail code:
 Copy

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=USERNAME
MAIL_PASSWORD=PASSWORD
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS=USERNAME
MAIL_FROM_NAME="${APP_NAME}"

Here  protected $signature = 'make:view {view}';  represents how to start this command and {view} means what should be name of our view file after that in handle() function consist of main logic behind the command to be executable.
 

4:Create pdf view blade

create a view file inside resources/views/sendEmail.blade.php and copy the code given below
sendEmail.blade.php:
  Copy

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel 10 Email PDF Attachment Example</title>
    </head>
    <body class="antialiased">
       <h2>A demo mail sent from DeveloperCodez.com</h2>
       <p>This is demo email send from DeveloperCodez </p>
    </body>
</html>

 5:Setup EmailController

 Now for setting controller create a controller EmailCotroller
code 
Copy

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class EmailController extends Controller
{
    public function index(Request $request){
        {
            $data["email"] = "[email protected]";
            $data["title"] = "DeveloperCodez";
            $data["body"] = "This is demo PDF email";
     
            $files = [
                public_path('attachments/demo.jpeg'),
                public_path('attachments/demo.pdf'),
            ];
     
            Mail::send('mail.sendEmail', $data, function($message)use($data, $files) {
                $message->to($data["email"])
                        ->subject($data["title"]);
     
                foreach ($files as $file){
                    $message->attach($file);
                }            
            });
   
            echo "Mail successfully sent!";
        }      
 }
}

6:Setup routes.php

Building new routes for sending email
code : 
Copy

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmailController;
/*
|--------------------------------------------------------------------------
| 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('send_pdf_mail', [EmailController::class, 'index']);

7:Sending email with attachment

php artisan serve
http://127.0.0.1:8000/send_pdf_mail

 

Tags: attach pdf with email in laravel 10 , send attachment with email in laravel 10 , laravel features , attach attachment with email in laravel 10 , send image with email in larvel 10,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter