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
#6
I modified your code a little, you seem to have repeated variable names which may have caused confusion.

I wrote Report in the email Subject field.

This still doesn't mark mails as SEEN, also it overwrites a file with the same name without hesitation.

Works for me!

import imaplib
import email
import os
 
# IMAP server credentials
savepath = '/home/pedro/myPython/email_stuff/'
imap_server = "imap.yourmail.com"
sender_of_interest = '[email protected]'
email_address = '[email protected]'
password = 'topsecret'
label = 'INBOX'
subject = 'Report'

# make a folder to hold the files
if 'email_attachments' not in os.listdir(savepath):
    os.mkdir(savepath + 'email_attachments')
 
def connect_to_imap_server(s, e, p):
    # Connect to the IMAP server
    mail = imaplib.IMAP4_SSL(s)
    mail.login(e, p)
    return mail
 
def download_tls_rpt_reports():
    M = connect_to_imap_server(imap_server, email_address, password )
    M.select(label) 
    # Search for emails containing TLS-RPT reports
    #typ, data = imapSession.search(None, 'UNSEEN', f'FROM {sender_of_interest}')
    result, data = M.search(None, 'UNSEEN', f'SUBJECT {subject}')
    for num in data[0].split():
        status, messageParts = M.fetch(num, '(RFC822)')
        emailBody = messageParts[0][1]
        raw_email_string = emailBody.decode('utf-8')
        msg = email.message_from_string(raw_email_string) 
        # Extract JSON attachment (if any)
        for part in msg.walk():
            if part.get_content_type() == "application/json":
                filename = part.get_filename()
                if filename:
                    filePath = os.path.join(savepath, 'email_attachments', filename)
                    with open(filePath, 'wb') as f:
                        f.write(part.get_payload(decode=True))
                        print(f"Saved TLS-RPT report: {filePath}") 
    M.close()
    M.logout()
 
if __name__ == "__main__":
    download_tls_rpt_reports()
Output:

Output:
download_tls_rpt_reports() Saved TLS-RPT report: /home/pedro/myPython/email_stuff/email_attachments/example2.json
Reply


Messages In This Thread
RE: Unable to download TLS Report attachment - by Pedroski55 - Feb-25-2024, 06:11 PM

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