Adding basic login support

This commit is contained in:
Jon Herron 2023-06-29 15:36:30 -04:00
parent 862be7e69f
commit 680a8f408d
1 changed files with 8 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import json
from email.message import EmailMessage
from smtplib import SMTP
from typing import Optional
class SendEmail:
def __init__(self,
@ -11,9 +12,13 @@ class SendEmail:
email_body: str,
email_to: str,
email_from: str,
smtp_user: Optional[str] = None,
smtp_password: Optional[str] = None,
):
self.smtp_host = smtp_host
self.smtp_port = smtp_port
self.smtp_user = smtp_user
self.smtp_password = smtp_password
self.email_subject = email_subject
self.email_body = email_body
self.email_to = email_to
@ -27,9 +32,12 @@ class SendEmail:
message["To"] = self.email_to
response = {}
should_login = self.smtp_user and self.smtp_password
try:
with SMTP(self.smtp_host, self.smtp_port) as smtp:
if should_login:
smtp.login(self.smtp_user, self.smtp_password)
smtp.send_message(message)
except Exception as e:
response["error"] = str(e)