Checking total email size

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Checking total email size

Post by alex.barylski »

Is there any way to determine the length of an email (w/ attachments, etc) without resorting to a bandwidth monitor plugin?

I would like to perform a quick calculation before the loop begins and exit if the emails total size or sum is greater than a monthly quota?

Adding attachment sizes can be done manually, but I'm wondering what also gets added to the email which I would not account for doing this.???
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Checking total email size

Post by Chris Corbyn »

Adding attachment sizes isn't as clear cut as measuring the filesize of the attachment since base64 encoded data will always be equal to:

(originalByteCount * 4) / 3 (then padded to be a multiple of 4)

You need to account for the headers and wot-not too.

A simple strlen() should return the number of bytes in the resulting string.

Code: Select all

echo strlen($message->build()->readFull());
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Checking total email size

Post by alex.barylski »

Hmmm...not sure I follow.

My email host allows only X number of bytes transfer per month so I need ot make sure my application doesn't go over - to the byte or damn near if possible. If I check the strlen of just the message, how do I account for things like heads, etc?

Is there no way to retreive the entire message size just before delivery?

The only thing which changes is the To: address which I suppose I could calculate accordingly, same goes for attachements, etc...but I wondering if this is sufficient to measure exact bytes???

Sorry if I repeat myself here, but it's getting late and my eyes are starting to tire...as are my over worked hands and fingers... :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Checking total email size

Post by Chris Corbyn »

Hockey wrote:Hmmm...not sure I follow.

My email host allows only X number of bytes transfer per month so I need ot make sure my application doesn't go over - to the byte or damn near if possible. If I check the strlen of just the message, how do I account for things like heads, etc?

Is there no way to retreive the entire message size just before delivery?

The only thing which changes is the To: address which I suppose I could calculate accordingly, same goes for attachements, etc...but I wondering if this is sufficient to measure exact bytes???

Sorry if I repeat myself here, but it's getting late and my eyes are starting to tire...as are my over worked hands and fingers... :)
*sigh*

Echo the content which $message->build()->readFull() returns.
Post Reply