From bc4382f67ac1de9d54c8252bac04b56e65bb6d73 Mon Sep 17 00:00:00 2001 From: Grey Li Date: Wed, 1 Nov 2023 23:17:40 +0800 Subject: [PATCH] Support passing from_email in tuple format --- CHANGELOG.md | 2 ++ flask_mailman/message.py | 2 ++ tests/test_mail.py | 14 +++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f2c254..dc26253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## [1.0.0] - unreleased - Drop Python 3.6 support. +- Support passing `from_email` in tuple format to `send_mail()` function +([#35](https://github.com/waynerv/flask-mailman/issues/35)). ## [0.3.0] - 2021-08-08 diff --git a/flask_mailman/message.py b/flask_mailman/message.py index 59be033..3451b6f 100644 --- a/flask_mailman/message.py +++ b/flask_mailman/message.py @@ -225,6 +225,8 @@ def __init__( else: self.reply_to = [] self.from_email = from_email or current_app.extensions['mailman'].default_sender + if isinstance(self.from_email, tuple): + self.from_email = formataddr(self.from_email) self.subject = subject self.body = body or '' self.attachments = [] diff --git a/tests/test_mail.py b/tests/test_mail.py index c0e6861..26acf85 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -13,6 +13,17 @@ def test_send_mail(self): sent_msg = self.mail.outbox[0] self.assertEqual(sent_msg.from_email, self.MAIL_DEFAULT_SENDER) + def test_send_mail_with_tuple_from_email(self): + self.mail.send_mail( + subject="testing", + message="test", + from_email=("Name", "support@mysite.com"), + recipient_list=["tester@example.com"], + ) + self.assertEqual(len(self.mail.outbox), 1) + sent_msg = self.mail.outbox[0] + self.assertEqual(sent_msg.from_email, "Name ") + def test_send_mass_mail(self): message1 = ( 'Subject here', @@ -20,7 +31,7 @@ def test_send_mass_mail(self): 'from@example.com', ['first@example.com', 'other@example.com'], ) - message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com']) + message2 = ('Another Subject', 'Here is another message', ('Name', 'from@example.com'), ['second@test.com']) self.mail.send_mass_mail((message1, message2), fail_silently=False) self.assertEqual(len(self.mail.outbox), 2) msg1 = self.mail.outbox[0] @@ -30,3 +41,4 @@ def test_send_mass_mail(self): self.assertEqual(msg1.from_email, "from@example.com") msg2 = self.mail.outbox[1] self.assertEqual(msg2.subject, "Another Subject") + self.assertEqual(msg2.from_email, "Name ")