gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:55:25 25 4
gpt4 key购买 nike

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', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4adaaa2ab84a8abb7b0b3ada8a8b6a1a3adb7b0a1b6eaa7aba9eaa5b1" rel="noreferrer noopener nofollow">[email protected]</a>')->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());
}
}

我想在 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 Remember:getAllLostWill >>/dev/null 2>&1

我是crontab新手,所以了解不多;但是,我假设您需要首先输入命令,如时间 00 00 ** **,然后是我上面提到的文件的路径。我不知道如何将它们组合在一起以每周四下午 1 点发送邮件,因此通过运行 getAllLostWill() 函数来发送邮件

最佳答案

Laravel 推荐的安排命令作业的方法是使用它们的 Task Scheduling功能。

您不应在服务器 crontab 中手动定义所有命令行。相反,您应该只添加一个作业来运行 Laravel scheduler :

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

关于你的情况,你应该 create a command并将代码移动到该命令中,而不是使用 Controller 。

然后,您可以定义您的 command schedules在您的 app/Console/Kernel.php 文件中:

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

}

关于php - 如何安排邮件在每周四下午 1 点发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76169224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com