|
| 1 | +import logging |
| 2 | +import smtplib |
| 3 | +import ssl |
| 4 | +from collections.abc import Mapping |
| 5 | +from email.mime.multipart import MIMEMultipart |
| 6 | +from email.mime.text import MIMEText |
| 7 | + |
| 8 | +from pybars import Compiler |
| 9 | + |
| 10 | +from testgen import settings |
| 11 | + |
| 12 | +LOG = logging.getLogger(__name__) |
| 13 | + |
| 14 | +MANDATORY_SETTINGS = ( |
| 15 | + "EMAIL_FROM_ADDRESS", |
| 16 | + "SMTP_ENDPOINT", |
| 17 | + "SMTP_PORT", |
| 18 | + "SMTP_USERNAME", |
| 19 | + "SMTP_PASSWORD", |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class EmailTemplateException(Exception): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class BaseEmailTemplate: |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + compiler = Compiler() |
| 31 | + self.compiled_subject = compiler.compile(self.get_subject_template()) |
| 32 | + self.compiled_body = compiler.compile(self.get_body_template()) |
| 33 | + |
| 34 | + def validate_settings(self): |
| 35 | + missing_settings = [ |
| 36 | + f"TG_{setting_name}" |
| 37 | + for setting_name in MANDATORY_SETTINGS |
| 38 | + if getattr(settings, setting_name) is None |
| 39 | + ] |
| 40 | + |
| 41 | + if missing_settings: |
| 42 | + LOG.error( |
| 43 | + "Template '%s' can not send emails because the following settings are missing: %s", |
| 44 | + self.__class__.__name__, |
| 45 | + ", ".join(missing_settings), |
| 46 | + ) |
| 47 | + |
| 48 | + raise EmailTemplateException("Invalid or insufficient email/SMTP settings") |
| 49 | + |
| 50 | + def get_subject_template(self) -> str: |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + def get_body_template(self) -> str: |
| 54 | + raise NotImplementedError |
| 55 | + |
| 56 | + def get_message(self, recipients: list[str], context: Mapping | None) -> MIMEMultipart: |
| 57 | + subject = self.compiled_subject(context) |
| 58 | + body = self.compiled_body(context) |
| 59 | + |
| 60 | + message = MIMEMultipart("alternative") |
| 61 | + message["Subject"] = subject |
| 62 | + message["To"] = ", ".join(recipients) |
| 63 | + message["From"] = settings.EMAIL_FROM_ADDRESS |
| 64 | + message.attach(MIMEText(body, "html")) |
| 65 | + return message |
| 66 | + |
| 67 | + def send_mime_message(self, recipients: list[str], message: MIMEMultipart) -> dict: |
| 68 | + ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 69 | + try: |
| 70 | + with smtplib.SMTP_SSL(settings.SMTP_ENDPOINT, settings.SMTP_PORT, context=ssl_context) as smtp_server: |
| 71 | + smtp_server.login(settings.SMTP_USERNAME, settings.SMTP_PASSWORD) |
| 72 | + response = smtp_server.sendmail(settings.EMAIL_FROM_ADDRESS, recipients, message.as_string()) |
| 73 | + except Exception as e: |
| 74 | + LOG.error("Template '%s' failed to send email with: %s", self.__class__.__name__, e) # noqa: TRY400 |
| 75 | + else: |
| 76 | + return response |
| 77 | + |
| 78 | + def send(self, recipients: list[str], context: Mapping | None) -> dict: |
| 79 | + self.validate_settings() |
| 80 | + mime_message = self.get_message(recipients, context) |
| 81 | + response = self.send_mime_message(recipients, mime_message) |
| 82 | + |
| 83 | + LOG.info( |
| 84 | + "Template '%s' successfully sent email to %d recipients -- %d failed.", |
| 85 | + self.__class__.__name__, |
| 86 | + len(recipients) - len(response), |
| 87 | + len(response) |
| 88 | + ) |
| 89 | + |
| 90 | + return response |
0 commit comments