0

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Pedroski
  • 433
  • 1
  • 7
  • 16
  • `multipart/alternative` (payload 0) is the content type *header* for what is likely the plaintext version of the email message, while `application/octet-stream` is the header your attachment. Your attachment will have other headers, one will be filename. I'm not familiar with the `pyzmail` module, but you may have some luck exploring [this section of the docs](http://www.magiksys.net/pyzmail/api/pyzmail.parse-module.html): – jedwards Oct 10 '18 at 23:08
  • Thanks! I'll read up. Someone told me I should use the imaplib module, but I can't seem to get that to work! – Pedroski Oct 11 '18 at 05:06
  • Relevant : [mimemultipart-mimetext-mimebase-and-payloads-for-sending-email-with-file-atta](https://stackoverflow.com/questions/38825943/mimemultipart-mimetext-mimebase-and-payloads-for-sending-email-with-file-atta) – stovfl Oct 11 '18 at 06:57

0 Answers0