Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import imaplib
- import email
- from PyPDF2 import PdfFileMerger
- import shutil
- import os
- # Email account credentials
- username = '[email protected]'
- password = 'your_password'
- # Connect to Yahoo IMAP server
- imap_server = 'imap.mail.yahoo.com'
- imap_port = 993
- # Establish connection
- mail = imaplib.IMAP4_SSL(imap_server, imap_port)
- mail.login(username, password)
- # Select the "Receipts" subfolder
- mail.select('Receipts')
- # Search for unread emails with attachments
- result, data = mail.search(None, '(UNSEEN)', 'HAS attachment')
- attachments_to_print = [] # List to store filenames of attachments to print
- unread_email_nums = data[0].split()
- # Iterate through unread emails with attachments
- for num in unread_email_nums:
- # Fetch email details
- _, msg_data = mail.fetch(num, '(RFC822)')
- _, msg_bytes = msg_data[0]
- msg = email.message_from_bytes(msg_bytes)
- # Check if email has two attachments
- if len(msg.get_payload()) == 2:
- # Iterate through attachments
- for part in msg.walk():
- if part.get_content_type() == 'application/pdf':
- # Identify attachment with "Receipt" in filename
- if 'receipt' in part.get_filename().lower():
- # Save attachment to a file
- filename = part.get_filename()
- with open(filename, 'wb') as f:
- f.write(part.get_payload(decode=True))
- attachments_to_print.append(filename)
- # Mark the email as read
- mail.store(num, '+FLAGS', '\\Seen')
- # Close the connection
- mail.logout()
- # Merge attachments into a single PDF file
- output_filename = 'receipts.pdf'
- pdf_merger = PdfFileMerger()
- for attachment in attachments_to_print:
- pdf_merger.append(attachment)
- pdf_merger.write(output_filename)
- pdf_merger.close()
- # Open the merged PDF in Safari
- shutil.move(output_filename, 'receipts.pdf')
- os.system('open -a Safari receipts.pdf')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement