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
#1
Hi Team,

I am trying to write a code which would download the TLS Report messages and then extract messages, unzip it and store it in file. Since I am not pro I happen to write few lines but the attachments are not getting downloaded.
The code then says it would search for the messages when matching content found in BODY but those are throwing exception.
Can someone please help me here?

import imaplib
import email
import json
import os

# IMAP server credentials
IMAP_SERVER = 'imap.xxxx.net'
EMAIL_ADDRESS = '[email protected]'
PASSWORD = 'XXXXXXX'

# Directory to save JSON files
SAVE_DIR = '/tmp/tls'

def connect_to_imap_server():
    # Connect to the IMAP server
    mail = imaplib.IMAP4_SSL(IMAP_SERVER)
    mail.login(EMAIL_ADDRESS, PASSWORD)
    return mail

def download_tls_rpt_reports():
    # Create directory if it doesn't exist
    if not os.path.exists(SAVE_DIR):
        os.makedirs(SAVE_DIR)

    mail = connect_to_imap_server()
    mail.select("inbox")

    # Search for emails containing TLS-RPT reports
    result, data = mail.search(None, 'SUBJECT', 'Report')
    for num in data[0].split():
        result, data = mail.fetch(num, '(RFC822)')
        raw_email = data[0][1]
        msg = email.message_from_bytes(raw_email)

        # Extract JSON attachment (if any)
        for part in msg.walk():
            if part.get_content_type() == "application/json":
                filename = part.get_filename()
                if filename:
                    with open(os.path.join(SAVE_DIR, filename), 'wb') as f:
                        f.write(part.get_payload(decode=True))
                        print(f"Saved TLS-RPT report: {filename}")

    mail.close()
    mail.logout()

if __name__ == "__main__":
    download_tls_rpt_reports()
TIA
Blason R
Reply


Messages In This Thread
Unable to download TLS Report attachment - by blason16 - Feb-21-2024, 05:48 PM

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