|
12 | 12 | FailedDispatchResponse, |
13 | 13 | SuccessfulDispatchResponse, |
14 | 14 | ) |
15 | | -from trench.settings import EMAIL_HTML_TEMPLATE, EMAIL_PLAIN_TEMPLATE, EMAIL_SUBJECT |
| 15 | +from trench.settings import EMAIL_HTML_TEMPLATE, EMAIL_PLAIN_TEMPLATE, EMAIL_SUBJECT_TEMPLATE, EMAIL_SUBJECT |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class SendMailMessageDispatcher(AbstractMessageDispatcher): |
19 | 19 | _KEY_MESSAGE = "message" |
20 | 20 | _SUCCESS_DETAILS = _("Email message with MFA code has been sent.") |
21 | 21 |
|
| 22 | + def get_context(self, request): |
| 23 | + return {} |
| 24 | + |
| 25 | + def get_from_email(self, request): |
| 26 | + return settings.DEFAULT_FROM_EMAIL |
| 27 | + |
22 | 28 | def dispatch_message(self) -> DispatchResponse: |
23 | | - context = {"code": self.create_code()} |
| 29 | + request = self.request |
| 30 | + context = self.get_context(request) |
| 31 | + context.update({"code": self.create_code()}) |
| 32 | + email_subject = self._config[EMAIL_SUBJECT] |
| 33 | + email_subject_template = self._config[EMAIL_SUBJECT_TEMPLATE] |
24 | 34 | email_plain_template = self._config[EMAIL_PLAIN_TEMPLATE] |
25 | 35 | email_html_template = self._config[EMAIL_HTML_TEMPLATE] |
| 36 | + from_email = self.get_from_email(request) |
| 37 | + if not email_subject: |
| 38 | + email_subject = get_template(email_subject_template).render(context).replace('\n','') |
| 39 | + email_plain = get_template(email_plain_template).render(context) |
| 40 | + email_html = get_template(email_html_template).render(context) |
26 | 41 | try: |
27 | 42 | send_mail( |
28 | | - subject=self._config.get(EMAIL_SUBJECT), |
29 | | - message=get_template(email_plain_template).render(context), |
30 | | - html_message=get_template(email_html_template).render(context), |
31 | | - from_email=settings.DEFAULT_FROM_EMAIL, |
| 43 | + subject=email_subject, |
| 44 | + message=email_plain, |
| 45 | + html_message=email_html, |
| 46 | + from_email=from_email, |
32 | 47 | recipient_list=(self._to,), |
33 | 48 | fail_silently=False, |
34 | 49 | ) |
|
0 commit comments