cron 作业脚本从浏览器工作,但不是从调度程序工作。?
cron job script working from browser but not from schedualer.?
这是我的 PHP 页面代码,我将用它发送电子邮件,但我无法提供数据库连接文件的路径,如果我通过浏览器 运行 脚本,该文件可以正常工作.
编辑:我更新了包含和文件目录的代码。现在正在更新其他错误截图。
#!/usr/bin/php70
include __DIR__.'/db_connection.php';
include __DIR__.'/PHPMailer-master/PHPMailerAutoload.php';
/* Date time sent emails */
$date = new DateTime('now', new DateTimeZone('Asia/Karachi'));
$email_query = "SELECT *FROM marketing_email WHERE email_timer = '1' ";
$email_result = mysqli_query($db, $email_query);
if (mysqli_num_rows($email_result) > 0) {
while ($email_rows = mysqli_fetch_assoc($email_result)) {
$attechment_query = "SELECT *FROM marketing_email_attechements WHERE marketing_email_id = '".$email_rows['id']."' AND status = '1' ";
$attechment_result = mysqli_query($db, $attechment_query);
if ($attechment_result) {
$path = array();
while ($attechment_rows = mysqli_fetch_assoc($attechment_result)) {
$path[] = '../uploads/email-files/'.date('dmY').'-'.$attechment_rows['filename'];
}
}
// exit();
if ($date->format('Y-m-d H:i:00') >= $email_rows['email_datetime']) {
/* Send email */
$subject = $email_rows['subject'];
$msg = $email_rows['message'];
$to = $email_rows['receiver_email'];
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
$mail->Port = 587;
for($ct=0;$ct<count($path);$ct++){
$mail->AddAttachment($path[$ct]);
}
// $mail->AddAttachment($path);
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'soomroa923@gmail.com';
//Password to use for SMTP authentication
$mail->Password = "abdullah999";
//Set who the message is to be sent from
$mail->From = 'from@example.com';
$mail->FromName = 'Job-interview';
//Set an alternative reply-to address
//$mail->addReplyTo(‘hidaya@gmail.com', 'hidaya');
//Set who the message is to be sent to
$mail->addAddress($to,'Job-interview');
//Set the subject line
$mail->Subject = $subject;
$mail->msgHTML($msg);
//send the message, check for errors
if (!$mail->send()) {
"Mailer Error: " . $mail->ErrorInfo;
//header("location:manage_users.php?flag=3");
}
else {
$update_query = "UPDATE marketing_email SET email_timer = '0' WHERE id = '".$email_rows['id']."' ";
$update_result = mysqli_query($db, $update_query);
}
}
else{
echo "time not exist";
}
}
?>
<script type="text/javascript">
//location.href="../email-list.php";
console.log('Thank you email has been sent');
</script>
<?php
}
else{
echo "Data not found";
}
?>
我正在尝试为其安排电子邮件,我创建了一个 cron 作业,该作业给了我一个文件错误,但在浏览器中运行正常。
这是我尝试创建 cron 作业并测试其文件是否 运行 时我的 cpanel 的屏幕截图。
您可以将 cron 命令重命名为 /usr/bin/php70 -f /path/to/your.php
,并确保 /usr/bin/php70
是有效路径。
我正在为类似的问题而苦苦挣扎。
我想我看到了一件事看起来不对劲。在第 19 行,您的代码显示
$path[] = '../uploads/email-files/'.date('dmY').'-'.$attechment_rows['filename'];
这是一个相对路径,不能从命令行运行,这是 cron 作业的方式 运行,但在浏览器中运行良好。
尝试将该行的开头更改为类似以下内容:
$path[] = 目录名(DIR)。 '/uploads/email-files/'.date('dmY')...
(DIR 前后应该有两个下划线)如果你尝试一些 echo 语句,你就会找到正确的路径。
这是我的 PHP 页面代码,我将用它发送电子邮件,但我无法提供数据库连接文件的路径,如果我通过浏览器 运行 脚本,该文件可以正常工作.
编辑:我更新了包含和文件目录的代码。现在正在更新其他错误截图。
#!/usr/bin/php70
include __DIR__.'/db_connection.php';
include __DIR__.'/PHPMailer-master/PHPMailerAutoload.php';
/* Date time sent emails */
$date = new DateTime('now', new DateTimeZone('Asia/Karachi'));
$email_query = "SELECT *FROM marketing_email WHERE email_timer = '1' ";
$email_result = mysqli_query($db, $email_query);
if (mysqli_num_rows($email_result) > 0) {
while ($email_rows = mysqli_fetch_assoc($email_result)) {
$attechment_query = "SELECT *FROM marketing_email_attechements WHERE marketing_email_id = '".$email_rows['id']."' AND status = '1' ";
$attechment_result = mysqli_query($db, $attechment_query);
if ($attechment_result) {
$path = array();
while ($attechment_rows = mysqli_fetch_assoc($attechment_result)) {
$path[] = '../uploads/email-files/'.date('dmY').'-'.$attechment_rows['filename'];
}
}
// exit();
if ($date->format('Y-m-d H:i:00') >= $email_rows['email_datetime']) {
/* Send email */
$subject = $email_rows['subject'];
$msg = $email_rows['message'];
$to = $email_rows['receiver_email'];
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
$mail->Port = 587;
for($ct=0;$ct<count($path);$ct++){
$mail->AddAttachment($path[$ct]);
}
// $mail->AddAttachment($path);
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'soomroa923@gmail.com';
//Password to use for SMTP authentication
$mail->Password = "abdullah999";
//Set who the message is to be sent from
$mail->From = 'from@example.com';
$mail->FromName = 'Job-interview';
//Set an alternative reply-to address
//$mail->addReplyTo(‘hidaya@gmail.com', 'hidaya');
//Set who the message is to be sent to
$mail->addAddress($to,'Job-interview');
//Set the subject line
$mail->Subject = $subject;
$mail->msgHTML($msg);
//send the message, check for errors
if (!$mail->send()) {
"Mailer Error: " . $mail->ErrorInfo;
//header("location:manage_users.php?flag=3");
}
else {
$update_query = "UPDATE marketing_email SET email_timer = '0' WHERE id = '".$email_rows['id']."' ";
$update_result = mysqli_query($db, $update_query);
}
}
else{
echo "time not exist";
}
}
?>
<script type="text/javascript">
//location.href="../email-list.php";
console.log('Thank you email has been sent');
</script>
<?php
}
else{
echo "Data not found";
}
?>
我正在尝试为其安排电子邮件,我创建了一个 cron 作业,该作业给了我一个文件错误,但在浏览器中运行正常。
这是我尝试创建 cron 作业并测试其文件是否 运行 时我的 cpanel 的屏幕截图。
您可以将 cron 命令重命名为 /usr/bin/php70 -f /path/to/your.php
,并确保 /usr/bin/php70
是有效路径。
我正在为类似的问题而苦苦挣扎。 我想我看到了一件事看起来不对劲。在第 19 行,您的代码显示
$path[] = '../uploads/email-files/'.date('dmY').'-'.$attechment_rows['filename'];
这是一个相对路径,不能从命令行运行,这是 cron 作业的方式 运行,但在浏览器中运行良好。 尝试将该行的开头更改为类似以下内容:
$path[] = 目录名(DIR)。 '/uploads/email-files/'.date('dmY')...
(DIR 前后应该有两个下划线)如果你尝试一些 echo 语句,你就会找到正确的路径。