Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>[Edit]</p> <p>Ok here we go =)</p> <pre><code>&gt;&gt;&gt; import imaplib, email &gt;&gt;&gt; mail = imaplib.IMAP4_SSL('imap.gmail.com') &gt;&gt;&gt; mail.login('emailaddr@gmail.com', 'password') ('OK', ['emailaddr@gmail.com Inget Namn authenticated (Success)']) &gt;&gt;&gt; mail.select('inbox') ('OK', ['14']) &gt;&gt;&gt; result, data = mail.uid('search', None, 'ALL') &gt;&gt;&gt; uids=data[0].split() &gt;&gt;&gt; result, data = mail.uid('fetch', uids[-1], 'BODYSTRUCTURE') &gt;&gt;&gt; print data ['14 (UID 340 BODYSTRUCTURE ((("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 17 1 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 17 1 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "20cf3071d16a5a877b04d0adcc43") NIL NIL)("APPLICATION" "PDF" ("NAME" "attiny40.pdf") NIL NIL "BASE64" 8429956 NIL ("ATTACHMENT" ("FILENAME" "attiny40.pdf")) NIL) "MIXED" ("BOUNDARY" "20cf3071d16a5a878104d0adcc45") NIL NIL))'] &gt;&gt;&gt; </code></pre> <p>The attachement for this message is called "attiny40.pdf" and you can clearly see that name in the BODYSTRUCTURE. All that is left is parsing that BODYSTRUCTURE.</p> <p>The code is pretty much taken straight from the last link below.</p> <p>[/Edit]</p> <p>You will need to change the parameter for fetch from RFC822 to BODYSTRUCTURE.</p> <p>And then as described <a href="http://tools.ietf.org/html/rfc3501#section-7.4.2" rel="nofollow noreferrer">here</a> for example.</p> <blockquote> <p>For example, a two part message consisting of a text and a BASE64-encoded text attachment can have a body structure of: (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff") "&lt;960723163407.20117h@cac.washington.edu>" "Compiler diff" "BASE64" 4554 73) "MIXED")</p> </blockquote> <p>See also <a href="https://stackoverflow.com/questions/4745572/imap-attachement-retriving-commnand">this post</a> and <a href="https://stackoverflow.com/questions/13663672/get-the-gmail-attachment-filename-without-downloading-it/13663947#13663947">this one</a>. The last link looks like pretty much as what you are trying to do.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload