php Cron作业脚本是否从浏览器而不是从调度程序运行?

pxq42qpu  于 2022-12-10  发布在  PHP
关注(0)|答案(2)|浏览(152)

这是我的PHP页面代码,我将发送电子邮件,但我不能给予一个路径的DB连接文件,这是完美的工作,如果我通过浏览器运行脚本。
编辑:我已经更新了包含和文件目录的代码。现在我得到其他错误-截图更新。

#!/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作业,给了我一个文件错误,从浏览器工作正常。
这是我的cpanel的屏幕截图,当我试图创建cron作业,并测试它的文件,如果它运行与否。

muk1a3rh

muk1a3rh1#

您可以将cron命令重命名为/usr/bin/php70 -f /path/to/your.php,并确保/usr/bin/php70是有效路径。

m1m5dgzv

m1m5dgzv2#

我也遇到了类似的问题。我想我看到了一个看起来不对的地方。在第19行,你的代码说
文件名为“文件名”;文件名为“文件名";
这是一个相对路径,在命令行中无法使用,而cron作业是这样运行的,但在浏览器中可以正常工作。请尝试将该行的开头更改为如下形式:
$path[] =目录名称(DIR). '/上载/电子邮件文件/'.日期('dmY')...
(DIR在它的前后应该有两个下划线)如果你尝试一些echo语句,你会得到正确的路径。

相关问题