Skip to content

Commit

Permalink
Support passing from_email in tuple format
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Nov 1, 2023
1 parent 054f090 commit bc4382f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions flask_mailman/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
14 changes: 13 additions & 1 deletion tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ 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 <support@mysite.com>")

def test_send_mass_mail(self):
message1 = (
'Subject here',
'Here is the message',
'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]
Expand All @@ -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 <from@example.com>")

0 comments on commit bc4382f

Please sign in to comment.