[英]Show mail sent through SMTP server in my IMAP “Sent” mailbox
我可以使用SMTP服務器發送郵件:
self.smtp_connection.sendmail(
'my_smtp_username@provider.com',
recipients, # list of To, Cc and Bcc mails
mime_message.as_string()
)
但是我看不到“ my_smtp_username@provider.com” IMAP帳戶的“已發送”框中發送的郵件。 如何在此框中顯示郵件?
SMTP和IMAP協議是兩種不同的事物 :通過SMTP服務器發送的郵件對於IMAP而言是不可見的。
因此,您需要自己模擬這種行為,方法是將郵件發送到您自己的地址(但不要在MIME對象的“ To:”標頭中添加您),然后將郵件移到正確的框中:
使用API抽象,使用相同的帳戶imap_connection
正確初始化了smtp_connection
和self.email_account
:
def send(self, recipients, mime_message):
"""
From the MIME message (object from standard Python lib), we extract
information to know to who send the mail and then send it using the
SMTP server.
Recipients list must be passed, since it includes BCC recipients
that should not be included in mime_message header.
"""
self.smtp_connection.sendmail(
self.email_account,
recipients + [self.email_account],
mime_message.as_string()
)
### On the IMAP connection, we need to move the mail in the "SENT" box
# you may need to be smarter there, since this name may change
sentbox_name = 'Sent'
# 1. Get the mail just sent in the INBOX
self.imap_connection.select_folder('INBOX', readonly=False)
sent_msg_id = self.imap_connection.search(['UNSEEN', 'FROM', self.username])
if sent_msg_id:
# 2. Mark it as read, to not bother the user at each mail
self.imap_connection.set_flags(sent_msg_id, '\Seen')
# 3. Copy the mail in the sent box
self.imap_connection.copy(sent_msg_id, sentbox_name)
# 4. Mark the original to delete and clean the INBOX
self.imap_connection.set_flags(sent_msg_id, '\Deleted')
self.imap_connection.expunge()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.