I am using a script to send files by email. Currently my script calculates the sum of all file sizes and if the sum > 20MB it sends as many emails as there are attachments. If the size < 20MB then all documents are attached to a single email.
Ideally, I would like to keep each email sent to a maximum of 10MB regardless of the number of attachments.
Any suggestions on how I can accomplish this?
Below the relevant part of my script:
$attachments_size = 0;
foreach ($files as $attachment) { // calculate size of all attachments
$url = "/path/to/attachments/{$attachment['attachment_filename']}";
$url = stat(iconv('UTF-8', 'ISO-8859-1', $url));
$attachments_size += $url[7];
}
$attachments = array();
if ($attachments_size > 20971520) { // send 1 attachment per email
$email_count = 1;
foreach ($files as $attachment) {
$subject ="[" . $attachment_id . "]-[" . $email_count . "/" . $file_count . "] " . $attachment['attachment_filename']; // file_count = number of attachments
array_push($attachments, $attachment['attachment_filename']);
$send_it = send_to_scans($subject, $body, $attachments); // sends using PHPMailer
$email_count++;
}
}
else { // send all attachments in 1 email
$subject = "[" . $attachment_id . "] " . ($file_count > 1 ? "Multiple files attached" : $attachment['attachment_filename']);
foreach ($files as $attachment) {
array_push($attachments, $attachment['attachment_filename']);
}
$send_it = send_to_scans($subject, $body, $attachments); // sends using PHPMailer
}