utils: add setting to choose email sending format (#50745)
This commit is contained in:
parent
d0b6b64bf1
commit
961af9538e
|
@ -354,6 +354,9 @@ default_settings = dict(
|
|||
A2_DUPLICATES_BIRTHDATE_BONUS=Setting(
|
||||
default=0.3,
|
||||
definition='Bonus in case of birthdate match (no bonus is 0, max is 1).'),
|
||||
A2_EMAIL_FORMAT=Setting(
|
||||
default='multipart/alternative',
|
||||
definition='Send email as "multiplart/alternative" or limit to "text/plain" or "text/html".'),
|
||||
)
|
||||
|
||||
app_settings = AppSettings(default_settings)
|
||||
|
|
|
@ -30,6 +30,7 @@ from importlib import import_module
|
|||
|
||||
import django.apps
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.http.request import QueryDict
|
||||
|
@ -666,14 +667,26 @@ def send_templated_mail(user_or_email, template_names, context=None, with_html=T
|
|||
html_body = None
|
||||
html_body_template_names = [template_name + '_body.html' for template_name in template_names]
|
||||
html_body_template_names += legacy_html_body_templates or []
|
||||
if with_html:
|
||||
if with_html and app_settings.A2_EMAIL_FORMAT != 'text/plain':
|
||||
try:
|
||||
html_body = render_to_string(html_body_template_names, ctx, request=request)
|
||||
except TemplateDoesNotExist:
|
||||
html_body = None
|
||||
|
||||
send_mail(subject, body, from_email or settings.DEFAULT_FROM_EMAIL, [user_or_email],
|
||||
html_message=html_body, **kwargs)
|
||||
if app_settings.A2_EMAIL_FORMAT == 'text/html':
|
||||
msg = EmailMessage(
|
||||
subject, html_body, from_email or settings.DEFAULT_FROM_EMAIL, [user_or_email], **kwargs
|
||||
)
|
||||
msg.content_subtype = 'html'
|
||||
msg.send()
|
||||
elif app_settings.A2_EMAIL_FORMAT == 'text/plain':
|
||||
msg = EmailMessage(
|
||||
subject, body, from_email or settings.DEFAULT_FROM_EMAIL, [user_or_email], **kwargs
|
||||
)
|
||||
msg.send()
|
||||
else:
|
||||
send_mail(subject, body, from_email or settings.DEFAULT_FROM_EMAIL, [user_or_email],
|
||||
html_message=html_body, **kwargs)
|
||||
|
||||
|
||||
def get_fk_model(model, fieldname):
|
||||
|
|
|
@ -177,6 +177,26 @@ def test_send_templated_mail_template_vars(settings, simple_user):
|
|||
assert sent_mail.body == 'here is the text body\n'
|
||||
assert sent_mail.alternatives == [('here is the html body\n', 'text/html')]
|
||||
|
||||
settings.A2_EMAIL_FORMAT = 'text/plain'
|
||||
send_templated_mail(simple_user, ['template_vars'])
|
||||
|
||||
assert len(mail.outbox) == 1
|
||||
sent_mail = mail.outbox.pop()
|
||||
assert sent_mail.subject == 'here is the subject'
|
||||
assert sent_mail.body == 'here is the text body\n'
|
||||
assert sent_mail.content_subtype == 'plain'
|
||||
assert not hasattr(sent_mail, 'alternatives')
|
||||
|
||||
settings.A2_EMAIL_FORMAT = 'text/html'
|
||||
send_templated_mail(simple_user, ['template_vars'])
|
||||
|
||||
assert len(mail.outbox) == 1
|
||||
sent_mail = mail.outbox.pop()
|
||||
assert sent_mail.subject == 'here is the subject'
|
||||
assert sent_mail.body == 'here is the html body\n'
|
||||
assert sent_mail.content_subtype == 'html'
|
||||
assert not hasattr(sent_mail, 'alternatives')
|
||||
|
||||
|
||||
def test_lazy_join():
|
||||
a = 'a'
|
||||
|
|
Loading…
Reference in New Issue