vendredi 3 février 2017

Laravel Send Birthday Reminder email Using Mailables

I am building an application which suppose to send birthday reminder email to users . Everything was working fine with laravel 5.2 , but now i want to use Mailables i don't know where to loop users . Here is my Mailable Class ..

namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BirthdayReminder extends Mailable
{
    use Queueable, SerializesModels;
    public $user;
    public function __construct()
    {
         $this->User = $user;
    }
    public function build()
    {
        $users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get(); 
         foreach($users as $user) {    
          return $this->from('info@bandali.co.tz')
        ->view('emails.birthday')
        ->with(['user' => $user]);
    }

    }
}

Here is my command to send birthday

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\User;
use App\Mail\BirthdayReminder;
use Mail;
use App;
class SendBirthdayReminderEmail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email:birthday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Email users a birthday Reminder message';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

   /*  $users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get();   
    foreach($users as $user) {   
         // send an email to "batman@batcave.io"
        Mail::to('robertrutenge@gmail.com')->queue(new BirthdayReminder($user));


     }
     */
     $email="xxxxxxxx@gmail.com";
     Mail::to($email)->send(new BirthdayReminder());

    $this->info('Birthday messages sent successfully!');

    }
}

And here is my view

<p>Hello Admin,</p>
<p>Today is  Birthday . Please take time and  wish  a happy birthday!</p>

My Kernel.php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //

      Commands\SendBirthdayReminderEmail::class,

    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
         $schedule->command('email:birthday')->everyMinute()->timezone('Africa/Dar_es_Salaam');
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

I want to check in the database if there is any user with birthday , then send the email to one or more users . If None , then do nothing .



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ka4llJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire