2012-07-07 78 views

回答

3

您可以使用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文件的內容上面一行。

乾杯,祝你好運!

+0

非常感謝我還是很有用的! – Farzamtm 2012-07-07 07:05:53

+0

我對示例代碼做了一些編輯,但我想到了另一種方式來實現它:1.爲電子郵件地址創建一個文件。 2獲取所有用戶的電子郵件數組並將其寫入文件。 3.每小時你碰到cron,閱讀90個電子郵件地址併發送電子郵件給他們,然後從文件中刪除這些電子郵件。 3B。每小時發送90封電子郵件,另外90個電子郵件地址從文件中刪除。 4。當文件爲空時,通過獲取用戶的電子郵件數組並將其寫入文件重新啓動進程。如果你需要一些示例代碼讓我知道,我可以寫這個新版本。 – Stegrex 2012-07-07 07:41:32

+0

再次感謝我也會嘗試 – Farzamtm 2012-07-07 08:59:33

1

PHP本身,是不適合這份工作。你可以寫PHP做實際的發送(和90的限制),但對於調度,需要cron或您的服務器,其配置爲調用定期你的PHP文件放在一個類似的機制。

+0

謝謝我瞭解cron,但是如何使用php代碼將我的電子郵件限制爲每小時90封電子郵件? – Farzamtm 2012-07-07 05:52:56

+0

也許你的問題應該改革(或關閉並重新提出),因爲這是一個完全不同的問題,因爲這是每小時發生的事情。 「如何創建時間表」意味着* cron *你試圖提出的問題是關於不同的東西,目前的標題會誤導。 – 2012-07-07 06:06:43

+0

謝謝我改變問題的標題我覺得這是現在好 – Farzamtm 2012-07-07 06:14:08

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: [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