相关疑难解决方法(0)

使用imaplib下载多个附件

如何使用imaplib从单个邮件下载多个附件?

假设我有一封电子邮件,该电子邮件包含4个附件.如何下载所有这些附件?以下代码仅从电子邮件中下载单个附件.

detach_dir = 'c:/downloads'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login('hello@gmail.com','3323434')
m.select("[Gmail]/All Mail")

resp, items = m.search(None, "(UNSEEN)")
items = items[0].split()

for emailid in items:
    resp, data = m.fetch(emailid, "(RFC822)") 
    email_body = data[0][1] 
    mail = email.message_from_string(email_body) 
    temp = m.store(emailid,'+FLAGS', '\\Seen')
    m.expunge()

    if mail.get_content_maintype() != 'multipart':
        continue

    print "["+mail["From"]+"] :" + mail["Subject"]

    for part in mail.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue

        filename = part.get_filename()
        att_path = os.path.join(detach_dir, filename)

        if not os.path.isfile(att_path) :
            fp = open(att_path, 'wb') …
Run Code Online (Sandbox Code Playgroud)

python email imap attachment

23
推荐指数
4
解决办法
3万
查看次数

如何仅从特定Gmail邮件标签下载未读附件?

我有一个Python脚本改编自下载使用Python发送到Gmail的MMS电子邮件

import email, getpass, imaplib, os

detach_dir = '.' # directory where to save attachments (default: current)
user = raw_input("Enter your GMail username:")
pwd = getpass.getpass("Enter your password: ")

# connecting to the gmail imap server
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)
m.select("[Gmail]/All Mail") # here you a can choose a mail box like INBOX instead
# use m.list() to get all the mailboxes

resp, items = m.search(None, 'FROM', '"Impact Stats Script"') # you could filter using the IMAP rules here …
Run Code Online (Sandbox Code Playgroud)

python gmail attachment imaplib

5
推荐指数
1
解决办法
9529
查看次数

标签 统计

attachment ×2

python ×2

email ×1

gmail ×1

imap ×1

imaplib ×1