使用python smtplib转发电子邮件 [英] Forwarding an email with python smtplib

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

问题描述

我正在尝试将一些脚本自动转发到符合特定标准的电子邮件到另一封电子邮件。

I'm trying to put together a script that automatically forwards certain emails that match a specific criteria to another email.

我已经下载并解析了使用imaplib和电子邮件工作的消息,但我无法弄清楚如何将整个电子邮件转发到另一个地址。我是否需要从头开始构建一个新消息,或者我可以以某种方式修改旧消息并重新发送?

I've got the downloading and parsing of messages using imaplib and email working, but I can't figure out how to forward an entire email to another address. Do I need to build a new message from scratch, or can I somehow modify the old one and re-send it?

这是我到目前为止(客户端是imaplib.IMAP4连接,id是消息ID):

Here's what I have so far (client is an imaplib.IMAP4 connection, and id is a message ID):

import smtplib, imaplib

smtp = smtplib.SMTP(host, smtp_port)
smtp.login(user, passw)

client = imaplib.IMAP4(host)
client.login(user, passw)
client.select('INBOX')

status, data = client.fetch(id, '(RFC822)')
email_body = data[0][1]
mail = email.message_from_string(email_body)

# ...Process message...

# This doesn't work
forward = email.message.Message()
forward.set_payload(mail.get_payload())
forward['From'] = 'source.email.address@domain.com'
forward['To'] = 'my.email.address@gmail.com'

smtp.sendmail(user, ['my.email.address@gmail.com'], forward.as_string())

我确定有一些ething稍微复杂一点我需要做的就是消息的MIME内容。当然有一些简单的方法只是转发整个消息呢?

I'm sure there's something slightly more complicated I need to be doing with regard to the MIME content of the message. Surely there's some simple way of just forwarding the entire message though?

# This doesn't work either, it just freezes...?
mail['From'] = 'source.email.address@domain.com'
mail['To'] = 'my.email.address@gmail.com'
smtp.sendmail(user, ['my.email.address@gmail.com'], mail.as_string())


推荐答案

我认为您错误的部分是如何替换邮件中的标题,而且您不需要复制邮件的事实,您可以在从IMAP服务器中获取的原始数据创建后,直接对其进行操作。

I think the part you had wrong was how to replace the headers in the message, and the fact that you don't need to make a copy of the message, you can just operate directly on it after creating it from the raw data you fetched from the IMAP server.

您没有省略一些细节,所以这里是我完整的解决方案,详细说明了所有细节。请注意,我将SMTP连接置于STARTTLS模式,因为我需要它,并注意到我将IMAP阶段和SMTP阶段相互分离。也许你以为改变消息会在IMAP服务器上改变它?如果你这样做,这应该清楚地表明,这不会发生。

You did omit some detail so here's my complete solution with all details spelled out. Note that I'm putting the SMTP connection in STARTTLS mode since I need that and note that I've separated the IMAP phase and the SMTP phase from each other. Maybe you thought that altering the message would somehow alter it on the IMAP server? If you did, this should show you clearly that that doesn't happen.

import smtplib, imaplib, email

imap_host = "mail.example.com"
smtp_host = "mail.example.com"
smtp_port = 587
user = "xyz"
passwd = "xyz"
msgid = 7
from_addr = "from.me@example.com"
to_addr = "to.you@example.com"

# open IMAP connection and fetch message with id msgid
# store message data in email_data
client = imaplib.IMAP4(imap_host)
client.login(user, passwd)
client.select('INBOX')
status, 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)

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

希望这有助于即使这个答案很晚。

Hope this helps even if this answer comes quite late.

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

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