Laravel使用Mailables发送生日提醒电子邮件

时间:2017-02-03 15:59:36

标签: laravel laravel-5 laravel-4 laravel-5.2 laravel-5.3

我正在构建一个应用程序,它假设向用户发送生日提醒电子邮件。 laravel 5.2一切正常,但现在我想使用Mailables,我不知道在哪里循环用户。这是我的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]);
    }

    }
}

这是我发送生日的命令

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!');

    }
}

这是我的观点

<p>Hello Admin,</p>
<p>Today is {{$user['first_name']}} 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');
    }
}

如果有任何生日用户,我想检查数据库,然后将电子邮件发送给一个或多个用户。如果没有,那么什么都不做。

1 个答案:

答案 0 :(得分:2)

在您的命令文件中

public function handle()
{ 
   $i = 0
   $users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get();  

   foreach($users as $user)
   {
       $email="xxxxxxxx@gmail.com";
       Mail::to($email)->send(new BirthdayReminder($user));
       $i++; 
   }
}

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

}

在你的Mailable课程中

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

 public function build()
 {
  $user = $this->user;
  return $this->from('info@bandali.co.tz')
              ->view('emails.birthday',compact('user'));
 }

在您的刀片文件(birthday.blade.php)

<p>Hello Admin,</p>
<p>Today is {{ $user->first_name }} Birthday . Please take time and  
wish  a happy birthday!</p>

希望这会有所帮助:)