如何通过Cron Job发送CodeIgniter电子邮件

ygya80vv  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(133)

我一直试图通过cron作业向我的用户发送电子邮件,但每次cron执行其任务时,我都会收到以下消息作为响应:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0

我不明白这是什么,但这是代码。我希望运行cron作业

<?php
class Cron_email extends CI_Controller{
    private $CI;
    public function __construct(){
        parent::__construct();
        $this->CI   =& get_instance();
        $this->CI->load->model('Action');
        $this->CI->load->model('Admin_db');
    }
    public function system_auto_calculate(){
       
       $this->CI->load->library('email');

       $site_email = $this->CI->Admin_db->get_site_email();
       $get_site_name = $this->CI->Admin_db->get_site_name();
       $get_site_g_name = $this->CI->Admin_db->get_site_g_name();
       $get_site_g_pass = $this->CI->Admin_db->get_site_g_pass();

       $current_domain = $_SERVER['SERVER_NAME'];

       $config =array(
            'protocol'=> 'ssmtp',
            'smtp_host'    => 'ssl://ssmtp.googlemail.com',
            'smtp_port'    => '465',
            'smtp_timeout' => '7',
            'smtp_user'    => $get_site_g_name,
            'smtp_pass'    => $get_site_g_pass,
            'charset'    => 'utf-8',
            'newline'    => "\r\n",
            'mailtype' => 'html', // or html
            'validation' => FALSE); // bool whether to validate email or not      

        $this->CI->email->set_mailtype("html");
        $this->CI->load->initialize($config);

        $result = $this->CI->Admin_db->get_all_unread_message_by_limit_200();
        if(is_array($result)){
           foreach($result as $row){
                $this->CI->email->clear();
                $id = $row['id'];
                $sender = $row['sender'];
                $receiver = $row['reciever'];
                $title = $row['title'];
                $message = $row['message'];
                $time = $row['time'];
                $date_created = $row['date_created'];
                $email_status = $row['email_status'];

                /**==========================Now Send Message**/
                $subject = $get_site_name.' | '.$title;
                $to = $receiver;

                $data['title'] = $title;
                $data['message'] = $message;
                // $data['link'] = $link;
                // $data['link_title'] = $link_title;

                $this->CI->email->from("$site_email", $get_site_name);
                $this->CI->email->to($to); 
                $this->CI->email->subject($subject);
                $body   =$this->CI->load->view('master_4',$data,TRUE);
                $this->CI->email->message($body);  
                if($this->CI->email->send()){
                    //set email status to send
                    $this->CI->Admin_db->reset_user_email_status($reciever);
                }
                
           }
       }
    }
}

这是我的cron作业命令

*/5 *   *   *   *   /usr/bin/curl "https://{domain.com}/Cron_email/system_auto_calculate"

我已经切换了服务器,也托管在不同的服务器上的文件,但问题仍然存在,但根据支持从我的托管注册,他们说:
我已经和技术支持团队仔细检查了这个问题。根据日志,服务器端没有问题,cPanel本身也没有错误日志。这个问题与cron作业的脚本编码有关。
拜托,我哪里做得不好了?

izkcnapc

izkcnapc1#

你怎么知道你的电子邮件没有被发送?我看到你通过Admin_db->reset_user_email_status()重置了收件人电子邮件状态(不管这意味着什么),你能确认它没有完成吗?
如果在发送电子邮件级别出现问题,可以使用printDebugger方法给予更多详细信息。
cron的输出看起来并不错误,因为你使用curl来运行你的控制器,这本身应该被视为安全漏洞。如果有人知道你的网址,并且有很多邮件要发送,它可能会运行几十次,你的服务器可以被驱逐,因为它试图一次发送太多的邮件。
请阅读CodeIgniter documentation on running controller actions via CLI

相关问题