Saraswitty / WittyMail

MIT License
0 stars 0 forks source link

Detect bounced emails #1

Open ajaynair opened 5 years ago

ajaynair commented 5 years ago

Can we use this? import poplib from email import parser

breaks with if this is left out for some reason (MAXLINE is set too low by default.)

poplib._MAXLINE=20480

pop_conn = poplib.POP3_SSL('your pop server',port) pop_conn.user(username) popconn.pass(password)

Get messages from server:

messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]

Concat message pieces:

messages = ["\n".join(mssg[1]) for mssg in messages]

Parse message intom an email object:

messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: if "Undeliverable" in message['subject']:

    print message['subject']
    for part in message.walk():
        if part.get_content_type():
            body = str(part.get_payload(decode=True))

            bounced = re.findall('[a-z0-9-_\.]+@[a-z0-9-\.]+\.[a-z\.]{2,5}',body)
            if bounced:

                bounced = str(bounced[0].replace(username,''))
                if bounced == '':
                    break

                print bounced 
dotbugfix commented 5 years ago

Hmm, this is basically reading emails in the account - bad for privacy! Maybe there is a way to 'test' email delivery to a recipient's mailbox, similar to traceroute?

dotbugfix commented 5 years ago

A more acceptable privacy-aware method could be to have the user manually forward all 'bounced' emails to a WittyMail email address, which we can then read and process separately. Ties in to session management.