befreeforlife
befreeforlife

Reputation: 511

Send pdf on mail using laravel-dompdf without saving on server

I would like to send pdf on mail using laravel-dompdf without saving on server

I have next code:

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts));
Mail::send('admin.costs.test', $pdf, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
    $message->attachData($pdf, 'invoice.pdf');
});
}

But I get next error:

Type error: Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, object given, called in /var/www/html/miltonia-update/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 221

How to fix it?

Upvotes: 1

Views: 2059

Answers (3)

befreeforlife
befreeforlife

Reputation: 511

Thank you all! I solve this problem next way, using ideas above:

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts))->stream();

Mail::send('admin.costs.test', ['pdf'=>$pdf], function($message) use ($pdf)
{
   $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
   $message->attachData($pdf, 'invoice.pdf');
});

Upvotes: 0

Sohel0415
Sohel0415

Reputation: 9853

Mail accepts second argument as an array of data you wish to pass to the view. Wrap your $pdf as an array data.

Mail::send('admin.costs.test', ['pdf'=>$pdf], function($message) use ($pdf)
{
   $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
   $message->attachData($pdf, 'invoice.pdf');
});

Upvotes: 4

Sapnesh Naik
Sapnesh Naik

Reputation: 11646

As the error suggests, you need to pass an array to Mail::send function but you are just passing $pdf which is an object and not an array.

This should work:

$data = [];

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts));

$data['pdf'] = $pdf;

Mail::send('admin.costs.test', $data, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
    $message->attachData($data['pdf'], 'invoice.pdf');
});
}

Upvotes: 1

Related Questions