-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
2 changed files
with
29 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
""" | ||
|
@@ -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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters