Skip to content Skip to sidebar Skip to footer

Find The Total Size Of An Email Message Using The Email Package

I am using Python's email package to analyze some emails. I need to get the total size of the email, including attachments. Is there a way to do this using the email package? Th

Solution 1:

No, there is no such method; the result of email.message_from_file() is a parsed object tree, and the original file size is not retained in it.

You could render the object tree back to a string, but then you'd still not reproduce the exact original message size as the rendering could easily use subtly different but functionally equivalent output (such as the boundaries between multipart messages).

Since you have a file, simply get the size from the open file object itself instead:

inf.seek(0, 2)
filesize = inf.tell()

Solution 2:

Based on the link posted by Martijn, I found this solution, which I think may be better than the seek/tell approach:

os.fstat(inf.fileno()).st_size

Post a Comment for "Find The Total Size Of An Email Message Using The Email Package"