被送到我用下面的代碼,我希望將電子郵件發送給用戶每星期日:限制發送電子郵件的數量使用PHP
if ($day == 'Sun') {
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, "SELECT email FROM sampledb.registration WHERE choice='weekly' AND mailaboute='Epidemic'");
while ($row = mysqli_fetch_row($result)) {
$to = $row[0];
$subject = "Weekly updates";
$message = "Dear subscriber" . "<br />" . "<br />"; //cwrite nice message
/*while ($j < 5) {
$message.=$array[$j];
$message.="<br>";
}*/
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: [email protected]" . "\r\n";
$retval = mail($to, $subject, $message, $headers);
if ($retval == true) {
echo "<div class = 'emailSent'>";
echo "<center><h1>Email sent</h1></center>";
echo "<center><h2>Please check your email</h2></center>";
echo "</div>";
} else {
echo "Email could not be sent...";
}
}
$conn->close();
}
不過,我面臨的問題是,這個代碼正在每次頁面加載時執行,這意味着如果我在週日打開此頁面10次,我將收到10封電子郵件。如何更改功能,以便在發送1封電子郵件後,代碼不會再次執行。
時間戳值添加到您的數據庫和記錄時,一個成功的電子郵件被髮送。當此腳本再次運行時,請查詢該時間戳並將其與當前時間戳進行比較。或者讓它成爲每個星期天運行的cron作業。或者你必須添加一個查詢字符串參數,以使其發出真正的電子郵件 – Drakes
試圖使用cron作業,謝謝你的建議:) – Awani