简体   繁体   English

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

[英]How to limit sending email 90 per hour using php code?

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

You can use PHP to do it in this very hackish way: 您可以使用PHP以这种非常怪异的方式进行操作:

Create a blank file called count_offset.txt This will be a file that tracks the offset of the chunked set of 90 users. 创建一个名为count_offset.txt的空白文件。该文件将跟踪90个用户的分块集合的偏移量。

Create another blank file called count_emails.txt This will be a file that tracks the number of emails sent in the particular hour. 创建另一个名为count_emails.txt的空白文件。这将是一个文件,该文件跟踪特定时间发送的电子邮件数量。

The PHP script that runs the email function (thru cron) can then open up this first text file to check which chunked set has been sent, and sends to the next set of users. 然后,运行电子邮件功能(通过cron)的PHP脚本可以打开此第一个文本文件,以检查已发送了哪个分块集,并发送给下一组用户。 It can check the second file for the 90 email limit. 它可以检查第二个文件的电子邮件限制为90。

For example: 例如:

$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);

Afterwards, write another cron that clears the count_emails.txt file every hour (or turns the contents to 0). 然后,编写另一个cron,每小时清除一次count_emails.txt文件(或将其内容更改为0)。 This other cron can run another PHP script or can be a Bash command if you prefer. 另一个cron可以运行另一个PHP脚本,也可以根据需要作为Bash命令。

Here would be the cron if you wanted to do it using Bash commands: 如果要使用Bash命令执行此操作,则为cron:

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

The above line when added to cron, with use cat to clear the contents of the count_emails.txt file. 将以上行添加到cron时,使用cat清除count_emails.txt文件的内容。

Cheers, and good luck! 干杯,祝你好运!

PHP, by itself, is not adequate for this job. 仅靠PHP本身不足以完成这项工作。 You can write PHP to do that actual sending (and the limit of 90), but for scheduling, you need cron or a similar mechanism on your server, which is configured to call your PHP file on a regular basis. 您可以编写PHP来进行实际发送(限制为90),但是要进行调度,您需要在服务器上使用cron或类似的机制,该机制已配置为定期调用PHP文件。

There is an in depth tutorial on this subject at https://a1websitepro.com/sending-emails-every-hour-server-limit-php/ Here is the code that you would run using cron. 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();
?>

Here is the cron code that you would insert in your cPanel after you uploaded all the scripts from the tutorial. 这是您上传了本教程中的所有脚本后将在cPanel中插入的cron代码。 /usr/bin/php -q /home/cpanelusername/public_html/sendnewsletter.php / usr / bin / php -q /home/cpanelusername/public_html/sendnewsletter.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM