I'm trying to get .jpg files from emails. I sent myself an email with a photo. I am using the Idle3 shell. After logging in, the following successfully saves the photo to my computer. So later on, I want to adapt to use a loop.
rawMessage = server.fetch([59], ['BODY[]', 'FLAGS'])
message = pyzmail.PyzMessage.factory(rawMessage[59][b'BODY[]'])
len(message.get_payload()) # response is 2
attachment = message.get_payload()[1]
attachment.get_content_type() # response is 'image/jpeg'
open(pathToFiles + 'rob.jpg', 'wb').write(attachment.get_payload(decode=True))
So then I look at another email server.fetch([60], ['BODY[]', 'FLAGS'])
. I know there is a photo in there. I find this:
>>> len(message.get_payload())
2
>>> attachment = message.get_payload()[1]
>>> attachment.get_content_type()
'application/octet-stream'
>>> attachment = message.get_payload()[0]
>>> attachment.get_content_type() 'multipart/alternative'
>>>
These emails are sent by students from their mobile phones.
In this case, where is the photo? The payload has len()
= 2, so one of those parts should be the photo, (I think).
What is 'application/octet-stream'
?
Somewhere in there must be the file name 'studentname.jpg'. I can download it with from the email using Thunderbird.
How can I find the name of the file and the file?