如何使用 Python imaplib 将消息从一个 imap 服务器复制到另一个 imap 服务器?

发布于 2024-10-02 05:36:23 字数 1216 浏览 9 评论 0原文

我想将邮件从一台 IMAP 服务器复制到另一台 IMAP 服务器。我不想更改任何消息数据。我正在使用 python imaplib。

这是我尝试过的代码:

typ, data = connection1.uid('FETCH', uid, 'RFC822')
connection2.uid('APPEND', None, data[0][1])

但这引发了一个异常:

imaplib.error:UID 命令错误:BAD ['"交付至:[电子邮件受保护]']

因此,我认为参数 (data[0][1]) 的格式不正确,

data[0][1] 的内容如下所示:

发送至:[电子邮件受保护]\r\n已接收:通过 10.216.207.222,SMTP ID n27cs38120weo;\r\n星期五,2010 年 11 月 12 日 09:43:47 -0800 (PST)\r\n收到:通过 10.200.19.19,SMTP ID y19mr234526eba.52.12894526694 ;\r\n11 月 12 日星期五2010 09:43:46 -0800 (PST)\r\n返回路径: [电子邮件] protected]\r\n已收到:来自 dub0-omc1-s20.dub03.hotmail.com (dub0-omc1-s20.dub03.hotmail.com [157.55.0.220])\r\n ..... .

怎么可以我修好这个吗?

更新:在 Wodin 和 Avadhesh 的帮助下,我现在可以附加消息,但如何获取刚刚附加的消息的 UID?

I want to copy a message from one IMAP server to another IMAP server. I don't want to alter any of the message data. I'm using python imaplib.

This is the code I tried:

typ, data = connection1.uid('FETCH', uid, 'RFC822')
connection2.uid('APPEND', None, data[0][1])

But this raises an exception:

imaplib.error: UID command error: BAD ['"Delivered-To: [email protected]']

So the argument (data[0][1]) is not properly formatted I think.

The contents of data[0][1] look like this:

Delivered-To: [email protected]\r\nReceived: by 10.216.207.222 with SMTP id n27cs38120weo;\r\nFri, 12 Nov 2010 09:43:47 -0800 (PST)\r\nReceived: by 10.200.19.19 with SMTP id y19mr234526eba.52.12894526694;\r\nFri, 12 Nov 2010 09:43:46 -0800 (PST)\r\nReturn-Path: [email protected]\r\nReceived: from dub0-omc1-s20.dub03.hotmail.com (dub0-omc1-s20.dub03.hotmail.com [157.55.0.220])\r\n ......

How can I fix this?

Update: With the help of Wodin and Avadhesh I can append messages now, but how do I get the UID of a just appended message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮茶煮酒煮时光 2024-10-09 05:36:24

解决了!

首先复制消息

typ, data = connection1.uid('FETCH', uid, 'RFC822')
connection2.append('Inbox', '', '', data[0][1])

然后从复制的消息中获取唯一的消息ID,如下所示

from email.parser import Parser
parser = Parser()
msg = parser.parsestr(data[0][1])

然后使用消息ID在目标邮箱中查找新消息,如下所示

typ, uid = connection2.uid('SEARCH', None, 'Header', 'Message-Id', msg['message-ID'])

Solved it!

First copy the message with

typ, data = connection1.uid('FETCH', uid, 'RFC822')
connection2.append('Inbox', '', '', data[0][1])

Then fetch the unique message-id from the copied message like this

from email.parser import Parser
parser = Parser()
msg = parser.parsestr(data[0][1])

Then use the message-id to find the new message in the destination mailbox like this

typ, uid = connection2.uid('SEARCH', None, 'Header', 'Message-Id', msg['message-ID'])
比忠 2024-10-09 05:36:24

您可以尝试以下代码:

typ, data = connection1.uid('FETCH', uid, 'RFC822')
import email
msg_str = email.message_from_string(data[0][1])
msg_create = connection2.append(str(dest_fold_code) , flags, '', str(msg_str))

如果看到电子邮件,则标志为“(\Seen)”,如果未看到电子邮件,则标志为“”。

You can try the follwing code:

typ, data = connection1.uid('FETCH', uid, 'RFC822')
import email
msg_str = email.message_from_string(data[0][1])
msg_create = connection2.append(str(dest_fold_code) , flags, '', str(msg_str))

where flags would be '(\Seen)' in case of seen email or '' in case of unseen email.

转瞬即逝 2024-10-09 05:36:24

您是否尝试过:

connection2.append(mailbox, flags, date_time, message)
    Append message to named mailbox.

RFC3501 显示了 UID 命令的语法如下:

uid             = "UID" SP (copy / fetch / search / store)

即似乎没有“UID APPEND”命令。

Have you tried:

connection2.append(mailbox, flags, date_time, message)
    Append message to named mailbox.

RFC3501 shows the syntax of the UID command as follows:

uid             = "UID" SP (copy / fetch / search / store)

i.e. there appears not to be a "UID APPEND" command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文