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

查看:47
本文介绍了使用 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())

我确信对于邮件的 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天全站免登陆