如何使用Python转发电子邮件 [英] How to forward email using Python

查看:582
本文介绍了如何使用Python转发电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我说,我已经知道在已经使用python smtplib转发电子邮件了.

我之所以发布与该问题密切相关的内容,是因为我尝试使用该问题的答案,尝试进行更改,搜索Google并毫不留情地搜索了大约5个小时,我愿意在此上花更多的时间

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 :)

我的问题如下,我正在尝试将电子邮件从我的gmail转发到另一个gmail,并且在运行尽可能多的python脚本以尝试执行此简单任务的过程中,我仍然无法弄清楚.

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 = "John.Michael.Dorian.4@gmail.com"
to_addr = "myotheremail@gmail.com"


# 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()

SMTP调试返回的消息表明一切正常,并且我知道它正在发送,因为我尝试替换了

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())

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

使用

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

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

效果很好.它可以很好地打印message.as_string(),我不知如何获得它来转发电子邮件!

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!

它不一定必须与SMTP或IMAP或任何此类代码一起使用(尽管如果这样的话会很好),但我真的很想知道如何做到这一点.

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.

我知道它的可能性,因为我昨天设法做到了,而我正在使用的计算机(当然是运行Windows)崩溃了,文件消失了.

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.

对于那些想知道为什么我不仅仅将google设置为自动转发所有内容的人来说,这是因为我想要一个脚本,该脚本最终将一次移动大量邮件.

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.

谢谢大家!

推荐答案

原始电子邮件的Received:标头很可能导致gmail删除邮件.在转发前,请尝试将其全部删除.

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.

但是,为什么要以这种方式前进?只需从一个IMAP帐户中提取并直接将其推到另一个IMAP帐户中,会更容易.

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

实际上,您可以使用Mozilla Thunderbird添加两个帐户,只需将消息从一个帐户拖放到另一个帐户即可.

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

这篇关于如何使用Python转发电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆