如何使用Python转发电子邮件
首先,我要说的是,我已经知道这是在转发电子邮件中提出的已经使用 python smtplib 了。
我发布与该问题密切相关的内容的原因是我尝试过使用该问题的答案,我尝试过改变一些东西,我不断地搜索 Google现在已经搞砸了大约 5 个小时,我愿意花更多的时间在这上面
——我只是觉得你们中的一个人可能有答案:)
我的问题如下,我正在尝试转发一封来自我的 gmail 到另一个 gmail,并在运行尽可能多的 python 脚本来尝试这个简单的任务时,我仍然无法弄清楚。
这是我正在运行的代码(这是我在其他表单中发布的代码的修改版本):
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()
SMTP 调试的返回表明一切正常,并且我知道它正在发送,因为我尝试替换
smtp.sendmail(from_addr, to_addr, message.as_string())
与
smtp.sendmail(from_addr, to_addr, '测试')
并且工作正常。它可以很好地打印 message.as_string() ,但我不知道如何让它转发电子邮件!
它不必与 SMTP 或 IMAP 或任何此类代码一起使用(尽管如果是的话那就太好了),但我真的很想弄清楚如何做到这一点。
我知道这是可能的,因为我昨天成功做到了这一点,而我正在使用的计算机(当然运行Windows)崩溃了并且文件消失了。
对于那些想知道为什么我不只是将谷歌设置为自动转发所有内容的人来说,这是因为我想要一个最终会一次性移动大量邮件的脚本。
谢谢大家!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很有可能,原始电子邮件的 Received: 标头导致 gmail 丢弃该邮件。在转发之前尝试将其全部删除。
如果这不能解决问题,请打印出标头并对其进行编码,以删除新撰写的消息中通常不会出现的所有标头。
然而,为什么要这样前进呢?从一个 IMAP 帐户中提取并将其直接推送到另一个 IMAP 帐户会更容易。
事实上,您可以使用 Mozilla Thunderbird 添加两个帐户,然后只需将消息从一个帐户拖放到另一个帐户即可。
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.