diff --git a/auto_merger/email.py b/auto_merger/email.py index 7cea4c0..75c468e 100644 --- a/auto_merger/email.py +++ b/auto_merger/email.py @@ -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 = "phracek@redhat.com" - send_to = DEFAULT_MAILS + def create_email_msg(self, subject_msg: str): + self.send_from = "phracek@redhat.com" + 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 = ("
" f"{whole_body}") - 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") diff --git a/tests/test_email.py b/tests/test_email.py new file mode 100644 index 0000000..a9f9dab --- /dev/null +++ b/tests/test_email.py @@ -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"] == "phracek@redhat.com" + assert "phracek@redhat.com" in es.mime_msg["To"] + assert "sclorg@redhat.com" in es.mime_msg["To"] + assert es.mime_msg["Subject"] == "something important" + + +def test_create_email_with_recipients(): + es = EmailSender(recipient_email=["foo@bar.com"]) + es.create_email_msg("something important") + assert es.mime_msg + assert es.mime_msg["From"] == "phracek@redhat.com" + assert "phracek@redhat.com" in es.mime_msg["To"] + assert "sclorg@redhat.com" in es.mime_msg["To"] + assert "foo@bar.com" in es.mime_msg["To"] + assert es.mime_msg["Subject"] == "something important"