电子邮件附件一起加入

时间:2019-05-08 17:12:34

标签: codeigniter phpmailer

我正在构建一个系统,您可以在该系统中按期望的日期和时间发送电子邮件,有一个界面可以捕获此数据,并通过cron发送电子邮件。 一切都按预期工作,但附件出现问题。

当cron运行并发送电子邮件时,好像有超过1封电子邮件出去了,第二封电子邮件添加了以前发送给自己的电子邮件的附件,然后继续。我试图取消保存文件名的数组的值,但是仍然没有运气。

电子邮件A-(具有3个附件/发送3个附件)

电子邮件B-(具有2个附件/发送5个附件)

电子邮件C-(具有2个附件/发送7个附件)

这很奇怪,因为这是每次循环后我的数组输出:

Successful
Array
(
    [id] => 38
    [company_id] => 225
    [message_sender] => Favour Sanctuary
    [message_subject] => Delayed Email Testing
    [recipients] => xxxxxxxx@gmail.com
    [message_body] => I hope you get this and the pictures
    [message_type_id] => 
    [time_sent] => 2019-05-08 14:17:27
    [attach_file] => ["instagram8.png","networking.jpg"]
    [date] => 2019-05-08
    [time] => 13:19:00
    [sent] => 0
)
Successful
Array
(
    [id] => 39
    [company_id] => 225
    [message_sender] => Verdana State
    [message_subject] => Attachment Mail
    [recipients] => xxxxxxxx@gmail.com
    [message_body] => This time you will get it
    [message_type_id] => 
    [time_sent] => 2019-05-08 14:33:16
    [attach_file] => ["checkout.gif","kokoseller_new_logo.jpg","data_empty.png"]
    [date] => 2019-05-08
    [time] => 14:35:00
    [sent] => 0
)
Successful
Array
(
    [id] => 40
    [company_id] => 225
    [message_sender] => Kwame Eugene
    [message_subject] => Memories of FFx
    [recipients] => xxxxxxxx@gmail.com
    [message_body] => Lets hope this goes well
    [message_type_id] => 
    [time_sent] => 2019-05-08 16:31:34
    [attach_file] => ["evidence.pdf","kokoelec.jpg"]
    [date] => 2019-05-08
    [time] => 16:35:00
    [sent] => 0
)

这是我的Cron功能:

function delayed_email(){
    $check_date = date('Y-m-d');
    $check_time = date('H:i:s');

    $events = $this->db->query('SELECT * from record_mail where `date` IS NOT NULL and `time` IS NOT NULL and (`sent` = 0 OR `sent` IS NULL)')->result_array();
    foreach($events as $e){

        if($e['date']==$check_date && $e['time'] <= $check_time){
            $company_id = $e['company_id'];
            $subject = $e['message_subject'];
            $message = $e['message_body'];
            $sender = $e['message_sender'];
            $recipients = $e['recipients'];

            $attach_files = json_decode($e['attach_file'], TRUE);
            $send = regular_email($recipients,$message,$subject,$attach_files,$company_id,$sender);
            unset($attach_files);
            $attach_files = array();

        }

            if ($send == TRUE){
                echo 'Successful';
                echo '<pre>';
                print_r($e);
                echo '</pre>';

                //$params =array('sent'=>1);
                //$this->db->where('id', $e['id']);
                //$this->db->update('record_mail',$params);
            }else{
                echo 'failed';
            }



    }
}

这是我的发送电子邮件助手:

function regular_email($recipients, $message=NULL, $subject=NULL, $attach_files= NULL, $company_id=NULL, $sender=NULL){

    $CI =& get_instance();
    $CI->load->database();    
    $CI->load->library('email');
    $CI->load->helper('url');

    $to = $recipients ? $recipients : 'xxxxxx@gmail.com';

    $subject = $subject ? $subject: 'Correspondence';
    $body = $message; 
    $sender = $sender ?: company_name($company_id);

    $CI->email->from('xxxxxxx@gmail.com' , $sender);
    $CI->email->reply_to('xxxxxxx@gmail.com');
    $CI->email->to($to);
    $CI->email->subject($subject);
    $CI->email->message($body);

        if(is_array($attach_files)) {
            foreach($attach_files as $k => $v){
                $attach = base_url('uploads/email_attach_file/') . '/' . $v;
                $CI->email->attach($attach);
            }
        } else {
                //ATTACH ONE
                $attach = base_url('uploads/email_attach_file/') . '/' . $attach_files;
                $CI->email->attach($attach);
        }

    $result = $CI->email->send();

        if($result){
            return TRUE;
        } else {
            return FALSE;
        };    

        /*if(!$CI->email->send()){
            return $CI->email->print_debugger();
        }    */

}

1 个答案:

答案 0 :(得分:1)

由于您已使用PHPMailer对此进行了标记,因此我假设在后台使用了PHPMailer。当您调用$CI->email->attach($attach);时,它会保留您已附加的文件列表,即,它总是添加个附件,但不设置个附件。请注意,附件的数量始终会随着您每次添加的文件数量的增加而增加,因此第一个文件为3,您将添加2,使5,然后再添加2,使7。这不是输入数组在错误,这是邮件实例($CI->email)中保存的附件的内部列表。

如果PHPMailer位于其中,则需要调用clearAttachments()方法以清除它们-我假设CI具有等效的东西或包装器-请参阅其文档。