Skip to content

Commit

Permalink
Fix email building structure.
Browse files Browse the repository at this point in the history
Also tests should be present in case we use it.

Signed-off-by: Petr "Stone" Hracek <[email protected]>
  • Loading branch information
phracek committed Nov 6, 2024
1 parent 54f9c0f commit f97090a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
21 changes: 13 additions & 8 deletions auto_merger/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,26 @@ class EmailSender:
def __init__(self, recipient_email: List[str] = None):
self.recipient_email = recipient_email
self.mime_msg = MIMEMultipart()
self.send_from = ""
self.send_to = ""

def send_email(self, subject_msg, body: List[str] = None):
send_from = "[email protected]"
send_to = DEFAULT_MAILS
def create_email_msg(self, subject_msg: str):
self.send_from = "[email protected]"
self.send_to = DEFAULT_MAILS
if self.recipient_email is not None:
send_to.extend(self.recipient_email)
self.send_to.extend(self.recipient_email)
self.mime_msg["From"] = self.send_from
self.mime_msg["To"] = ", ".join(self.send_to)
self.mime_msg["Subject"] = subject_msg

def send_email(self, subject_msg, body: List[str] = None):
whole_body = "".join(body)
msg = ("<html><head><style>table, th, td {border: 1px solid black;}</style></head>"
f"<body>{whole_body}</body></html>")
self.mime_msg["From"] = send_from
self.mime_msg["To"] = ", ".join(send_to)
self.mime_msg["Subject"] = subject_msg
self.create_email_msg(subject_msg)
self.mime_msg.attach(MIMEText(msg, "html"))
smtp = smtplib.SMTP("127.0.0.1")
smtp.sendmail(send_from, send_to, self.mime_msg.as_string())
smtp.sendmail(self.send_from, self.send_to, self.mime_msg.as_string())
smtp.close()
print("Sending email finished")

26 changes: 26 additions & 0 deletions tests/test_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

from flexmock import flexmock

from auto_merger.email import EmailSender


def test_create_email_no_recepients():
es = EmailSender(recipient_email=[])
es.create_email_msg("something important")
assert es.mime_msg
assert es.mime_msg["From"] == "[email protected]"
assert "[email protected]" in es.mime_msg["To"]
assert "[email protected]" in es.mime_msg["To"]
assert es.mime_msg["Subject"] == "something important"


def test_create_email_with_recipients():
es = EmailSender(recipient_email=["[email protected]"])
es.create_email_msg("something important")
assert es.mime_msg
assert es.mime_msg["From"] == "[email protected]"
assert "[email protected]" in es.mime_msg["To"]
assert "[email protected]" in es.mime_msg["To"]
assert "[email protected]" in es.mime_msg["To"]
assert es.mime_msg["Subject"] == "something important"

0 comments on commit f97090a

Please sign in to comment.