-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
308 additions
and
147 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import abc | ||
from typing import Dict, List | ||
|
||
|
||
class EmailProvider: | ||
""" | ||
This is the base class of all email sending services. User can extend this abstract class to connect other email services. | ||
""" | ||
@abc.abstractmethod | ||
def send(self, matrix: Dict[str, List[str]], to: str, attachment_path: str = None) -> bool: | ||
""" | ||
Send the email. | ||
:param matrix: The argument matrix will be included in the email for reference. | ||
:param to: The recipient. | ||
:param attachment_path: The Excel file. | ||
:return: if the sending is successfully executed. Returning ``True`` does NOT mean the email has already been delivered, | ||
the meaning of return value may vary across difference email service platforms. | ||
""" | ||
pass |
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,58 @@ | ||
from typing import Dict, List | ||
import os | ||
import base64 | ||
|
||
import mailjet_rest as mailjet | ||
|
||
from . import EmailProvider | ||
|
||
|
||
class MailjetProvider(EmailProvider): | ||
""" | ||
This is the adaptor class for `Mailjet <https://mailjet.com>`_. | ||
""" | ||
def __init__(self, *, api_key: str, api_secret: str): | ||
self.client = mailjet.Client(auth=(api_key, api_secret), version='v3.1') # type: mailjet.Client | ||
|
||
def send(self, matrix: Dict[str, List[str]], to: str, attachment_path: str = None) -> bool: | ||
# check attached file size | ||
if attachment_path is not None: | ||
if os.stat(attachment_path).st_size / 1024 / 1024 >= 15: # >= 15MB | ||
print("Attached file is too large.") | ||
return False | ||
|
||
# generate string from matrix | ||
body = "Your MatrixTest job has completed. The argument matrix is shown below:\n" | ||
for k, v in matrix.items(): | ||
body += "%s : %s\n" % (k, v) | ||
|
||
# send | ||
data = { | ||
"From": { | ||
"Email": "matrixtest@funqtion.xyz", | ||
"Name": "MatrixTest" | ||
}, | ||
"To": [ | ||
{ | ||
"Email": to | ||
} | ||
], | ||
"Subject": "MatrixTest job has completed", | ||
"TextPart": body | ||
} | ||
if attachment_path is not None: | ||
attachment_fd = open(attachment_path, 'rb') | ||
data['Attachments'] = [{ | ||
"ContentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
"Filename": os.path.basename(attachment_path), | ||
"Base64Content": base64.b64encode(attachment_fd.read()).decode() | ||
}] | ||
result = self.client.send.create(data={'Messages': [data]}) | ||
|
||
if result.status_code != 200: | ||
print(result.status_code) | ||
print(result.json()) | ||
return False | ||
else: | ||
return True |
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,2 @@ | ||
from .EmailProvider import EmailProvider | ||
from .MailjetProvider import MailjetProvider |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .MatrixTestRunner import MatrixTestRunner | ||
from .EmailService import * |
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
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
|
||
for item in sys.argv: | ||
print(item, flush=True) | ||
# time.sleep(.2) | ||
time.sleep(.1) |
Oops, something went wrong.