Added env error handling
This commit is contained in:
parent
14bd208378
commit
cffddf97fe
@ -13,7 +13,12 @@ from email import message_from_bytes
|
|||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from os import getenv
|
from os import getenv
|
||||||
from os.path import dirname, join
|
from os.path import dirname, join, exists
|
||||||
|
|
||||||
|
# Exit if .env does not exist
|
||||||
|
if not exists(join(dirname(__file__), "../.env")):
|
||||||
|
print("Please create a .env file in the root directory.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
# Load environment variables from .env file
|
# Load environment variables from .env file
|
||||||
dotenv_path = join(dirname(__file__), "../.env")
|
dotenv_path = join(dirname(__file__), "../.env")
|
||||||
@ -21,16 +26,24 @@ load_dotenv(dotenv_path)
|
|||||||
|
|
||||||
# SMTP server settings
|
# SMTP server settings
|
||||||
SMTP_SERVER = getenv("SMTP_SERVER")
|
SMTP_SERVER = getenv("SMTP_SERVER")
|
||||||
SMTP_PORT = int(getenv("SMTP_PORT"))
|
SMTP_PORT = getenv("SMTP_PORT")
|
||||||
SMTP_USERNAME = getenv("SMTP_USERNAME")
|
SMTP_USERNAME = getenv("SMTP_USERNAME")
|
||||||
SMTP_PASSWORD = getenv("SMTP_PASSWORD")
|
SMTP_PASSWORD = getenv("SMTP_PASSWORD")
|
||||||
|
|
||||||
# IMAP server settings
|
# IMAP server settings
|
||||||
IMAP_SERVER = getenv("IMAP_SERVER")
|
IMAP_SERVER = getenv("IMAP_SERVER")
|
||||||
IMAP_PORT = int(getenv("IMAP_PORT"))
|
IMAP_PORT = getenv("IMAP_PORT")
|
||||||
IMAP_USERNAME = getenv("IMAP_USERNAME")
|
IMAP_USERNAME = getenv("IMAP_USERNAME")
|
||||||
IMAP_PASSWORD = getenv("IMAP_PASSWORD")
|
IMAP_PASSWORD = getenv("IMAP_PASSWORD")
|
||||||
|
|
||||||
|
# Check if all environment variables are set
|
||||||
|
if not SMTP_SERVER or not SMTP_PORT or not SMTP_USERNAME or not SMTP_PASSWORD or not IMAP_SERVER or not IMAP_PORT or not IMAP_USERNAME or not IMAP_PASSWORD:
|
||||||
|
print("Please set all environment variables in .env file.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
SMTP_PORT = int(SMTP_PORT)
|
||||||
|
IMAP_PORT = int(IMAP_PORT)
|
||||||
|
|
||||||
def send_email(receiver_emails, subject, body):
|
def send_email(receiver_emails, subject, body):
|
||||||
sender_email = SMTP_USERNAME
|
sender_email = SMTP_USERNAME
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user