Skip to content

Commit

Permalink
Merge pull request #6 from sclorg/fix_email
Browse files Browse the repository at this point in the history
Fix email
  • Loading branch information
phracek authored Nov 6, 2024
2 parents 24b88e8 + f97090a commit 78baee9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 18 additions & 10 deletions auto_merger/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,34 @@
from typing import List


DEFAULT_MAILS = ["phracek@redhat.com", "sclorg@redhat.com"]


class EmailSender:

def __init__(self, recipient_email: List[str]):
def __init__(self, recipient_email: List[str] = None):
self.recipient_email = recipient_email
self.mime_msg = MIMEMultipart()
self.send_from = ""
self.send_to = ""

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:
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]):
send_from = "phracek@redhat.com"
send_to = self.recipient_email
print(body)
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>")
print(msg)
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"] == "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"

0 comments on commit 78baee9

Please sign in to comment.