Jump to content

Split emails based on attachment sizes


walermo

Recommended Posts

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.