【问题标题】:Python - Forward IMAP Email with Attachments (imaplib, smtplib)Python - 转发带附件的 IMAP 电子邮件(imaplib、smtplib)
【发布时间】:2011-07-31 03:56:52
【问题描述】:

我在转发带有附件的邮件时遇到问题。谁能指出我正确的方向?我猜它在 Fetch 语句中,但我不确定。

import sys
import uuid
import re
import smtplib
import email

address = ''
username = ''
password = ''

def checkMail():
    M = imaplib.IMAP4_SSL(address)
    M.login(username, password)
    M.select("INBOX", readonly=True)
    typ, data = M.search(None, 'Unseen')

    messageCount = len(data[0].split())    
    print('messageCount: %', messageCount)
    if messageCount > 0:     
        for num in data[0].split():
            typ, data = M.fetch(num, '(BODY[TEXT])')
            foundAt = data[0][1].find('Content-Type: application')
            if(foundAt > 0):
                print('attachmentfound')
                sendMail(data[0][1])


    M.close()
    M.logout()       


def sendMail(raw_message):

    toAddress = ''
    fromAddress = ''
    LOGIN    = ''
    PASSWORD = ''


    server = smtplib.SMTP('', 587)
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login(LOGIN, PASSWORD)
    server.sendmail(fromAddress, toAddress, raw_message)
    server.quit()

def main():
    checkMail()

main()

【问题讨论】:

  • 你可能想看看IMAPClient(减轻了很多使用 imaplib 的挫败感)

标签: python email


【解决方案1】:

如果您想使用 imaplib,为什么不直接获取整个消息 (RFC822)?请参阅这个类似的 SO 问题:

How to fetch an email body using imaplib in python?

【讨论】:

    【解决方案2】:

    这是 fetch...为 (BODY[]) 添加 fetch 就可以了。

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 2011-02-12
      • 1970-01-01
      • 2021-04-26
      • 2017-04-04
      • 2021-08-19
      • 2022-08-03
      • 2015-02-21
      • 1970-01-01
      相关资源
      最近更新 更多