2017-02-03 4 views
1

私は、誕生日のリマインダーメールをユーザーに送信すると思われるアプリケーションを構築しています。すべてがlaravel 5.2で正常に動作していましたが、今は私がMailablesを使用したいと思っています。ユーザーをループする場所がわかりません。ここに私のMailableクラス..ですここで Laravel Mailabilityを使用してBirthday Reminderのメールを送信する

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

データベースがあるかどうかチェックしたい誕生日を持つユーザーであれば、1人以上のユーザーに電子メールを送信します。 Noneの場合は、何もしないでください。ご使用のブレード・ファイル(birthday.blade.php)

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

ホープの

答えて

2
あなたのコマンド・ファイルに

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

} 
あなたMailableクラスで

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

これは役立ちます:)

+0

ありがとうございました。私は私のGmailに行くまで私はメールを持っていないすべてがうまくいくようだ。 Cron Jobは次の実行予定コマンドを返します。 '/ opt/php70/bin/php' 'artisan' email:birthday> '/ dev/null' 2>&1&。だから、どういうわけか、電子メールを送信せずにコマンドが実行されます。 – bobin56

+0

あなたはどのユーザーも持っていますか? handleメソッドのループ内で 'Log :: info($ user);'を実行し、laravel.logファイルをチェックすることができます。私はそれ以外の場合は送信する必要がないユーザーはないと思います。 – Vikash

+0

プロジェクトのルートディレクトリから 'php artisan email:birthday'を実行して、何がログに記録されているかを確認し、コマンドプロンプトで情報メッセージとして何が来るのかを確認してください。 ?教えてください – Vikash

関連する問題