我的问题在另一篇文章中得到了不同的回答,但我不能用我自己的代码处理 - 所以它来了:
我正在创建一个reminder function
,它应该由cron作业执行并发送reminder email
来提醒客户有关事件的信息。
我将使用mysql中的信息,根据X小时数和提醒时间范围(if the cron job runs every 15 minutes, the code must find all event starting in the minutes between each cron job run)
来排序将要提醒的事件。
上述所有工作都很好,我从 test-cron.php 获得echo "test1";
。但是 test-cron-email-reminder.php 的电子邮件不会发送,我也没有打印出echo "test2";
。
我好像我在test-cron.php中包含的内容不起作用。为什么呢?
如果我将它们放在一个php文件中,它可以正常工作。
准备就绪后,我会制作一个类似的代码,用twilio发送短信提醒。只要整个代码在一个文件中,那也可以正常工作。
两个php文件都在同一个文件夹中。
这是我的代码:
TEST-CRON.PHP
<?php
require_once 'Connect_db.php';
require 'configuration.php';
// Get info from SQL
$result = performQuery("SELECT mindful_pbbooking_events.service_id,
mindful_pbbooking_treatments.id,
mindful_pbbooking_events.id, name,
customfields_data,
dtstart, dtend, date_created
FROM mindful_pbbooking_events,
mindful_pbbooking_treatments
WHERE
mindful_pbbooking_events.service_id=mindful_pbbooking_treatments.id;
");
while ($row = mysqli_fetch_array($result)) {
//Split customfields_data and collect the informaton from created array (just making things a little bit more easy to work with)
$dataArray = $row[customfields_data];
$dataArrayDecoded = json_decode($dataArray,TRUE);
$clientFname = $dataArrayDecoded[0][data];
$clientLname = $dataArrayDecoded[1][data];
$clientEmail = $dataArrayDecoded[2][data];
$clientMobile = $dataArrayDecoded[3][data];
$clientGender = $dataArrayDecoded[4][data];
//Collect information from customfields_data (more making things a little bit more easy to work with)
$eventId = $row[mindful_pbbooking_events.id];
$eventStart = $row[dtstart];
$eventDate = date("Y-m-d", strtotime($eventStart));
$eventTime = date("H:i", strtotime($eventStart));
$eventEnd = $row[dtend];
$service = $row[name];
$eventCreated = $row[date_created];
//Time calculation to find out who to send reminder to
$eventtimestring = strtotime("$eventStart");
$nowtimestring = strtotime("now");
$reminderdurationstring = $reminderDuration*60;
$startstring = $nowtimestring + $hours*3600;
$endstring = $startstring + $reminderdurationstring;
while (($startstring <= $eventtimestring) && ($eventtimestring < $endstring)) {
// Just a little test to find out where things goes wrong
echo "test1";
// ****** HERE IT COMES ******
// The test-cron-email-reminder.php is the file with the code I want to include
include 'test-cron-email-reminder.php';
}
}
?>
TEST-CRON-EMAIL-REMINDER-PHP
<?php
require_once 'Connect_db.php';
require 'configuration.php';
// Just a little test to find out where things goes wrong
echo "test2";
$to = "$clientEmail";
$subject = "The reminder mail body";
$message = "
<html>
<head>
<title>The reminder mail title</title>
</head>
<body>
<p>The reminder mail body</p>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: $clientFname <$clientEmail>';
$headers[] = 'From: Mindful <mail@mail.com>';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
break;
?>
答案 0 :(得分:0)
问题在于TEST-CRON.PHP - 不要在include
循环中放置while
,除非你真的想要反复包含该文件。 (你不要)
while (($startstring <= $eventtimestring) && ($eventtimestring < $endstring)) {
// Just a little test to find out where things goes wrong
echo "test1";
...
/// DON'T DO THIS
include 'test-cron-email-reminder.php';
/// DON'T DO THIS
}
相反,这样做。在TEST-CRON.PHP中:
<?php
require_once 'Connect_db.php';
require 'configuration.php';
require_once 'TEST-CRON-EMAIL-REMINDER-PHP'
...
while (($startstring <= $eventtimestring) && ($eventtimestring < $endstring)) {
// Just a little test to find out where things goes wrong
echo "test1";
...
doSomething(); // Defined in TEST-CRON-EMAIL-REMINDER-PHP
break;
}
在TEST-CRON-EMAIL-REMINDER-PHP中:
<?php
require_once 'Connect_db.php';
require 'configuration.php';
// And wrap all this stuff up in a function that
// you can call from within your while() loop.
func doSomething() {
// Just a little test to find out where things goes wrong
echo "test2";
$to = "$clientEmail";
$subject = "The reminder mail body";
$message = "
<html>
<head>
<title>The reminder mail title</title>
</head>
<body>
<p>The reminder mail body</p>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: $clientFname <$clientEmail>';
$headers[] = 'From: Mindful <mail@mail.com>';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
}
?>
答案 1 :(得分:0)
要求'configuration.php'; 在TEST-CRON-EMAIL-REMINDER-PHP中 可能会产生重新声明错误
尝试require_once'configuration.php'; 防止它