使用python从邮件下载附件 [英] Download attachment from mail using python
本文介绍了使用python从邮件下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多封包含附件的电子邮件。我想下载未读电子邮件的附件,并带有特定主题行。
I have multiple emails that contain an attachment. I would like to download the attachment for unread emails and with a specific subject line.
例如,我收到一封电子邮件,主题为 EXAMPLE,并包含附件。因此,在下面的代码中,
会是怎样的,我尝试了一下,但是没有用
这是Python代码
for example, I got an email that has a subject "EXAMPLE" and contains an attachment. So how it would be Below code, I tried but it is not working" it's a Python code
#Subject line can be "EXAMPLE"
for subject_line in lst_subject_line:
# typ, msgs = conn.search(None,'(UNSEEN SUBJECT "' + subject_line + '")')
typ, msgs = conn.search(None,'("UNSEEN")')
msgs = msgs[0].split()
print(msgs)
outputdir = "C:/Private/Python/Python/Source/Mail Reader"
for email_id in msgs:
download_attachments_in_email(conn, email_id, outputdir)
谢谢
推荐答案
我能找到的大多数答案已经过时了。 />
这是一个Python(> = 3.6)脚本,用于从Gmail帐户下载附件。请确保选中下面的过滤器选项。
Most answers I could find were outdated.
Here's a python (>=3.6) script to download attachments from a Gmail account. Make sure you check the filter options below.
import os
from imbox import Imbox # pip install imbox
import traceback
# enable less secure apps on your google account
# https://myaccount.google.com/lesssecureapps
host = "imap.gmail.com"
username = "username"
password = 'password'
download_folder = "/path/to/download/folder"
if not os.path.isdir(download_folder):
os.makedirs(download_folder, exist_ok=True)
mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
messages = mail.messages() # defaults to inbox
for (uid, message) in messages:
mail.mark_seen(uid) # optional, mark message as read
for idx, attachment in enumerate(message.attachments):
try:
att_fn = attachment.get('filename')
download_path = f"{download_folder}/{att_fn}"
print(download_path)
with open(download_path, "wb") as fp:
fp.write(attachment.get('content').read())
except:
pass
print(traceback.print_exc())
mail.logout()
"""
Available Message filters:
# Gets all messages from the inbox
messages = mail.messages()
# Unread messages
messages = mail.messages(unread=True)
# Flagged messages
messages = mail.messages(flagged=True)
# Un-flagged messages
messages = mail.messages(unflagged=True)
# Flagged messages
messages = mail.messages(flagged=True)
# Un-flagged messages
messages = mail.messages(unflagged=True)
# Messages sent FROM
messages = mail.messages(sent_from='sender@example.org')
# Messages sent TO
messages = mail.messages(sent_to='receiver@example.org')
# Messages received before specific date
messages = mail.messages(date__lt=datetime.date(2018, 7, 31))
# Messages received after specific date
messages = mail.messages(date__gt=datetime.date(2018, 7, 30))
# Messages received on a specific date
messages = mail.messages(date__on=datetime.date(2018, 7, 30))
# Messages whose subjects contain a string
messages = mail.messages(subject='Christmas')
# Messages from a specific folder
messages = mail.messages(folder='Social')
"""
这篇关于使用python从邮件下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文