laravel 如何安排邮件在每周四下午1点发送

zujrkrfu  于 2023-06-24  发布在  其他
关注(0)|答案(3)|浏览(117)

bounty还有3天到期。回答此问题可获得+100声望奖励。user022yufjb希望吸引更多注意力这个问题:如果您可以通过运行我的函数getAllLostWill()

class  SendLostwillEmailsController extends Controller
{
    public function runCommand()
    {
        return $this->getAllLostWill();
    }

    protected function getAllLostWill()
    {
        try {
            $status = false;
            $newMailSetting = new ChangeMailSettings();

            $getToday = Carbon::now()->format('Y-m-d');
            $getLast7day = Carbon::now()->subDays(7)->format('Y-m-d');
            $lostwill = LostWill::select('id', 'lost_first_name', 'lost_last_name', 'lost_dod', 'user_id')->whereBetween('created_at', [$getLast7day, $getToday])->get();

            if (!$lostwill->isEmpty()) {
                $listOfEmails = $this->getEmailList($lostwill->pluck('user_id'));

                $newMailSetting->sendMail();
                $listChunkEmail = array_chunk($listOfEmails['emails'], 550, false);
                $i = 0;
                $countSentMail = 0;
                while ($i < count($listChunkEmail)) {
                    Notification::route('mail', 'info@lostwillregister.com.au')->notify(new SendLostwillMarketingEmail($listChunkEmail[$i], $lostwill));
                    $countSentMail = ($countSentMail + count($listChunkEmail[$i]));
                    $i++;
                }
                //$listChunkEmail[$i]
                Log::channel('marketing_mail')->info('total email sent:' . $countSentMail);
                $newMailSetting->setDefault();
                $status = true;
            } else {
                $status = false;
                Log::channel('marketing_mail')->info("lost will not registers for yesterday's date");
            }

            return $status;
        } catch (Exception $e) {
            return false;
            Log::channel('marketing_mail')->info($e->getMessage());
        }
    }

来回答我的问题,即如何在每周下午1点安排邮件

我想在crontab中运行getAllLostWill()。此功能获取过去7天的所有丢失的遗嘱并发送邮件。SendLostwillEmailsController类的文件路径是lost-will-register\app\Http\Controllers当前,在我的crontab中,我有这个:

GNU nano 4.8                                                                                /tmp/crontab.qrUExv/crontab                                                                                          
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
25 10 * * * cd /var/www/lost-will-register && php artisan marketing:startCampaign >> /dev/null 2>&1
00 16 * * * cd /var/www/lost-will-register && php artisan delete:files >> /dev/null 2>&1
25 10 * * * cd /var/www/australian-will-register && php artisan reminder:annualwillregistration >> /dev/null 2>&1
00 00 * * 4 cd /var/www/lost-will-register-nz && php artisan marketing:startCampaign >> /dev/null 2>&1
00 16 * * * cd /var/www/lost-will-register-nz && php artisan delete:files >> /dev/null 2>&1

这是运行函数getAllLostWill()00 01 1 1 * cd /var/www/lost-will-register && php artisan reminder:getAllLostWill >> /dev/null 2>&1的正确命令吗?
我是新来的crontab,所以我不知道很多;但是,我假设您需要将命令放在第一位,如time 00 00 ** **,然后是我上面提到的文件的路径。我不确定如何将所有这些组合在一起,以便在每周四下午1点发送一封邮件,因此通过运行getAllLostWill()函数,该函数发送邮件

nfzehxib

nfzehxib1#

要使用crontab计划每周四下午1点执行getAllLostWill()函数,可以使用以下条目更新crontab文件。

0 13 * * 4 cd /var/www/lost-will-register && php artisan reminder:getAllLostWill >> /dev/null 2>&1
  • 0:指定分钟(0表示小时的开始)。使用00是有效的,并且表示相同的事情。
  • 13:指定24小时格式的小时(13表示下午1点)。
    • :表示每月的任何一天都可以接受。
    • :表示任何月份均可接受。
  • 4:指定星期几(4表示星期四)。
csbfibhn

csbfibhn2#

这里有两个组件:学习cron的语法,有很多在线资源,并构建PHP/Laravel代码。
出于理智的考虑,我建议使用一个自包含的PHP页面来处理所有代码,并设置cron来调用该页面(而不是在crontab中使用PHP代码)。

25 10 * * * php -d safe_mode=off -f /var/www/lost-will-register/my-schedule.php > /dev/null

除了将代码与cron分离之外,这还可以让您更轻松地维护和故障排除。理想情况下,出于安全考虑,您应该将此类脚本存储在webroot之外的自己的文件夹中。还值得一提的是,您可以为不同的Linux用户设置cron(例如www-data用于Web内容),并且需要在目标PHP脚本上设置正确的权限。

pcww981p

pcww981p3#

Laravel推荐的调度命令作业的方法是使用它们的Task Scheduling功能。
您不应该在服务器crontab中手动定义所有命令行。相反,你应该只添加一个作业来运行Laravel调度器:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

关于你的情况,你应该创建一个命令,并在该命令中移动你的代码,而不是使用一个控制器。
然后,您可以在app/Console/Kernel.php文件中定义命令调度:

protected function schedule(Schedule $schedule): void
{
    $schedule->command('your:command-name')->weeklyOn(4, '13:00');

}

相关问题