2017-02-03 181 views
1

我正在構建一個應用程序,它假設發送生日提醒電子郵件給用戶。一切工作正常與laravel 5.2,但現在我想使用Mailables我不知道在哪裏循環用戶。這是我可郵寄類..Laravel發送生日提醒電子郵件使用郵件

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

我想檢查數據庫中是否存在是具有生日的任何用戶,然後將該電子郵件發送給一個或多個用戶。如果沒有,那麼什麼也不做。

回答

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

} 

在你可郵寄類

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> 

希望這將有助於:)

+0

非常感謝您的幫助。一切似乎工作正常,直到我去我的Gmail,我沒有電子郵件。 Cron作業返回正在運行的計劃命令:'/ opt/php70/bin/php''artisan'電子郵件:生日>'/ dev/null'2>&1&。所以,不知何故,該命令在不發送任何電子郵件的情況下執行。 – bobin56

+0

你有沒有檢查你是否有任何用戶。你可以執行'Log :: info($ user);'在循環中處理方法並檢查laravel.log文件。我想沒有用戶,否則它應該發送。 – Vikash

+0

只需從項目的根目錄運行'php artisan email:birthday',並檢查日誌中的打印內容並檢查命令提示符即將發出的信息消息。 ?。請告訴我 – Vikash