我想限制發送電子郵件90每小時使用php代碼的電子郵件如何限制每小時使用php代碼發送電子郵件90?
回答
您可以使用PHP來做到這一點在這個非常hackish的方式:
創建一個名爲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);
然後,再寫的cron掃清每隔一小時count_emails.txt文件(或打開的內容爲0)。這個其他的cron可以運行另一個PHP腳本,或者如果你願意的話可以是Bash命令。
這裏將是cron的,如果你想要做它使用bash命令:
0 * * * * cat /dev/null > count_emails.txt
時加入的cron,在使用貓以清除count_emails.txt文件的內容上面一行。
乾杯,祝你好運!
非常感謝我還是很有用的! – Farzamtm 2012-07-07 07:05:53
我對示例代碼做了一些編輯,但我想到了另一種方式來實現它:1.爲電子郵件地址創建一個文件。 2獲取所有用戶的電子郵件數組並將其寫入文件。 3.每小時你碰到cron,閱讀90個電子郵件地址併發送電子郵件給他們,然後從文件中刪除這些電子郵件。 3B。每小時發送90封電子郵件,另外90個電子郵件地址從文件中刪除。 4。當文件爲空時,通過獲取用戶的電子郵件數組並將其寫入文件重新啓動進程。如果你需要一些示例代碼讓我知道,我可以寫這個新版本。 – Stegrex 2012-07-07 07:41:32
再次感謝我也會嘗試 – Farzamtm 2012-07-07 08:59:33
PHP本身,是不適合這份工作。你可以寫PHP做實際的發送(和90的限制),但對於調度,需要cron或您的服務器,其配置爲調用定期你的PHP文件放在一個類似的機制。
關於此主題的深入教程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: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\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
- 1. 如何使用mailgun發送電子郵件php代碼
- 2. 限制發送電子郵件的數量使用PHP
- 3. 發送電子郵件使用PHP代碼
- 4. 如何從自定義PHP代碼發送電子郵件?
- 5. 如何使用Android代碼發送電子郵件
- 6. 用PHP郵件發送電子郵件()
- 7. PHP發送電子郵件多次發送電子郵件
- 8. 電子郵件發送代碼在Ubuntu
- 9. 代碼發送電子郵件
- 10. 使用php發送電子郵件
- 11. 使用PHP發送HTML電子郵件
- 12. PHP發送電子郵件
- 13. PHP發送電子郵件
- 14. PHP發送電子郵件
- 15. ColdFusion限制爲每天發送5封電子郵件
- 16. 如何使用php curl發送來自yahoo電子郵件的電子郵件
- 17. 如何強制Mailgun通過API(php)使用base64編碼發送電子郵件?
- 18. PHP郵件不發送電子郵件
- 19. 限制每小時電子郵件數joomla
- 20. 限制使用SSRS通過電子郵件發送訂閱
- 21. 每1小時發送一次SQL服務器電子郵件
- 22. 如何在使用PHPMailer作爲電子郵件發送時保留HTML代碼
- 23. 每天通過電子郵件發送PHP警告或如何?
- 24. 如何發送沒有速度限制的電子郵件?
- 25. 如何使用PHP發送帶附件的電子郵件?
- 26. 如何使用PHP將xml文件發送到電子郵件?
- 27. 用於使用vba代碼發送電子郵件的按鈕
- 28. 使用apache common發送電子郵件使用「代表」電子郵件
- 29. PHP代碼發送郵件
- 30. 嘗試使用sendmail發送/發送html電子郵件,但顯示電子郵件的源代碼
你能更準確? – damned 2012-07-07 06:20:12
我有把它放在cron來發送簡訊對我的通訊,但我的服務器,只是讓我送90,我有90多個用戶怎樣才能限制每小時發送郵件? – Farzamtm 2012-07-07 06:23:41