我有以下代码,我希望每周日向用户发送电子邮件:
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: noreply@beawarebeprepared.com" . "\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封电子邮件后,代码不应再次执行。
答案 0 :(得分:1)
如果您使用我的cron作业建议,这是一个脚本,每周日上午9点将调用您的PHP页面。它基本上就像你在浏览器中输入它一样取你的PHP,用stdout
标志将输出发送到-o
,但是确实将输出和任何错误发送到了一个下沉洞,所以你不要分别使用>/dev/null
和2>&1
查看。
* 9 * * 0 wget -O - http://yoursite.com/emailer.php?param=val >/dev/null 2>&1
以下是包含许多不同选项的cron脚本的很好示例,例如,提供密码:http://www.labnol.org/software/wget-command-examples/28750/