Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the <a href="http://docs.python.org/library/poplib.html" rel="nofollow">documentation</a>:</p> <pre><code>POP3.retr(which) Retrieve whole message number which, and set its seen flag. Result is in form (response, ['line', ...], octets). </code></pre> <p>So, assuming you have put the result of <code>retr()</code> into a variable called <code>response</code>, the lines of the message are stored as a list in <code>response[1]</code>. By <a href="http://www.ietf.org/rfc/rfc2822.txt" rel="nofollow">RFC 2822</a> we know that the headers are separated from the body of the message by a blank line. The sender of the message will be in the <code>From:</code> header line. So we can just iterate over the lines of the message, stop when we get a blank line, and set a variable to our sender when we see a line that starts with <code>From:</code>.</p> <pre><code>sender = None for line in response[1]: if line.startswith("From: "): sender = line.partition(" ")[2].strip() elif line == "": break </code></pre> <p>If you plan to do a lot with the headers, it might be useful to put them into a dictionary by header name. Since each header can appear multiple times, each value in the dictionary should be a list.</p> <pre><code> headers = {} for line in response[1]: if line == "": break line = line.partition(" ") key = line[0].strip().rstrip(":") value = line[2].stirp() headers.setdefault(key, []).append(value) </code></pre> <p>After this you can use <code>headers["From"][0]</code> to get the sender of the message.</p> <p>I wanted to show the basic way of doing this, because it's not very complicated, but Python can do most of the work for you. Again, assuming that your <code>retr()</code> result is in <code>response</code>:</p> <pre><code> import email # convert our message back to a string and parse it headers = email.parsefromstring("\n".join(response[0]), headersonly=True) print headers["From"] # prints the sender </code></pre> <p>You can find out more about the message object in the documentation for the <a href="http://docs.python.org/library/email.message.html" rel="nofollow">email module</a>.</p> <p>The <code>From:</code> line of an e-mail message may have additional text besides the e-mail address, such as the sender's name. You can extract the email address with a regular expression:</p> <pre><code>sender = re.find(r".*[ &lt;](.+@.+)\b", headers["From"]).match(1) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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