0

I recently posted Use python to download email attachments only based on Subject Answers from that post allowed me to see that the reason my script wasnt working to download the attachments was because there was HTML in the email that im trying to download the attachments from. However, im not sure how to get this script to work. It works fine on downloading attachments from plaintext emails, but wont work whenever there is HTML. Does anyone have any suggestions here, any issue in my code? Please refrain from posting other links to peoples codes, i have looked at a lot of them and tried implementing them, all seem to have a similar issue when there is HTML in the email. Here is a picture of the email my code is having trouble downloading the attachment from. Email my code is trying to get

 import imaplib
 import email

server = 'imap.gmail.com'
user = '*****@lhac.com'
password = '*******'
outputdir = 'C:\install files'
subject = 'testtttt' #subject line of the emails you want to download attachments from

def connect(server, user, password):
    m = imaplib.IMAP4_SSL(server)
    m.login(user, password)
    m.select()
    return m

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

downloadAttachments(subject)

Following is the error i get:

"C:/Users/gkidd/Documents/mailscript.py", line 24, in 
downloaAttachmentsInEmail open(outputdir + '/' + part.get_filename(), 'wb').write(part.get_payload(decode=True)) 
OSError: [Errno 22] Invalid argument: 'C:\\install files/testing_lhac.com__Advanced__lhac.com.csv
Adirmola
  • 783
  • 5
  • 15

1 Answers1

0
from imap_tools import MailBox

# get all attachments from INBOX and save them to files
with MailBox('imap.my.ru').login('acc', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch():
        for att in msg.attachments:
            print(att.filename, att.content_type)
            with open('C:/1/{}'.format(att.filename), 'wb') as f:
                f.write(att.payload)

https://github.com/ikvk/imap_tools

Vladimir
  • 6,162
  • 2
  • 32
  • 36