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.
143 lines
4.3 KiB
143 lines
4.3 KiB
import imaplib, email, os, json |
|
from config import server, username, pw, searchAll, searchYesterday |
|
|
|
from smtplib import SMTP_SSL, SMTP_SSL_PORT |
|
|
|
|
|
|
|
def sendEmails(text, reciever): |
|
SMTP_HOST = server |
|
SMTP_USER = username |
|
SMTP_PASS = pw |
|
|
|
# Craft the email by hand |
|
from_email = 'Maily-Sommerschein <bookin@sommerschein.de>' # or simply the email address |
|
to_emails = reciever |
|
body = text |
|
headers = f"From: {from_email}\r\n" |
|
headers += f"To: {', '.join(to_emails)}\r\n" |
|
headers += f"Subject: Autoresponse\r\n" |
|
email_message = headers + "\r\n" + body # Blank line needed between headers and body |
|
|
|
# Connect, authenticate, and send mail |
|
smtp_server = SMTP_SSL(SMTP_HOST, port=SMTP_SSL_PORT) |
|
smtp_server.set_debuglevel(1) # Show SMTP server interactions |
|
smtp_server.login(SMTP_USER, SMTP_PASS) |
|
smtp_server.sendmail(from_email, to_emails, email_message) |
|
|
|
# Disconnect |
|
smtp_server.quit() |
|
|
|
def loadKnownContacts(): |
|
f = open('contacts.json') |
|
contacts = json.load(f) |
|
return contacts |
|
|
|
def getContacts(search): |
|
# 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, search) |
|
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) |
|
|
|
newContacts = [] |
|
|
|
for c in contacts: |
|
contact = c.split('<') |
|
if len(contact) == 2: |
|
contact = contact[1].split('>') |
|
#contact = contact[0].split('(') |
|
newContacts.append(contact[0]) |
|
#print(contact[0]) |
|
else: |
|
contact = contact[0].split('(') |
|
#print(contact[0]) |
|
newContacts.append(contact[0]) |
|
|
|
|
|
return newContacts |
|
|
|
def jsonAddContacts(contacts): |
|
knowContacts = loadKnownContacts() |
|
for c in contacts: |
|
knowContacts.append(c) |
|
|
|
if os.path.isfile('./contacts.json'): |
|
print('Deleting Contacts') |
|
os.remove('contacts.json') |
|
|
|
with open('contacts.json', 'w', encoding='utf-8') as f: |
|
json.dump(knowContacts, f, ensure_ascii=False, indent=4) |
|
|
|
def jsonCreateContacts(): |
|
# 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, searchAll) |
|
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) |
|
|
|
newContacts = [] |
|
rawContacts = [] |
|
|
|
for c in contacts: |
|
rawContacts.append(c) |
|
contact = c.split('<') |
|
if len(contact) == 2: |
|
contact = contact[1].split('>') |
|
#contact = contact[0].split('(') |
|
newContacts.append(contact[0]) |
|
#print(contact[0]) |
|
else: |
|
contact = contact[0].split('(') |
|
#print(contact[0]) |
|
newContacts.append(contact[0]) |
|
|
|
if os.path.isfile('./contacts.json'): |
|
print('Deleting Contacts') |
|
os.remove('contacts.json') |
|
|
|
|
|
if os.path.isfile('./rawContacts.json'): |
|
print('Deleting Contacts') |
|
os.remove('rawContacts.json') |
|
|
|
with open('contacts.json', 'w', encoding='utf-8') as f: |
|
json.dump(newContacts, f, ensure_ascii=False, indent=4) |
|
|
|
with open('rawContacts.json', 'w', encoding='utf-8') as f: |
|
json.dump(rawContacts, f, ensure_ascii=False, indent=4) |
|
|
|
def checkFor_unkownContacts(): |
|
contacts = getContacts(searchYesterday) |
|
knownContacts = loadKnownContacts() |
|
unknownContacts = [] |
|
for c in contacts: |
|
if c not in knownContacts: |
|
# TODO: send Email |
|
#print(c) |
|
unknownContacts.append(c) |
|
return unknownContacts
|
|
|