简体   繁体   English

Cron Job PHP Foreach仅发送一封电子邮件/运行一行

[英]Cron Job PHP Foreach Only Sends Out ONE Email/Runs ONE Row

I am trying to run a cron job that executes the following PHP code: 我正在尝试运行执行以下PHP代码的cron作业:

<?php
require 'core/init.php';
$freq = "1day";
$camp = $users->get_campaigns($freq);
foreach($camp as $val) {
    $data['customer'] = $val['thename']; 
    $data['companyname'] = $val['company']; 
    $data['email'] = $val['reply_to']; 
    if($val['defaultc'] == "1") {
        $phrase = $val['1'];
    }
    elseif($val['defaultc'] == "2") {
        $phrase = $val['2'];
    }
    elseif($val['defaultc'] == "3") {
        $phrase = $val['3'];
    }
    $placeholders = array("{customer}", "{companyname}", "{email}");
    $new_member_file = str_replace($placeholders, $data, $phrase);

    $headers = "From: ".$val['reply_to']."\r\n";
    $headers .= "Reply-To: ".$val['reply_to']."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $message = "<html><head><style type='text/css'>body { font-family: Arial, sans-serif; }</style></head><body>";
    $message .= "".$new_member_file."</body></html>";

    mail($val['theemail'], $val['sub1'], $message, $headers,'-f'.$val['reply_to'].'');
    $users->email_sent($user_id);   
}
?>

If I access the file directly via my browser, it works perfect . 如果我直接通过浏览器访问该文件,它可以完美运行。 However, if running it via a cron job, it will only execute the foreach code ONCE...meaning it only pulls the first row out of the MySQL database and sends ONE email, when it should be pulling multiple rows and sending multiple emails. 但是,如果通过cron作业运行它,它将仅执行foreach代码ONCE ...,这意味着它仅应拉出多行并发送多封电子邮件时,才将第一行从MySQL数据库中拉出并发送一封电子邮件。

I have no clue why this is happening - I have tried searching for a reason why this is happening with no luck. 我不知道为什么会这样-我试图寻找一个没有运气的原因。

Anybody have any ideas? 有人有什么想法吗?

get_campaigns() function in the script was using $_POST superglobals - cannot do this via cron jobs. 脚本中的get_campaigns()函数使用的是$ _POST超全局变量-无法通过cron作业执行此操作。 Removed the $_POST superglobals and the cron script executed flawlessly. 删除了$ _POST超全局变量,并且cron脚本完美执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM