2010-11-14 108 views
1

我想將郵件從一臺IMAP服務器複製到另一臺IMAP服務器。我不想更改任何消息數據。我正在使用python imaplib。如何使用Python imaplib將消息從一臺imap服務器複製到另一臺imap服務器?

這是我試過的代碼:

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

但是,這引發了一個異常:

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

所以參數(數據[0] [1])的格式不正確,我認爲。

數據的內容[0] [1]是這樣的:

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 ......

我該如何解決這個問題?

更新:在Wodin和Avadhesh的幫助下,我現在可以添加消息,但是如何獲取剛添加的消息的UID?

回答

0

解決了!

第一拷貝和

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

的消息。然後,從這樣的

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

複製的信息獲取唯一的消息-ID然後使用郵件ID找到目的地的新信息像這樣的郵箱

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

你試過:

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

RFC3501顯示UID命令的語法如下:

​​

即似乎不是一個 「UID APPEND」 命令。

+0

connection2.append(mailbox,'','',data [0] [1])起作用。我將不得不用一個單獨的IMAP命令來獲取uid。 TNX。 – 2010-11-14 21:54:52

+0

如何獲取剛添加的郵件的uid? – 2010-11-14 22:02:46

2

U可以嘗試follwing代碼:

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

其中標誌將是「(\看)」在看不見的電子郵件時看到的電子郵件或「」的情況。

相關問題