'how to send a scheduled report via email to user in laravel
i am trying to send a schedule report (containing charts and graphs) every week to the user via email using Laravel
so , what is the best way to do that
thank you
Solution 1:[1]
hi you can make a command and register it in the app/Console/Kernel.php
1) app/Console/Commands/Reporter.php
use Mail;
use Illuminate\Console\Command;
class Reporter extends Command {
protected $name = 'Reporter';
protected $description = 'This is a test.';
public function __construct()
{
parent::__construct();
}
public function fire()
{
Mail::send('emails.pdf', function($message) use ($path) {
$message->to('[email protected]', 'testuser')->subject
('pdf Report '. Carbon::now()->format('m-d-Y'));
$message->attach($path);
$message->from('[email protected]','test-tech');
});
Storage::disk('pdf')->delete(Storage::disk('pdf')->allFiles());
$this->info('Mail has fired.');
}
}
2.app/Console/Kernel.php
protected $commands = [
'App\Console\Commands\Inspire',
'App\Console\Commands\Reporter',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('Reporter')->weekly();
}
you can create pdf with mpdf https://github.com/mccarlosen/laravel-mpdf
use PDF;
class ReportController extends Controller {
public function generate_pdf()
{
$data = [
'foo' => 'bar'
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('document.pdf');
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MajidGh |
