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

问题描述 投票:2回答:1

我正在构建一个应用程序,该应用程序应该向用户发送生日提醒电子邮件。 laravel 5.2一切都正常,但是现在我想使用Mailables我不知道在哪里循环用户。这是我的可邮寄课程..

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('[email protected]')
        ->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 "[email protected]"
        Mail::to('[email protected]')->queue(new BirthdayReminder($user));


     }
     */
     $email="[email protected]";
     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>

我的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');
    }
}

我想检查数据库中是否有生日用户,然后将电子邮件发送给一个或多个用户。如果没有,则什么也不做。

laravel laravel-5 laravel-4 laravel-5.2 laravel-5.3
1个回答
3
投票

在命令文件中

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

   foreach($users as $user)
   {
       $email="[email protected]";
       Mail::to($email)->send(new BirthdayReminder($user));
       $i++; 
   }

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

在您的可邮寄课程中

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

 public function build()
 {
  $user = $this->user;
  return $this->from('[email protected]')
              ->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>

希望这会有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.