Reputation: 1011
I am building a system in which you can send email at a desired date and time, there is an interface which captures this data and through a cron the email is sent. This is all working as expected but I am having an issue with the attachments.
When the cron runs and sends the email it seems as if there is more than 1 email going out, the 2nd email adds the attachments of the previous email sent to itself, and this carries on. I have tried unsetting the value of the array which holds the file names but still no luck.
Email A -- (Has 3 Attachments / Sends 3 Attachments)
Email B -- (Has 2 Attachments / Sends 5 Attachments)
Email C -- (Has 2 Attachments / Sends 7 Attachments)
This is strange because this is my array output after each loop:
Successful
Array
(
[id] => 38
[company_id] => 225
[message_sender] => Favour Sanctuary
[message_subject] => Delayed Email Testing
[recipients] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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
)
Here is my Cron Function:
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';
}
}
}
Here is my send email Helper:
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 : '[email protected]';
$subject = $subject ? $subject: 'Correspondence';
$body = $message;
$sender = $sender ?: company_name($company_id);
$CI->email->from('[email protected]' , $sender);
$CI->email->reply_to('[email protected]');
$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();
} */
}
Upvotes: 0
Views: 25
Reputation: 37810
Since you tagged this with PHPMailer, I assume that PHPMailer is being used behind the scenes. When you call $CI->email->attach($attach);
, it retains the list of files you've attached, i.e. it always adds attachments, it doesn't set them. Notice that the number of attachments always increases by the number of files you've added each time, so the first one gets 3, you add 2, making 5, then add another 2, making 7. It's not your input arrays that are at fault, it's the internal list of attachments held within your mail instance ($CI->email
).
If PHPMailer is inside there, you need to call the clearAttachments()
method to clear them - I'd assume that CI has an equivalent or a wrapper for it - refer to their docs.
Upvotes: 1