如何使用PHP代码限制每小时发送电子邮件90?

时间:2012-07-07 05:47:40

标签: php email sendmail

我想限制使用php代码每小时发送90封电子邮件

3 个答案:

答案 0 :(得分:3)

您可以使用PHP以这种非常黑客的方式执行此操作:

创建一个名为count_offset.txt的空白文件 这将是一个跟踪90个用户的分组集的偏移量的文件。

创建另一个名为count_emails.txt的空白文件 这将是一个跟踪特定小时内发送的电子邮件数量的文件。

运行电子邮件功能(通过cron)的PHP脚本然后可以打开第一个文本文件以检查已经发送了哪个分块集,并发送给下一组用户。它可以检查第二个文件是否有90个电子邮件限制。

例如:

$userCount = getNumberOfUsers(); // Whatever query you may have that counts how many total users there are.

$numChunks = ceil($userCount/90); // How many different groups to send the email.

$chunkFile = fopen('chunk_offset.txt', 'r+'); // Loads the file as resource.
$currentChunk = fread($chunkFile, filesize('chunk_offset.txt')); // Load the contents of chunk_offset.txt into variable.
$currentChunk = ($currentCount == '' ? 0 : (int)$currentChunk); // Load 0 if contents of file blank.

$countFile = fopen('count_emails.txt', 'r+'); // Loads the file as a resource in variable $countFile.
$currentCount = fread($countFile, filesize('count_emails.txt')); // Load the content of the file into variable $currentCount.
$currentCount = ($currentCount == '' ? 0 : (int)$currentCount); // If the value of $currentCount is blank, then sets it to integer 0, otherwise sets the variable as the integer value of file contents.

if ($currentCount <= 90) // Test the variable to see if it's under the limit. If it's under, send the email.
{
    foreach ($whateverUserListYouHave as $integerKey => $emailAddress) // Iterating through whatever array of users you have.
    // Hopefully index number => email, but the index number is important.
    // Also, consistent ordering of the list of users is important.
    // Remember, you can always create your own counter.
    {
        // The magic:
        // You're testing for set of people who fall within the current chunk.
        if ($integerKey >= ($currentChunk * 90) && $integerKey < ($currentChunk * 90 + 90))
        {
            send_email($emailAddress); // Whatever arbitrary email function you have here.
        }
    }
}

$currentCount++; // Iterate up the count.
fwrite($countFile, $currentCount); // Write the new count into the file.

if ($currentChunk == $numChunks) // If the current chunk number hits the total number of groups of 90, then reset the file to blank...
{
    $currentChunk = '';
}
else if ($currentChunk < $numChunks) // ... Otherwise iterate up and let it hit the next chunk on the next hour.
{
    $currentChunk++; // Iterate up the chunk.
}
fwrite($chunkFile, $currentChunk);

然后,编写另一个每小时清除count_emails.txt文件的cron(或将内容转换为0)。这个其他的cron可以运行另一个PHP脚本,如果你愿意,也可以是Bash命令。

如果您想使用Bash命令执行此操作,那么这将是cron:

0 * * * * cat /dev/null > count_emails.txt

将以上行添加到cron中,使用cat清除count_emails.txt文件的内容。

干杯,祝你好运!

答案 1 :(得分:1)

PHP本身并不适合这项工作。您可以编写PHP来执行实际发送(并且限制为90),但是对于调度,您需要cron或服务器上的类似机制,它被配置为定期调用您的PHP文件。

答案 2 :(得分:0)

https://a1websitepro.com/sending-emails-every-hour-server-limit-php/有关此主题的深入教程  这是您将使用cron运行的代码。

    <?php
include('config.php');
$result = $con->query("SELECT * FROM newsletter     ORDER BY id DESC LIMIT 1") ;
while ($row = $result->fetch_assoc()) {
 $newid=$row['id'];
$thtetitle=$row['title'];
$thecontent=$row['content'];
echo '<hr/>';
}
$resultt = $con->query("SELECT * FROM users WHERE      emailed <> $newid ORDER BY id ASC LIMIT 50") ;
while ($rowt = $resultt->fetch_assoc()) {
$userid=$rowt['id'];
$email= $rowt['email'];
echo '<hr/>';
$to = $email;
$subject = $thetitle;
$message = $thecontent;
$headers = 'From: you@yourwebsite.com' . "\r\n" .
'Reply-To: noreply@yourwebsite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysqli_query($con,"UPDATE users SET     emailed='$newid'
WHERE id='$userid' ");
}
$con->close();
?>

以下是从教程上传完所有脚本后将在cPanel中插入的cron代码。     / usr / bin / php -q /home/cpanelusername/public_html/sendnewsletter.php