【问题标题】:CakePHP sends multiple emails instead of oneCakePHP 发送多封电子邮件而不是一封
【发布时间】:2014-04-13 17:49:43
【问题描述】:

我有一个奇怪的错误。我有一个脚本,每天为约 12K 用户发送电子邮件。它运行良好大约 3 个月,然后几天前它开始发送垃圾邮件。它向所有用户发送了大约 15 份电子邮件副本,他们当然开始抱怨。该脚本每天被 cron 作业激活一次,根据服务器的支持,它只被触发一次,没有任何问题。当我尝试将脚本仅发送到一个电子邮件地址时,它可以正常工作,但是当我再次尝试触发所有电子邮件时,我遇到了获取多个副本的相同问题。我将它发送到同一个电子邮件地址只是为了避免再次向用户发送垃圾邮件。我放了一些日志语句来查看被解雇的计数和电子邮件,并看到了一些奇怪的东西:一旦它接近用户列表的末尾,我看到来自列表开头的电子邮件被解雇了。比如:

    2014-04-13 04:33:02 Email: 12340 yyy@gmail.com
2014-04-13 04:33:03 Email: 12341 yyy@hotmail.com
2014-04-13 04:33:03 Email: Daily email function got called <- looks like it gets trough new iterations
2014-04-13 04:33:03 Email: 12342 yyy@gmail.com
2014-04-13 04:33:03 Email: 12343 yyy@o2.pl
2014-04-13 04:33:03 Email: 12344 yyy@yahoo.com
2014-04-13 04:33:03 Email: 12345 yyy@aol.com
2014-04-13 04:33:03 Email: 12346 yyy@outlook.com
2014-04-13 04:33:03 Email: 12347 yyy@hotmail.com
2014-04-13 04:33:03 Email: 1 yyy@gmail.com       
2014-04-13 04:33:03 Email: 12348 yyy@comcast.net
2014-04-13 04:33:03 Email: 2 yyy@yahoo.com
2014-04-13 04:33:03 Email: 12349 yyy@yahoo.es
2014-04-13 04:33:03 Email: 3 yyy@hotmail.com
2014-04-13 04:33:03 Email: 12350 yyy@hotmail.com
2014-04-13 04:33:03 Email: 4 yyy@yahoo.com
2014-04-13 04:33:03 Email: 12351 yyy@gmail.com
2014-04-13 04:33:03 Email: 5 yyy@gmail.com.com
2014-04-13 04:33:03 Email: 12352 yyy@hotmail.com

看起来该脚本被第二次调用,但可能会调用它什么?

功能:

 function daily_email() {
        CakeLog::write('email', 'Daily email function got called');
        $count = 0;
        ini_set("memory_limit", "2048M");
        set_time_limit("0");
        $from = array('yyy@yyyy.com' => 'yyyyyyyyy');
        $subject = 'bla';
        $template = 'daily';
        $layout = 'daily_layout';
        $users_data = $this->User->find('all', array('conditions' => array('User.unsubscribed ' => '0')));
        foreach ($users_data as $user) {
            $count++;
            $user = $user['User'];
            $to = $user['email_address'];
            $s5 = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 31); //change security string every day
            $offer_link = 'http://bla.bla.com' . $s5;
            $unsubscribe_link = "http://" . $_SERVER['HTTP_HOST'] . "/dailies/user/unsubscribe/" . $user['id'];
            $this->Emailer->send_email($from, $to, $subject, $template, $layout, $offer_link, $unsubscribe_link, $s5);
            $this->User->id = $user['id'];
            $user['is_used'] = 0;
            $user['security_string'] = $s5;
            $this->User->save($user);
        }
        CakeLog::write('email', 'Sent ' . $count . ' emails');
        $this->autoRender = false;
    }

电子邮件组件:

App::uses('Component', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class EmailerComponent extends Component {

    function send_email($from, $to, $subject, $template, $layout, $offer_link, $unsubscribe_link, $s5=null) {
        $email = new CakeEmail();
        $email->template($template, $layout);
        $email->emailFormat('html');         
        $email->to($to);
        $email->from($from);
        $email->subject($subject);
        $email  ->viewVars(array('offer_link' => $offer_link, 'unsubscribe_link' => $unsubscribe_link, 's5' => $s5));
       $email ->send();
    }
}

定时任务:

0 4 * * * /usr/bin/wget -O /dev/null http://www.myNiceSite.com/dailies/user/daily_email

此外,不确定它是否相关,但 ps aux 会在脚本运行后显示两个进程。

提前感谢大家的帮助

【问题讨论】:

    标签: mysql email cakephp-2.0


    【解决方案1】:

    看来我找到了问题的根源。 Wget 调用脚本并等待响应,但脚本完成所需的时间太长。所以 wget 再次调用,它可能进入无限循环。现在的修复是在脚本运行时在数据库中设置一个标志,它会阻止脚本第二次运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2011-09-06
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多