-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also tests should be present in case we use it. Signed-off-by: Petr "Stone" Hracek <[email protected]>
- Loading branch information
Showing
2 changed files
with
39 additions
and
8 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 |
---|---|---|
|
@@ -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") | ||
|
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 |
---|---|---|
@@ -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" |