Search code examples
pythonsmtpgmailforwardgmail-imap

How to forward email using Python


First, let me say, I already know that this was asked at Forwarding an email with python smtplib already.

The reason that I am posting something so closely related to that question is that I have tried using the answers to that question, I have tried changing things, I have searched Google and relentlessly monkeyed with this for about 5 hours now, and I am willing to spend a lot more time on this

-- I just thought one of you might have the answer though :)

My problem is as follows, I am trying to forward an email from my gmail to another gmail, and in running as many python script as I can to try this simple task, I still cannot figure it out.

Here is the code that I am running(this is my modified version of what was posted in the other form):

import smtplib, imaplib, email, string

imap_host = "imap.gmail.com"
imap_port = 993
smtp_host = "smtp.gmail.com"
smtp_port = 587
user = "John.Michael.Dorian.4"
passwd = "mypassword"
msgid = 1
from_addr = "[email protected]"
to_addr = "[email protected]"


# open IMAP connection and fetch message with id msgid
# store message data in email_data
client = imaplib.IMAP4_SSL(imap_host, imap_port)
client.login(user, passwd)
client.select()
typ, data = client.search(None, 'ALL')
for mail in data[0].split():
    typ, data = client.fetch(msgid, "(RFC822)")
    email_data = data[0][1]
client.close()
client.logout()


# create a Message instance from the email data
message = email.message_from_string(email_data)

# replace headers (could do other processing here)
message.replace_header("From", from_addr)
message.replace_header("To", to_addr)
print message.as_string()

# open authenticated SMTP connection and send message with
# specified envelope from and to addresses
smtp = smtplib.SMTP(smtp_host, smtp_port)
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(user, passwd)
smtp.sendmail(from_addr, to_addr, message.as_string()) 
smtp.quit()

The return from the SMTP debug says everything went okay, and I know that it is sending because I tried replacing the

smtp.sendmail(from_addr, to_addr, message.as_string())

With

smtp.sendmail(from_addr, to_addr, 'test')

And it worked fine. It prints the message.as_string() fine, and I am at a loss as how to get it to forward the email!

It doesn't have to be with SMTP or IMAP or any of this code(though it would be nice if it was) but I would really like to figure out how to do this.

I know its possible because I managed to do it yesterday, and the computer I was working on(running Windows of course) crashed and the file was gone.

For those of you who are wondering why I do not just set google to forward everything automatically, it is because I want a script that will eventually move a large amount of mail, once.

Thank you everyone!


Solution

  • More than likely, the Received: headers of the original email are causing gmail to drop the message. Try removing all of them before forwarding it.

    If that doesn't fix it, print out the headers and code it to remove all of the ones that would not normally be there on a newly composed message.

    However, why forward this way? It would be easier to just pull from one IMAP account and push it to another IMAP account directly.

    In fact you could use Mozilla Thunderbird to add both accounts and just drag and drop the messages from one to the other.