Easily Share Files via Email in Laravel - Try It Now!

How to send file url as attachment in Laravel Mail

Send File URL as attachment in Mail - Laravel

In this article we will learn how to send file url as an attachment in Laravel Mail . We will take a a example where we will generate a PDF using Laravel Excel package and store it in our project directory and then we will send the file url as an attachment in our Mail .

Table of Content :

  • Setup Your Mail Configuration
  • Create your Route
  • Generate PDF using Laravel Excel
  • Write query on Export file
  • Sent Mail

Step 1  - Setup Mail Configuration :

The very first step is to set up our mail configuration , first setup your mail configuration on your .env file as per your mail credentials as i am using mailtrap for sending emails , Here is how you can setup your mail configurations shown below .

Send Markdown Mail in Laravel 7

.env :

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=34d7af640c9818
MAIL_PASSWORD=d07708a6993e38
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=studywithkishan@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 2 - Setup Routes :

Now setup your routes .

Route::get('/sendattachemntmail','CheckController@sendAttachmentMail')->name('send-attachment-mail');

Step 3 - Generate PDF :

Now let's generate a PDF file using Laravel Excel package , or you can use any other package for generating files , the main motive is to get the file url .

CheckController.php :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Mail;
use App\Exports\TestReportExport;
use App\Mail\TestReportExportMail;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Storage;

class CheckController extends Controller
{

    public function sendAttachmentMail()
    {
        $format = 'pdf';
        $email = 'demo@gmail.com';
                    $file_name = 'TestReport' . "_" . time() . '.'.$format;
                    $storage_path = 'public/TestReport';
                    $filePath = $storage_path . '/' . $file_name;
                    $exl = Excel::store(new TestReportExport(), $filePath);
                    if($exl)
                    {
                     $fileurl = Storage::path('public/TestReport/'.$file_name);
                
                    }
                    
                    $mail = Mail::to($email)->send(new TestReportExportMail($fileurl));
    }
}

Step 4 - Query on Export file :

If you know about Laravel Excel , then we have to create an export file to write our query for getting data from database and to convert it to different format of files .

To create a export file use the following command .

php artisan make:export TestReportExport --model=User

Exports/TestReprotExport.php :

Write your database query on your export file to make it PDF .

<?php

namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class TestReportExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }
}

Step 5 - Configuring Mail File :

On your controller we are sending the file url variable to the mail file , now we have to access this variable inside the mail file and send it as attachment in the Mail .

app/Mail/TestReportExportMail.php :

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestReportExportMail extends Mailable
{
    use Queueable, SerializesModels;
    protected $fileurl;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($fileurl)
    {
        $this->fileurl = $fileurl;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('Email.TestReport')->from('studywithkishan@gmail.com')->subject('Testing Attachment')->attach($this->fileurl);
    }
}

That's it , now simply hit your route and check your mail and attachment .

Output :

Send File URL as attachment in Mail - Laravel

Send File URL as attachment in Mail - Laravel










Previous Post Next Post

Contact Form