1

I'm trying to write a fairly simple script that logs into an email account, looks at each email, does something only for ones with a certain subject, and fwds the rest to another email address. When doing so, I'd like the sender to remain as the original sender rather than the account I'm sending from. The problem is, the target email address (which is a google domain) sends it to spam saying it's in violation of DMARC policy. Here is my code. Is there anything I can change to make it work?

pop_conn = poplib.POP3_SSL(POP_URI)
pop_conn.user(USERNAME)
pop_conn.pass_(PASSWORD)

messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
messages = [parser.Parser().parsestr("\n".join(msg[1])) for msg in messages]

pop_conn.quit()

for email in messages:
    subject = email['subject']
    #if subject is some str, do stuff and continue else fwd mail below

    try:
            server = smtplib.SMTP(SERVER,587)
            server.set_debuglevel(1)
            server.starttls()
            server.ehlo()
            server.login(USERNAME, PASSWORD)
            server.sendmail(email['from'], [TARGET_EMAIL], email.as_string())
            server.quit()
    except Exception as e:
            print "failed to fwd\n"
            print e

Also, additional question: how do I remove the copies from the inbox after retrieving the emails?

Syed H
  • 305
  • 1
  • 2
  • 9

0 Answers0