vigil/services/mail.py

41 lines
1.2 KiB
Python

# _ __________________
# | | / / _/ ____/ _/ /
# | | / // // / __ / // /
# | |/ // // /_/ // // /___
# |___/___/\____/___/_____/
# © Uthmn 2025 under MIT license
import smtplib
from email.mime.text import MIMEText
from dotenv import load_dotenv
from os import getenv
from os.path import dirname, join
# Load environment variables from .env file
dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)
# SMTP server settings
SMTP_SERVER = getenv("SMTP_SERVER")
SMTP_PORT = int(getenv("SMTP_PORT"))
SMTP_USERNAME = getenv("SMTP_USERNAME")
SMTP_PASSWORD = getenv("SMTP_PASSWORD")
def send_email(receiver_email, subject, body):
sender_email = SMTP_USERNAME
# Create the email
message = MIMEText(body, "html")
message["Subject"] = subject
message["From"] = sender_email
message["To"] = receiver_email
# Connect using SSL
try:
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(sender_email, receiver_email, message.as_string())
print("HTML email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")