2

I have this code to extract email body, but the output show the message and some encrypted information. I need help to get only the message.

Last version i've try the lib imaplib, but i don´t have sucess because all message it´s encrypted, so i change to poplib.

As future updates i want to add Subject, date and sender

#!/usr/bin/env python3
# -- coding: utf-8 --

import email
import poplib

login = input('Email: ')
password = input('Password: ')
pop_server = 'pop-mail.outlook.com'
pop_port = 995

mail_box = poplib.POP3_SSL(pop_server, pop_port)
mail_box.user(login)
mailbox.pass_(password)
numMessages = len(mail_box.list()[1])

if numMessages > 15:
    numMessages = 15
for i in range(15):
    (server_msg, body, octets) = mail_box.retr(i+1)
    for j in body:
        try:
            msg = email.message_from_string(j.decode("utf-8"))
            strtext = msg.get_payload()
            print(strtext)
        except:
            pass
Ricardo91
  • 79
  • 1
  • 2
  • 4
  • Can you show the output you are getting? – Easton Bornemeier Aug 03 '17 at 14:53
  • This is the output about the only 2 email i have inbox: --001a11440afa60ba050555c38489 Este =C3=A9 o body do Email **[Email 1 Body]** --001a11440afa60ba050555c38489 Este =C3=A9 o body do Email --001a11440afa60ba050555c38489-- --f403045dd7a8ee72410555c397d6 njsdkbafj **[Email 2 Body]** --f403045dd7a8ee72410555c397d6
    njsdkbafj
    --f403045dd7a8ee72410555c397d6--
    – Ricardo91 Aug 03 '17 at 14:59

1 Answers1

2

If a is the raw email string:

msg = email.message_from_string(a)
if msg.is_multipart():
    for part in msg.walk():
        payload = part.get_payload(decode=True) #returns a bytes object
        strtext = payload.decode() #utf-8 is default
        print(strtext)
else:
    payload = msg.get_payload(decode=True)
    strtext = payload.decode()
    print(strtext)
Sten
  • 116
  • 6