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

时间:2014-11-11 02:56:50

标签: php cron

我正在尝试运行执行以下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);   
}
?>

如果我通过浏览器直接访问该文件,则可以完美。但是,如果通过cron作业运行它,它将只执行foreach代码ONCE ...这意味着它只从MySQL数据库中拉出第一行并发送一封电子邮件,当它应该拉多行并发送多封电子邮件时。

我不知道为什么会发生这种情况 - 我一直在寻找没有运气的原因。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

脚本中的

get_campaigns()函数使用$ _POST superglobals - 无法通过cron作业执行此操作。删除了$ _POST superglobals和执行完美的cron脚本。