簡體   English   中英

python中的腳本下載電子郵件附件

[英]Script in python to download email attachments

我有以下腳本:

import imaplib
import email
import os

svdir = 'c:/downloads'

mail = imaplib.IMAP4('https://outlook.office365.com/mapi/emsmdb/?'
                     'MailboxId=6ebd46ee-55df-452e-a0af-44e7b248d992@jmawireless.com')
mail.login("RNa", "RN!")
mail.select("ADP Files")

typ, msgs = mail.search(None, '(SUBJECT "ADP Files")')
msgs = msgs[0].split()

for emailid in msgs:
    resp, data = mail.fetch(emailid, "(RFC822)")
    email_body = data[0][1]
    m = email.message_from_string(email_body)

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

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

        filename = part.get_filename()
        if filename is not None:
            sv_path = os.path.join(svdir, filename)
            if not os.path.isfile(sv_path):
                print(sv_path)
                fp = open(sv_path, 'wb')
                fp.write(part.get_payload(decode=True))
                fp.close()

產生的錯誤是:

C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP>email.py

Traceback (most recent call last):
    File "C:\Python34\lib\encodings\idna.py", line 165, in encode
    raise UnicodeError("label empty or too long") UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
    File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 2, in <module>
        import email
    File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 8, in <module>
        mail=imaplib.IMAP4( 'https://outlook.office365.com/mapi/emsmdb/?MailboxId=6ebd46ee-55df-452e-a0af-44e7b248d992@jmawireless.com')
    File "C:\Python34\lib\imaplib.py", line 181, in __init__
        self.open(host, port)
    File "C:\Python34\lib\imaplib.py", line 257, in open
        self.sock = self._create_socket()
    File "C:\Python34\lib\imaplib.py", line 247, in _create_socket
        return socket.create_connection((self.host, self.port))
    File "C:\Python34\lib\socket.py", line 494, in create_connection
        for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
        for res in _socket.getaddrinfo(host, port, family, type, proto, flags): UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or to o long)

我不知道為什么會產生這個錯誤。 我對這種腳本很陌生。

根據文檔(以及源代碼),構造函數imaplib.IMAP4()接受兩個參數: host :主機名(通常是服務器名稱或 IP 地址),默認為“localhost”,以及port :端口號,默認為標准 IMAP4 端口)。

在這里,您使用的是錯誤的 URL。

嘗試打開這樣的連接:

with imaplib.IMAP4("outlook.office365.com") as imap4:
    ...

請參閱此頁面以獲取 Office 360​​ 的連接設置: Outlook Office 365 for business 的 POP 和 IMAP 設置

Outlook Office 365 可能需要 SSL 連接。這是一個更有用的用法示例:

import imaplib

username = 'user@example.com'
password = 'password'

with imaplib.IMAP4_SSL('outlook.office365.com') as imap4:
    imap4.login(username, password)

    # retrieve a list of the mailboxes and select one
    result, mailboxes = imap4.list()
    imap4.select("inbox")

編輯:沒有with語句的替代方案

try:
    imap4.login(username, password)

    # retrieve a list of the mailboxes and select one
    result, mailboxes = imap4.list()
    imap4.select("inbox")

finally:
    imap4.close()
import imaplib
import email
import os

server = 'outlook.office365.com'
user = 'YOUR USERNAME'
password = 'YOUR PASSWORD'
outputdir = 'DIRECTORY THAT YOU WANT FILES DOWNLOADED TO'
subject = 'Data Exports' #subject line of the emails you want to download attachments from

# connects to email client through IMAP
def connect(server, user, password):
    m = imaplib.IMAP4_SSL(server)
    m.login(user, password)
    m.select()
    return m

# downloads attachment for an email id, which is a unique identifier for an
# email, which is obtained through the msg object in imaplib, see below 
# subjectQuery function. 'emailid' is a variable in the msg object class.

def downloaAttachmentsInEmail(m, emailid, outputdir):
    resp, data = m.fetch(emailid, "(BODY.PEEK[])")
    email_body = data[0][1]
    mail = email.message_from_bytes(email_body)
    if mail.get_content_maintype() != 'multipart':
        return
    for part in mail.walk():
        if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None:
            open(outputdir + '/' + part.get_filename(), 'wb').write(part.get_payload(decode=True))

# download attachments from all emails with a specified subject line
# as touched upon above, a search query is executed with a subject filter,
# a list of msg objects are returned in msgs, and then looped through to 
# obtain the emailid variable, which is then passed through to the above 
# downloadAttachmentsinEmail function

def subjectQuery(subject):
    m = connect(server, user, password)
    m.select("Inbox")
    typ, msgs = m.search(None, '(SUBJECT "' + subject + '")')
    msgs = msgs[0].split()
    for emailid in msgs:
        downloaAttachmentsInEmail(m, emailid, outputdir)

subjectQuery(subject)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM