How do I send mail attachment in laravel

Published January 17, 2022

In this post, we'll see how to send an email attachment in Laravel. Laravel offers a mail class you can use to send emails. You only need to configure SMTP driver details such as Sendmail, Amazon SES, Postmark, and mailgun in the .env file to send emails.

Sending a mail attachment in laravel is pretty easy, you just need to follow the following simple steps:

Step 1: First, install the Laravel application using the following composer command:

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

PHP Laravel Send Email Attachement

 

Our Laravel application has been successfully created and Laravel files installed in our attach_Mail directory

PHP Laravel Send Email Attachement 2

Now let's go ahead and configure our SMTP in the  .env file.

 

Step 2: Open your  .env file and configure  MAIL_DRIVER, MAIL_HOST, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD, MAIL_ENCRYPTION

 

PHP Laravel Send Email Attachement 3

 

Once our, SMTP is successfully configured, let's install our PDF library in our Laravel application to enable us to attach a PDF file

Step 3: To install the PDF library, open your Composer command prompt and run the

 

composer require niklasravnsborg/laravel-pdf.

 

PHP Laravel Send Email Attachement 4

This command will install the niklasravnsborg/laravel-pdf package that will allow our application to generate pdf.

To start using Laravel, you will need to add the Façade and Service Provider to the config/app.php. Open your config/app.php  and paste the following code:

 

Now, run the code below to publish  the package  config  file in the directory using  the command below

 

php artisan vendor: publish

 

Step 4: In this step, we are going to add the email route. To do this, open your  web.php  and add the following code below

use App\Http\Controllers\SendEmailController;

Route::get('send-email-pdf', [SendEmailController::class, 'index']);

Step5: Now, lets create test.blade.php view file  in the resources/views/ and paste the code below

<!DOCTYPE html>

<html>

<head>

 <title>Laravel 8 Send Email Example</title>

</head>

<body>

 <h1>How to send mail in Laravel</h1>

 <p>Laravel 8 send email example</p> 

</body>

</html>

 

 

Step 6: Now we need to send the email controller. To create one, open your composer command prompt and run the command below

php artisan make:controller SendEmailController

 

A SendEmailController.php file will be created in your app/Http/Controllers directory

 

PHP Laravel Send Email Attachement 5

 

Open your SendEmailController.php and paste the code below

<?php

 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use Mail; 

use PDF; 

class SendEmailController extends Controller

{    

public function sendmail(Request $request){

                        $data["email"]=$request->get("email");

     $data["client_name"]=$request->get("client_name");

        $data["subject"]=$request->get("subject"); 

        $pdf = PDF::loadView('test', $data); 

        try{

           Mail::send('mails.mail', $data, function($message)use($data,$pdf) {

            $message->to($data["email"], $data["client_name"])

            ->subject($data["subject"])

            ->attachData($pdf->output(), "invoice.pdf");

            });

        }catch(JWTException $exception){

            $this->serverstatuscode = "0";

            $this->serverstatusdes = $exception->getMessage();

      }

        if (Mail::failures()) {

             $this->statusdesc  =   "Error sending mail";

             $this->statuscode  =   "0"; 

        }else{

           $this->statusdesc  =   "Message sent Succesfully";

           $this->statuscode  =   "1";

       }

        return response()->json(compact('this'));

 }

}

Step 7: That’s it, now run the php artisan serve to open our application in the browser

PHP Laravel Send Email Attachement 6

 

Now you are able to send attachment with mail using laravel

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

792 Views