Python 在“我的IMAP”中显示通过SMTP服务器发送的邮件;“发送”;邮箱

Python 在“我的IMAP”中显示通过SMTP服务器发送的邮件;“发送”;邮箱,python,smtplib,imaplib,Python,Smtplib,Imaplib,我可以使用SMTP服务器发送邮件: self.smtp_connection.sendmail( 'my_smtp_username@provider.com', recipients, # list of To, Cc and Bcc mails mime_message.as_string() ) 但是我看不到“我的smtp”中“已发送”框中发送的邮件_username@provider.com'IMAP帐户。如何使邮件在此框中可见?SMTP和IMAP协议是两种不

我可以使用SMTP服务器发送邮件:

self.smtp_connection.sendmail(
    'my_smtp_username@provider.com',
    recipients,  # list of To, Cc and Bcc mails
    mime_message.as_string()
)

但是我看不到“我的smtp”中“已发送”框中发送的邮件_username@provider.com'IMAP帐户。如何使邮件在此框中可见?

SMTP和IMAP协议是两种不同的协议:通过SMTP服务器发送的邮件在IMAP服务器上不可见

因此,您需要自己模拟这种行为,将邮件发送到您自己的地址(但不将您添加到MIME对象的“收件人:”标题中),然后将邮件移动到正确的框中:

使用API抽象,其中
smtp\u连接
imap\u连接
被正确初始化,使用相同的帐户,名为
self.email\u帐户

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

SMTP和IMAP协议是两个不同的东西:通过SMTP服务器发送的邮件在IMAP服务器上不可见

因此,您需要自己模拟这种行为,将邮件发送到您自己的地址(但不将您添加到MIME对象的“收件人:”标题中),然后将邮件移动到正确的框中:

使用API抽象,其中
smtp\u连接
imap\u连接
被正确初始化,使用相同的帐户,名为
self.email\u帐户

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