简体   繁体   中英

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
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.

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. Removed the $_POST superglobals and the cron script executed flawlessly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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