0

因此,我创建了 3 个命令来根据特定标准发送电子邮件,并将它们设置为每天早上在内核中运行。我每天早上都会在正确的时间收到 emailOutputTo 通知,但应该发出的电子邮件永远不会发出。如果我在环境中手动运行命令,它会起作用,我会收到电子邮件。当我在本地机器上运行它们时,它们会起作用。在搬到 Vapor 之前,他们在我的旧服务器上工作。无论如何知道可能是什么问题?

这是设置为运行的作业的示例:

我的命令:

    class EmailFreeTrialCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'emails:EmailFreeTrialCode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Email users a free trial promo code if they that have not upgraded after 7 days';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $startDate = Carbon::now()->subDays(7)->startOfDay();
        $endDate = Carbon::now()->subDays(7)->endOfDay();
        $now = Carbon::now()->endOfDay();
        $users = User::whereBetween('created_at', [$startDate, $endDate])->get();

        if (!empty($users) ) {
            foreach ( $users as $user ) {

                $created = Carbon::parse( $user->created_at )->startOfDay();
                $diff    = $created->diffInDays( $now );

                if ( $diff === 7 && !$user->subscription) {
                    //$page = $user->pages()->firstWhere( 'user_id', $user->id );

                    if ($user->email_subscription) {
                        $userData = ( [
                            'username' => $user->username,
                            'userID'   => $user->id,
                        ] );

                        $details = ( [
                            "data" => $userData,
                            "userEmail" => $user->email
                        ]);
                 
                        JobFreeTrialEmail::dispatch($details);
                    }
                }
            }
        }

        return 0;
    }
}

我的工作:

    class JobFreeTrialEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $details;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new MailFreeTrial($this->details["data"]);
        Mail::to($this->details["userEmail"])->send($email);

    }
}

核心:

    class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        EmailInactiveUsers::class,
        EmailSocialShare::class,
        EmailFreeTrialCode::class,
        TestAddDBRecord::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        /*$schedule->command('test:TestAddDBRecord')
                 ->timezone('America/New_York')
                 ->everyMinute()
                 ->emailOutputTo('mcirami@gmail.com');*/

        $schedule->command('emails:EmailInactiveUsers')
                 ->timezone('America/New_York')
                 ->dailyAt('7:00')
                 ->emailOutputTo('mcirami@gmail.com');

        $schedule->command('emails:EmailSocialShare')
                 ->timezone('America/New_York')
                 ->dailyAt('7:15')
                 ->emailOutputTo('mcirami@gmail.com');

        $schedule->command('emails:EmailFreeTrialCode')
                 ->timezone('America/New_York')
                 ->dailyAt('7:30')
                 ->emailOutputTo('mcirami@gmail.com');
    }

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

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

任何帮助都会很棒!先感谢您!

更新

所以我想出了我自己的问题。还不完全确定为什么,但我从内核中删除了 emailOutputTo ,现在一切正常......在这一点上对我来说真的没有意义,因为它在其他任何地方都以这种方式工作,只是在 Vapor 上没有。希望这对将来的某人有所帮助。

4

0 回答 0