Z
Z
zigen2014-04-24 14:30:08
Python
zigen, 2014-04-24 14:30:08

How to parse email addresses from mail and send a response to them in Python?

Good afternoon
I receive answers from one of the services at mail.ru. Email addresses are included in the text of these emails. I need to parse these letters, get an email address from them and send a response. I want to implement it in Python. started to study it.
Prompt, please, in what direction to dig. I found the poplib module for parsing mail.
But I don’t understand how to get the text of the letter, so that later I can go through it with a regular expression - get a soapbox and send a response. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SilentSokolov, 2014-04-24
@zigen

Doesn't the documentation say that poplib.retr returns the body of the email?
For example, this should work fine on 2.7, the code is based on the .
(server_msg, body, octets) = M.retr(numMessages)

A
Ashot Ogoltsov, 2014-04-25
@Prenom

Example for imaplib + email library, should work on poplib too.
It should be taken into account that the letter in 99% of cases is multipart, so, an example for 2.7:

import email
 
resp, data = M.FETCH(1, '(RFC822)')
mail = email.message_from_string(data[0][1])
 
for part in mail.walk():
  # мультипарт это лишь сборка контейнером (один для текста, другой для фоток и т.д.)
  if part.get_content_maintype() == 'multipart':
      continue
 
  # ищем текст (plain)
  if part.get_content_subtype() != 'plain':
    continue
 
  payload = part.get_payload()
  print payload
 
M.LOGOUT()

Somehow I was guided by this manual - segfault.in/2010/08/playing-with-python-and-gmail-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question