通过托管服务器使用laravel Task Scheduler自动发送提醒电子邮件

时间:2019-01-22 15:45:35

标签: php laravel task scheduler

我想向收货日期在3天后到期的供应商发送提醒电子邮件。

因此,我创建了一个email:send命令,在其中输入了验证码,然后发送了电子邮件。然后每分钟在文件command/kernel.php中执行命令email:send

email:commande逻辑

$data = $this->mailRep->getCommandes();
$current_date = Carbon::now();
$current_date = $current_date->addDays(3);
$current_date = $current_date->toDateString(); 
$mail_directrice = "souley27souley@gmail.com";

foreach($data as $commande ){
        if($commande->available_date === $current_date){
            $com = array("date" => $commande->available_date,"fournisseur" => $commande->fournisseur);

            Mail::to($commande->responsableEmail)
                ->cc([$commande->responsableEmail,$mail_directrice])
                ->send(new reminderMail($com,'Available'));
        } elseif ($commande->start_send_date === $current_date) {

            $com = array("date" => $commande->start_send_date,"fournisseur" => $commande->fournisseur);

            Mail::to($commande->responsableEmail)
                ->cc([$commande->responsableEmail,$mail_directrice])
                ->send(new reminderMail($com,'start send'));
        } elseif ($commande->arrive_at_port_date === $current_date) {

            $com = array("date" => $commande->arrive_at_port_date,"fournisseur" => $commande->fournisseur);

            Mail::to($commande->fournisseurEmail)
                ->cc([$commande->responsableEmail,$mail_directrice])
                ->send(new reminderMail($com,'Arrive au port'));
        } elseif ($commande->arrive_at_ouaga_date === $current_date) {

            $com = array("date" => $commande->arrive_at_ouaga_date,"fournisseur" => $commande->fournisseur);

            Mail::to($commande->responsableEmail)
                ->cc([$commande->responsableEmail,$mail_directrice])
                ->send(new reminderMail($com,'Arrive a ouaga'));
        } else {
            $this->info("No mail to send !");
        }
    }

reminderEmail类:

class reminderMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public $viewData;
public $type;
public $mails;
public function __construct($datas,$type)
{
    $this->viewData = $datas;
    $this->type = $type;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from("info@groupekyba.com","PPI/Trakng-control")
        ->with(['commandes' =>$this->viewData, 'type' => $this->type])
        ->view('emails.emails');
}
}

command / kernel.php内容

/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    \App\Console\Commands\SendEmails::class
];

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

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this->load(__DIR__.'/Commands');

    require base_path('routes/console.php');
}

当我在Windows命令提示符下手动执行命令时,一切顺利。邮件已发送。现在,我已将项目上传到托管服务商,在那里我创建了一个cron作业,该作业每分钟运行一次订单。但是,cron作业可以正常运行,但是邮件不会发送。

0 个答案:

没有答案