Python Forum
Unable to download TLS Report attachment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to download TLS Report attachment
#5
This works for me, downloads a small json file example3.json but it should collect any file, I think!

Probably better to only get files from people you know, or you might pick up some nasty stuff!

Strangely, when I do this step by step, I have to use:

Quote:typ, messageParts = imapSession.fetch(msgId[0], '(RFC822)') # msgId is a list

But, within myApp() this works fine! Can't figure that out!

Quote:typ, messageParts = imapSession.fetch(msgId, '(RFC822)')

import email
import imaplib
import os

savepath = '/home/pedro/myPython/email_stuff/'
imap_server = "imap.yourmail.com"
sender_of_interest = '[email protected]'
email_address = '[email protected]'
password = 'topsecret'
label = 'INBOX'

# make a folder to hold the files
if 'email_attachments' not in os.listdir(savepath):
    os.mkdir(savepath + 'email_attachments')

def myApp():
    try:
        imapSession = imaplib.IMAP4_SSL(imap_server)
        typ, accountDetails = imapSession.login(email_address, password)
        imapSession.select(label)
        typ, data = imapSession.search(None, 'UNSEEN', f'FROM {sender_of_interest}')
        print(typ, data)
        print('Search...')
        for msgId in data[0].split(): # msgId is a list I need msgId[0] when I do this step by step           
            typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
            emailBody = messageParts[0][1]
            raw_email_string = emailBody.decode('utf-8')
            mail = email.message_from_string(raw_email_string)
            print('got the whole email body as a string...')
            for part in mail.walk():
                if part.get_content_maintype() == 'multipart':
                    print(part.as_string())
                    continue
                if part.get('Content-Disposition') is None:
                    print(f'This part has Content-Disposition: {part.as_string()}')
                    continue
                fileName = part.get_filename()
                print(f'file name {fileName} being processed ...')
                if bool(fileName):
                    filePath = os.path.join(savepath, 'email_attachments', fileName)
                    # don't overwrite if file exists can change this behaviour if wanted
                    if not os.path.isfile(filePath):
                        print(f'the file is {fileName}')
                        fp = open(filePath, 'wb')
                        fp.write(part.get_payload(decode=True))
                        fp.close()
                        print('fp closed ...')
        imapSession.close()
        imapSession.logout()
    except:
        print('Not able to download all attachments.')
Hope it works for you!

This does not mark the emails as SEEN, but you can add that if you wish.

For testing, I kept this mail as unread.
Reply


Messages In This Thread
RE: Unable to download TLS Report attachment - by Pedroski55 - Feb-25-2024, 09:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with Allure report Nik1811 0 294 Apr-22-2024, 09:59 AM
Last Post: Nik1811
  Extract PDF Attachment from Gmail jstaffon 0 683 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  Allure Report Generation rotemz 0 897 Jan-24-2023, 08:30 PM
Last Post: rotemz
  Right to left alignment in python report using Reportlab jalal0034 1 2,017 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  I get attachment paperclip on email without any attachments monika_v 5 2,220 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  Trying to determine attachment file type before saving off.. cubangt 1 2,326 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,247 Jan-07-2022, 07:38 PM
Last Post: cubangt
  download with internet download manager coral_raha 0 3,150 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  Clicking Every Page and Attachment on Website shoulddt 1 1,843 Jul-09-2021, 01:08 PM
Last Post: snippsat
  Print Report Invoice nio74maz 0 1,634 Jun-17-2021, 08:25 AM
Last Post: nio74maz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020