I got an Python script that parses emails and extracts various things like headers, SHA's of attachments and such. This works most of the time but I have recently ran into a weird thing.
If the email is sent from Apple Mail I don't seem to be able to extract all the headers. So far the only times I see this is if I find the x-mailer:
header to contain some version of Apple Mail.
I grab the emails straight from a IMAP server so they're all stored in the same way on the server as well.
The code I use - minus grabbing from email server etc - are as follows:
import email
from email.parser import HeaderParser
# Some stuff to grab in the email from the IMAP server
# Once grabbed, the email is stored as an 'string' object with the name of message
parser = HeaderParser()
headers = parser.parsestr(message, headersonly=True)
This works perfectly on all emails from all other, as far as I can see, email clients but Apple Mail seems to be different.
It looks like it just disregards some of the headers because it's not like it drops after a certain number of them or anything, it's a bit in the middle and them some random ones on top of that.
Is there any specific tricks to parsing emails that are sent from Apple Mail or is it a case of raising an exception and parsing these manually?
Any ideas or previous experiences out there?