我有一系列的4个电子邮件地址。每个星期二我都需要发送一封电子邮件,所以我使用CRON运行一个php脚本。该脚本包含一系列电子邮件地址。第一周我发送电子邮件到数组,第二周我向array1发送电子邮件,第三周我向array2发送电子邮件,第4周我向array3发送电子邮件。然后我重复一遍,所以第五周我发送一封电子邮件到数组,等等,直到无限大。
我如何计算date()来确定脚本运行时发送哪封电子邮件?我唯一能想到的就是在开始日期之后的10年内填写一个日期查找数组,然后进行查找,但这看起来效率很低。
如果明年,我可以在数组中添加另一个用户,而不会扰乱到目前为止的订单。
发布于 2017-02-15 22:43:34
如果我的理解是正确的,你没有问题,什么时候发送,而是给谁,遵守一定的订单。话虽如此,你不是在找错误的解决方案吗?
为什么不简单地将数组的索引号存储在某个地方(例如,在一个普通的文件中)?并在执行cron时(每个星期二)自动更新该文件?这允许您在不扰乱订单的情况下添加另一个用户。
参见下面的示例代码:
// The list with users
$userList = [
0 => "you@domain.tld",
1 => "john.doe@domain.tld"
];
// Determine the user who received last email
$lastUsedIndex = 1; // e.g. extract this input from a file
// Determine the last user in the list
$maxUserIndex = max(0, count($userList) - 1);
// Determine who is next in line to receive the email
$newIndex = (++$lastUsedIndex <= $maxUserIndex ? $lastUsedIndex : 0);
$reciever = $userList[$newIndex];
// Send the email
// Update the source containing the last receiver (which is the value of $newIndex)发布于 2017-02-15 22:23:56
您可以使用一些模块化的数学操作来实现一个简单的循环系统。
此外,您还可以使用date("W")获取周数。
假设您只有3个数组:
<?php
$emails = [
['email1@corp.com', 'email2@corp.com', 'email3@corp.com'],
['email1@anothercorp.com', 'email2@anothercorp.com', 'email3@anothercorp.com'],
['email1@athirdcorp.com', 'email2@athirdcorp.com', 'email3@athirdcorp.com'],
];
$roundRobinSize = count($emails);
$thisWeekEmailsList = $emails[(intval(date("W")) - 1) % $roundRobinSize];
someFunctionForSendingMail($thisWeekEmailsList, 'Subject', 'Message');如果最终添加了另一个列表,这段代码可能会得到错误的数组。这是因为它根据周数实现了一个简单的循环。
https://stackoverflow.com/questions/42261020
复制相似问题