Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty from_email param #131

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ This library, as others, is used in projects of Vinta clients. We are always loo
.. _Django: http://djangoproject.com
.. |GitterBadge| image:: https://badges.gitter.im/vintasoftware/django-templated-email.svg
.. _GitterBadge: https://gitter.im/vintasoftware/django-templated-email?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
.. |TravisBadge| image:: https://travis-ci.org/vintasoftware/django-templated-email.svg?branch=develop
.. _TravisBadge: https://travis-ci.org/vintasoftware/django-templated-email
.. |TravisBadge| image:: https://travis-ci.com/vintasoftware/django-templated-email.svg?branch=develop
.. _TravisBadge: https://travis-ci.com/vintasoftware/django-templated-email
.. |CoverageBadge| image:: https://coveralls.io/repos/github/vintasoftware/django-templated-email/badge.svg?branch=develop
.. _CoverageBadge: https://coveralls.io/github/vintasoftware/django-templated-email?branch=develop
.. |PypiversionBadge| image:: https://img.shields.io/pypi/v/django-templated-email.svg
Expand Down
6 changes: 3 additions & 3 deletions templated_email/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def get_templated_mail(template_name, context, from_email=None, to=None,
create_link=create_link)


def send_templated_mail(template_name, from_email, recipient_list, context,
cc=None, bcc=None, fail_silently=False, connection=None,
headers=None, template_prefix=None,
def send_templated_mail(template_name, from_email=None, recipient_list=None,
context=None, cc=None, bcc=None, fail_silently=False,
connection=None, headers=None, template_prefix=None,
template_suffix=None,
create_link=False, **kwargs):
"""Easy wrapper for sending a templated email to a recipient list.
Expand Down
8 changes: 6 additions & 2 deletions templated_email/backends/vanilla_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ def _render_email(self, template_name, context,

return response

def get_email_message(self, template_name, context, from_email=None, to=None,
def get_email_message(self, template_name, context=None, from_email=None, to=None,
cc=None, bcc=None, headers=None,
template_prefix=None, template_suffix=None,
template_dir=None, file_extension=None,
attachments=None, create_link=False):

from_email = from_email or settings.DEFAULT_FROM_EMAIL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have also a TEMPLATED_EMAIL_FROM_EMAIL setting.
A user thought this was a default. See: #97 (comment)

So I think this should read: from_email = from_email or settings.TEMPLATED_EMAIL_FROM_EMAIL or settings.DEFAULT_FROM_EMAIL

Also, check again the README, see if this is well explained. And check where TEMPLATED_EMAIL_FROM_EMAIL is being used.

Copy link
Contributor Author

@tuliolages tuliolages Jun 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've seen, this TEMPLATED_EMAIL_FROM_EMAIL is used only by the Class-based mixin. It's documentation is written here. While it's not saying explicitly that the setting is not required, it does not specify that it'll fall back to DEFAULT_FROM_MAIL. I think this could be updated.

I'm not sure, however, if we should put it as a default in the vanilla-django file, since this class-based mixin is like a wrapper over the vanilla and the docs specific that this setting is used only by the class-based view approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the backend use this setting TEMPLATED_EMAIL_FROM_EMAIL. So from_email = from_email or settings.TEMPLATED_EMAIL_FROM_EMAIL or settings.DEFAULT_FROM_EMAIL seems fine for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fjsj I've updated the vanilla backend and added a note about this behavior in the readme.


context = context or {}

if create_link:
email_uuid = uuid.uuid4()
link_context = dict(context)
Expand Down Expand Up @@ -234,7 +238,7 @@ def _generate_plain_part(self, parts):
parts['plain'] = plain_func(parts['html'])
return True

def send(self, template_name, from_email, recipient_list, context,
def send(self, template_name, from_email=None, recipient_list=None, context=None,
cc=None, bcc=None,
fail_silently=False,
headers=None,
Expand Down
87 changes: 68 additions & 19 deletions tests/backends/test_vanilla_django_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_email_text_escaping(self):
)
def test_get_email_message(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
Expand All @@ -155,7 +155,7 @@ def test_get_email_message(self, mock):
)
def test_get_email_message_with_create_link(self, mocked):
self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'],
create_link=True)
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_get_email_message_with_inline_image(self, mocked):
)
def test_custom_emailmessage_klass(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, AnymailMessage))
Expand All @@ -205,7 +205,7 @@ def test_custom_emailmessage_klass(self, mock):
)
def test_get_email_message_without_subject(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
Expand All @@ -216,6 +216,55 @@ def test_get_email_message_without_subject(self, mock):
self.assertEqual(message.bcc, ['[email protected]'])
self.assertEqual(message.from_email, '[email protected]')

@patch.object(
template_backend_klass, '_render_email',
return_value={'plain': PLAIN_RESULT, 'subject': SUBJECT_RESULT}
)
def test_get_email_message_without_from_email_when_no_default_email_is_set(self, mock):
message = self.backend.get_email_message(
'foo.email', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
self.assertEqual(message.body, PLAIN_RESULT)
self.assertEqual(message.subject, SUBJECT_RESULT)
self.assertEqual(message.to, ['[email protected]'])
self.assertEqual(message.cc, ['[email protected]'])
self.assertEqual(message.bcc, ['[email protected]'])
self.assertEqual(message.from_email, 'webmaster@localhost')

@override_settings(DEFAULT_FROM_EMAIL='default.email@localhost')
@patch.object(
template_backend_klass, '_render_email',
return_value={'plain': PLAIN_RESULT, 'subject': SUBJECT_RESULT}
)
def test_get_email_message_without_from_email_when_default_email_is_set(self, mock):
message = self.backend.get_email_message(
'foo.email', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
self.assertEqual(message.body, PLAIN_RESULT)
self.assertEqual(message.subject, SUBJECT_RESULT)
self.assertEqual(message.to, ['[email protected]'])
self.assertEqual(message.cc, ['[email protected]'])
self.assertEqual(message.bcc, ['[email protected]'])
self.assertEqual(message.from_email, 'default.email@localhost')

@patch.object(
template_backend_klass, '_render_email',
return_value={'plain': PLAIN_RESULT, 'subject': SUBJECT_RESULT}
)
def test_get_email_message_without_recipient_list(self, mock):
message = self.backend.get_email_message(
'foo.email',
from_email='[email protected]')
self.assertTrue(isinstance(message, EmailMessage))
self.assertEqual(message.body, PLAIN_RESULT)
self.assertEqual(message.subject, SUBJECT_RESULT)
self.assertEqual(message.to, [])
self.assertEqual(message.cc, [])
self.assertEqual(message.bcc, [])
self.assertEqual(message.from_email, '[email protected]')

@override_settings(TEMPLATED_EMAIL_DJANGO_SUBJECTS={'foo.email':
'foo\r\n'})
@patch.object(
Expand All @@ -224,7 +273,7 @@ def test_get_email_message_without_subject(self, mock):
)
def test_get_email_message_without_subject_multiple_templates(self, mock):
message = self.backend.get_email_message(
['woo.email', 'foo.email'], {},
['woo.email', 'foo.email'],
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
Expand All @@ -241,7 +290,7 @@ def test_get_email_message_without_subject_multiple_templates(self, mock):
)
def test_get_email_message_generated_plain_text(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMultiAlternatives))
Expand All @@ -260,12 +309,12 @@ def test_get_email_message_generated_plain_text(self, mock):
)
@override_settings(TEMPLATED_EMAIL_PLAIN_FUNCTION=lambda x: 'hi')
def test_get_email_message_custom_func_generated_plain_text(self, mock):
message = self.backend.get_email_message('foo.email', {})
message = self.backend.get_email_message('foo.email')
self.assertEqual(message.body, 'hi')

def test_get_multi_match_last_email_message_generated_plain_text(self):
message = self.backend.get_email_message(
['multi-template.email', 'foo.email', ], {},
['multi-template.email', 'foo.email', ],
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertEqual(message.body, MULTI_TEMPLATE_PLAIN_RESULT)
Expand All @@ -277,7 +326,7 @@ def test_get_multi_match_last_email_message_generated_plain_text(self):

def test_get_multi_first_match_email_message_generated_plain_text(self):
message = self.backend.get_email_message(
['foo.email', 'multi-template.email', ], {},
['foo.email', 'multi-template.email', ],
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertEqual(message.body, MULTI_TEMPLATE_PLAIN_RESULT)
Expand All @@ -289,7 +338,7 @@ def test_get_multi_first_match_email_message_generated_plain_text(self):

def test_get_multi_options_select_last_plain_only(self):
message = self.backend.get_email_message(
['non-existing.email', 'also-non-existing.email', 'non-existing-without-suffix', 'foo.email', 'multi-template.email', ], {},
['non-existing.email', 'also-non-existing.email', 'non-existing-without-suffix', 'foo.email', 'multi-template.email', ],
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertEqual(message.body, MULTI_TEMPLATE_PLAIN_RESULT)
Expand All @@ -306,7 +355,7 @@ def test_get_multi_options_select_last_plain_only(self):
)
def test_get_email_message_with_plain_and_html(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMultiAlternatives))
Expand All @@ -326,7 +375,7 @@ def test_get_email_message_with_plain_and_html(self, mock):
def test_get_email_message_with_no_body_parts(self, mock):
with pytest.raises(EmailRenderException):
self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])

Expand All @@ -338,7 +387,7 @@ def test_get_email_message_with_no_body_parts(self, mock):
)
def test_custom_emailmessage_klass_multipart(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, AnymailMessage))
Expand All @@ -351,7 +400,7 @@ def test_custom_emailmessage_klass_multipart(self, mock):
)
def test_get_email_message_html_only(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'])
self.assertTrue(isinstance(message, EmailMessage))
Expand All @@ -370,7 +419,7 @@ def test_get_email_message_html_only(self, mock):
)
def test_send(self, render_mock):
ret = self.backend.send('mixed_template', '[email protected]',
['[email protected]', '[email protected]'], {},
['[email protected]', '[email protected]'],
headers={'Message-Id': 'a_message_id'})
self.assertEqual(ret, 'a_message_id')
self.assertEqual(len(mail.outbox), 1)
Expand Down Expand Up @@ -443,7 +492,7 @@ def test_all_arguments_passed_forward_from_send(
)
def test_send_attachment_mime_base(self, render_mock):
self.backend.send('plain_template', '[email protected]',
['[email protected]', '[email protected]'], {},
['[email protected]', '[email protected]'],
attachments=[MIMEImage(TXT_FILE, 'text/plain')])
attachment = mail.outbox[0].attachments[0]
self.assertEqual(decode_b64_msg(attachment.get_payload()),
Expand All @@ -456,7 +505,7 @@ def test_send_attachment_mime_base(self, render_mock):
)
def test_send_attachment_tripple(self, render_mock):
self.backend.send('plain_template', '[email protected]',
['[email protected]', '[email protected]'], {},
['[email protected]', '[email protected]'],
attachments=[('black_pixel.png', TXT_FILE, 'text/plain')])
attachment = mail.outbox[0].attachments[0]
self.assertEqual(('black_pixel.png', TXT_FILE, 'text/plain'),
Expand All @@ -468,7 +517,7 @@ def test_send_attachment_tripple(self, render_mock):
)
def test_get_email_message_attachment_mime_base(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'],
attachments=[MIMEImage(TXT_FILE, 'text/plain')])
Expand All @@ -482,7 +531,7 @@ def test_get_email_message_attachment_mime_base(self, mock):
)
def test_get_email_message_attachment_tripple(self, mock):
message = self.backend.get_email_message(
'foo.email', {},
'foo.email',
from_email='[email protected]', cc=['[email protected]'],
bcc=['[email protected]'], to=['[email protected]'],
attachments=[('black_pixel.png', TXT_FILE, 'text/plain')])
Expand Down