Mcfaith
Mcfaith

Reputation: 275

How to send multiple attachment in laravel mail

I created an two separate excel files $recent_signup_file, $ddr_failed_file, and attach on laravel mailer

Mail::send('emails.weeklyreports', ['data' => $data], function($message) use ($recipient, $subject_date, $recent_signup_file, $ddr_failed_file) {
   $message->from('[email protected]', 'Whitebelt.co Support');
   $message->to($recipient)->subject('Weekly Report '.$subject_date);
   $message->addAttachment($recent_signup_file->store("xlsx",false,true)['full']);
   $message->addAttachment($ddr_failed_file->store("xlsx",false,true)['full']);
});

But it gives me duplicate excel file, its this is right or did i miss something, can anyone help me.

How do I it?

Upvotes: 0

Views: 3225

Answers (1)

eResourcesInc
eResourcesInc

Reputation: 1028

Which version of Laravel are you using? You should be using a Mailable if you are using Laravel 5.4+, something that looks like this:

Mail::to($recipient)->send(new WeeklyReport($data));

Within the WeeklyReport mailable, you would attach your files and pass an array of your data to the view like this:

class WeeklyReport extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    public function __construct($data)
    {
         $this->data = $data;
    }

    public function build()
    {
        return $this->view('emails.weeklyreports', $this->data)
                ->from('[email protected]', 'Whitebelt.co Support')
                ->attach($this->data['recent_signup_file']->store("xlsx",false,true)['full'])
                ->attach($this->data['ddr_failed_file']->store("xlsx",false,true)['full']);
    }
}

See the Laravel documentation here.

This all assumes that your functions that you're using to create the excel sheets are actually working as intended. Since we don't see that code here, I can't comment on its functionality. But the above is the correct way to attach files with Laravel 5.4+.

Upvotes: 1

Related Questions