我目前正在处理一项计划任务,其中任务计划程序将每天运行文件以获取从现在起 3 个月后的到期日期并将电子邮件发送给收件人。但是在这个时候,我似乎想不出正确的语法来做到这一点。这就是我现在所拥有的,这只会给我一个错误。
<?php
//authentication for database
$hostname = "localhost";
$username = "admin";
$password = "xxxxxx";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("notification",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tbl_lead WHERE pass_expiry >= DATE(NOW() + INTERVAL 3 MONTHS");
//variable for email message
$emailBody = "";
$headers = 'From: Pass Validity Reminder' . "\r\n" .
'Reply-To: myemail@email.com' . "\r\n" .
'Cc: ccemail@Wemail. com' . "\r\n".
'X-Mailer: PHP/' . phpversion();
$to = "myemail@email.com";
//fetch tha data from the database
while ($row = mysql_fetch_array($result))
{
$subject = $row['company_name']."'s"." work pass is expiry soon";
$emailBody .="Creator: ".$row['rlog_create_user_name']." \n". "Email: ".$row['email']."
\n"."Comment: ".$row['comment']." \n"."Contact: ".$row['contact']." \n";
}
mail($to, $subject, $emailBody, $headers);
echo 'Email sent successfully!';
//close the connection
mysql_close($dbhandle);
?>
但是,此错误不断出现,我很确定当没有匹配时也会出现错误消息。我该如何完善这个脚本?
( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
C:\wamp\www\notification\staff\notify2.php on line 27
Call Stack
# Time Memory Function Location
1 0.0005 681120 {main}( ) ..\notify2.php:0
2 1.0247 689424 mysql_fetch_array ( ) ..\notify2.php:27
( ! ) Notice: Undefined variable: subject in C:\wamp\www\notification\staff\notify2.php on line34
Call Stack
# Time Memory Function Location
1 0.0005 681120 {main}( ) ..\notify2.php:0
我已经根据@peterm 的建议进行了修改,现在错误已经消失了。但是,现在电子邮件仍然不会发送。
我添加了对电子邮件参数的检查。我在错误消息之前回显了结果,以确保它通过查询。
//fetch tha data from the database
while ($row = mysql_fetch_array($result))
{
$subject = $row['company_name']."'s"." work pass is expiry soon";
$emailBody .= "Company: ".$row['company_name']." \n"."Comment: ".$row['comment']."
\n"."Contact: ".$row['contact']." \n";
}
if(mail($to, $subject, $emailBody, $headers)) {
echo 'Email sent successfully!';
} else {
echo $emailBody;
die('Failure: Email was not sent!');
}
该脚本假设检查数据库中的每个条目并为每个匹配的条目发送电子邮件。很抱歉评论中的编码,我是 stackoverflow 的第一次用户,并且已经 8 年多没有接触编程了。忘记一切,nv 听说过 PDO。@peterm。