You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
66 lines
2.2 KiB
2 years ago
|
import imaplib, email, os
|
||
|
from dotenv import load_dotenv
|
||
|
|
||
|
complexSearch1 = "UNANSWERED SENTSINCE 01-Sep-2022"
|
||
|
|
||
|
load_dotenv()
|
||
|
pw = os.getenv('PASSWORD')
|
||
|
server ="w00e1ce2.kasserver.com"
|
||
|
username ="booking@sommerschein.de"
|
||
|
|
||
|
# Connect to inbox
|
||
|
imap_server = imaplib.IMAP4_SSL(host=server)
|
||
|
imap_server.login(username, pw)
|
||
|
imap_server.select() # Default is `INBOX`
|
||
|
|
||
|
contacts = []
|
||
|
|
||
|
# Find all emails in inbox
|
||
|
_, message_numbers_raw = imap_server.search(None, complexSearch1)
|
||
|
for message_number in message_numbers_raw[0].split():
|
||
|
_, msg = imap_server.fetch(message_number, '(RFC822)')
|
||
|
|
||
|
# Parse the raw email message in to a convenient object
|
||
|
message = email.message_from_bytes(msg[0][1])
|
||
|
contact = message['from'].lower()
|
||
|
if contact not in contacts:
|
||
|
contacts.append(contact)
|
||
|
"""
|
||
|
print('== Email message =====')
|
||
|
# print(message) # print FULL message
|
||
|
print('== Email details =====')
|
||
|
print(f'From: {message["from"]}')
|
||
|
|
||
|
|
||
|
print(f'To: {message["to"]}')
|
||
|
print(f'Cc: {message["cc"]}')
|
||
|
print(f'Bcc: {message["bcc"]}')
|
||
|
print(f'Urgency (1 highest 5 lowest): {message["x-priority"]}')
|
||
|
print(f'Object type: {type(message)}')
|
||
|
print(f'Content type: {message.get_content_type()}')
|
||
|
print(f'Content disposition: {message.get_content_disposition()}')
|
||
|
print(f'Multipart?: {message.is_multipart()}')
|
||
|
# If the message is multipart, it basically has multiple emails inside
|
||
|
# so you must extract each "submail" separately.
|
||
|
if message.is_multipart():
|
||
|
print('Multipart types:')
|
||
|
for part in message.walk():
|
||
|
print(f'- {part.get_content_type()}')
|
||
|
multipart_payload = message.get_payload()
|
||
|
for sub_message in multipart_payload:
|
||
|
# The actual text/HTML email contents, or attachment data
|
||
|
print(f'Payload\n{sub_message.get_payload()}')
|
||
|
else: # Not a multipart message, payload is simple string
|
||
|
print(f'Payload\n{message.get_payload()}')
|
||
|
# You could also use `message.iter_attachments()` to get attachments only
|
||
|
|
||
|
for c in contacts:
|
||
|
print(c)
|
||
|
"""
|
||
|
for c in contacts:
|
||
|
contact = c.split('<')
|
||
|
if len(contact) == 2:
|
||
|
contact = contact[1].split('>')
|
||
|
print(contact[0])
|
||
|
else:
|
||
|
print(contact[0])
|