How to configure email sending in Laravel within cron?

1

After the creation of cron (code below) a scan is made in the database behind altered data. I have to send an email with this data. How can I do this ???

cron code:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class envioEmailBIcron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'envioEmailBI:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command Email';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       //Pegar os dados no banco
        $sql = ' select * from payments as P, receipts as R ';
        $sql .= ' where P.created_at < CURRENT_DATE AND P.updated_at < CURRENT_DATE ';
        $sql .= ' AND R.created_at < CURRENT_DATE AND R.updated_at < CURRENT_DATE';

        //pega os dados no banco
        $query = \DB::select($sql);

       // executando as funções de envio de e-mail
       $this->info('Example Cron comando rodando com êxito');
    }
}
    
asked by anonymous 25.10.2017 / 16:42

1 answer

1

To create an email with data coming from the database or any source you feed, do the following:

Type in the command line:

php artisan make:mail ExampleCronSend

This will create an email configuration class with this layout:

<?php

namespace App\Mail;

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

class ExampleCronSend extends Mailable
{
    use Queueable, SerializesModels;

    private $dados;

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

    public function build()
    {
        return $this->view('ExampleEmail')
                    ->to('[email protected]')
                    ->with(['dados' => $this->dados]);
    }
}

Now you will configure a page for the layout of this email, a simple example, create within the folder resources/views a page with the name of ExampleEmail.blade.php :

<!doctype html>
<html lang="{{ config('app.locale') }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel - Resultados</title>
    </head>
    <body>    
    @foreach($dados as $r)
        <p>{{$r->description}}</p>
    @endforeach
    </body>
</html>

This provides a nice layout that can still be improved.

Return to Command file and add this call

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use DB;
use App\Mail\ExampleCronSend;

class ExampleCron extends Command
{

    protected $signature = 'example:cron';
    protected $description = 'Example Cron Minutes';
    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {

        $dados = DB::table('example')->get();

        Mail::send(new ExampleCronSend($dados));

        $this->info('E-mail enviado com sucesso ...');

    }
}

to test type:

php artisan example:cron

If the process has Ok you receive the following message:

  

    
25.10.2017 / 21:20