2022-08-31 04:28:00 +00:00
|
|
|
import os
|
|
|
|
import imaplib
|
2023-01-08 15:11:40 +00:00
|
|
|
import json
|
2023-01-09 04:10:07 +00:00
|
|
|
from time import strftime
|
|
|
|
import influxdb_client
|
|
|
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
|
|
|
from dotenv import load_dotenv, dotenv_values
|
2022-08-31 04:28:00 +00:00
|
|
|
|
|
|
|
load_dotenv()
|
2023-01-09 04:10:07 +00:00
|
|
|
config = dotenv_values(".env")
|
2022-08-31 04:28:00 +00:00
|
|
|
|
2023-01-08 15:11:40 +00:00
|
|
|
EMAIL_ACCOUNTS = json.loads(os.environ.get("MAIL_ACCOUNTS"))
|
|
|
|
EMAIL_PASSWORDS = json.loads(os.environ.get("MAIL_PASSWORDS"))
|
|
|
|
# using zip()
|
|
|
|
# to convert lists to dictionary
|
|
|
|
accounts = dict(zip(EMAIL_ACCOUNTS, EMAIL_PASSWORDS))
|
|
|
|
print(accounts)
|
2023-01-09 04:10:07 +00:00
|
|
|
|
|
|
|
# Influx DB
|
|
|
|
client = influxdb_client.InfluxDBClient(
|
|
|
|
url=config['DB_URL'],
|
|
|
|
token=config['DB_TOKEN'],
|
|
|
|
org=config['DB_ORG']
|
|
|
|
)
|
|
|
|
|
2022-08-31 04:28:00 +00:00
|
|
|
################ IMAP SSL ##############################
|
|
|
|
|
2023-01-08 15:11:40 +00:00
|
|
|
for account in accounts:
|
|
|
|
with imaplib.IMAP4_SSL(host="imap.gmail.com", port=imaplib.IMAP4_SSL_PORT) as imap_ssl:
|
|
|
|
print("Connection Object : {}".format(imap_ssl))
|
|
|
|
print("Logging into mailbox...")
|
|
|
|
try:
|
|
|
|
resp_code, response = imap_ssl.login(account, accounts[account])
|
|
|
|
except Exception as e:
|
|
|
|
print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
|
|
|
|
resp_code, response = None, None
|
2022-08-31 04:28:00 +00:00
|
|
|
|
2023-01-08 15:11:40 +00:00
|
|
|
print("Response Code : {}".format(resp_code))
|
|
|
|
print("Response : {}\n".format(response[0].decode()))
|
2022-08-31 04:28:00 +00:00
|
|
|
|
2023-01-08 15:11:40 +00:00
|
|
|
imap_ssl.select('inbox')
|
|
|
|
resp_code, messages = imap_ssl.search(None, 'UnSeen')
|
|
|
|
if resp_code == 'OK':
|
|
|
|
if len(messages[0].split()) > 0:
|
2023-01-09 04:10:07 +00:00
|
|
|
# print('True')
|
|
|
|
# print(messages[0].split())
|
2023-01-08 19:13:55 +00:00
|
|
|
unread_messages = len(messages[0].split())
|
2023-01-08 15:11:40 +00:00
|
|
|
else:
|
|
|
|
print('False')
|
2022-08-31 04:28:00 +00:00
|
|
|
|
2023-01-08 15:11:40 +00:00
|
|
|
############### Logout of Mailbox ######################
|
|
|
|
print("\nLogging Out....")
|
2022-08-31 04:28:00 +00:00
|
|
|
|
2023-01-08 19:13:55 +00:00
|
|
|
imap_ssl.close()
|
|
|
|
if unread_messages:
|
|
|
|
message = f"{account} has {unread_messages} messages."
|
2023-01-09 04:10:07 +00:00
|
|
|
date = strftime("%Y-%m-%d")
|
|
|
|
time = strftime("%H:%M:%S")
|
|
|
|
timestamp = date + "T" + time + "Z"
|
|
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
|
|
|
point = influxdb_client.Point("unread_emails").time(timestamp).tag("account", account)\
|
|
|
|
.field("count", unread_messages)
|
|
|
|
write_api.write(bucket="personal_data",
|
|
|
|
org=config['DB_ORG'], record=point)
|
2023-01-08 19:13:55 +00:00
|
|
|
print(message)
|