Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
EliotAmn committed Jan 31, 2025
2 parents c646309 + 2ec2516 commit 188389a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 47 deletions.
68 changes: 26 additions & 42 deletions app/services/mail_service.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
import os
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from mailersend import emails

from app.tools.teklogger import log_debug, log_error, log_info, log_success


class MailService:


@staticmethod
def _connect_and_authenticate():
"""
Connect to the SMTP server and authenticate using the provided credentials.
"""
smtp_host = os.getenv("SMTP_HOST")
smtp_port = int(os.getenv("SMTP_PORT", 587))
smtp_user = os.getenv("SMTP_USER")
smtp_password = os.getenv("SMTP_PASSWORD")
try:
server = smtplib.SMTP(smtp_host, smtp_port)
#server.starttls()
server.login(smtp_user, smtp_password)
log_debug(
f"Connected and authenticated with SMTP server: {smtp_host}")
return server
except Exception as e:
log_error(f"Failed to connect/authenticate to SMTP server: {e}")
raise

@staticmethod
def send_mail(recipient: str, subject: str, body: str):
"""
Expand All @@ -41,23 +18,30 @@ def send_mail(recipient: str, subject: str, body: str):

log_info(f"Sending mail to {recipient}")

# Create a multipart message
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = "Support LOGI-IT <[email protected]>"
message["To"] = f"{recipient} <{recipient}>"

# Add HTML content
part = MIMEText(body, "plain")
message.attach(part)
# Convert message to string
message_str = message.as_string()
# Send the email
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL(os.getenv("SMTP_HOST"), int(os.getenv("SMTP_PORT")), context=context) as server:
server.login(os.getenv("SMTP_USER"), os.getenv("SMTP_PASSWORD"))
server.sendmail(os.getenv("SMTP_USER"), recipient, message_str)
log_success(f"Mail successfully sent to {recipient}")
mailer = emails.NewEmail(os.getenv("MAILERSEND_API_KEY"))

mail_body = {}

mail_from = {
"name": "TekBetter Dashboard",
"email": os.getenv("MAILERSEND_FROM_EMAIL"),
}

recipients = [
{
"email": recipient,
}
]

mailer.set_mail_from(mail_from, mail_body)
mailer.set_mail_to(recipients, mail_body)
mailer.set_subject(subject, mail_body)
mailer.set_plaintext_content(body, mail_body)

response = mailer.send(mail_body)
log_debug(response)
log_success(f"Mail sent to {recipient}")

except Exception as e:
log_error(f"Failed to send mail to {recipient}: {e}")
8 changes: 3 additions & 5 deletions app/tools/envloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
"JWT_SECRET": None,
"AES_KEY": None,
"DATA_PATH": "./data",
"SMTP_HOST": None,
"SMTP_PORT": 587,
"SMTP_USER": None,
"SMTP_PASSWORD": None,
"ENABLE_MAILER": "false",
"MAILERSEND_API_KEY": None,
"MAILERSEND_FROM_EMAIL": None
}

def displ_deprecated(message):
Expand All @@ -43,7 +41,7 @@ def load_env():
log_debug("Loading environment variables")
load_dotenv()

smtp_vars = ["SMTP_HOST", "SMTP_USER", "SMTP_PASSWORD"]
smtp_vars = ["MAILERSEND_API_KEY", "MAILERSEND_FROM_EMAIL"]
if os.getenv("ENABLE_MAILER") == "true":
for var in smtp_vars:
if os.getenv(var) is None:
Expand Down

0 comments on commit 188389a

Please sign in to comment.