Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Everyone that made python-postmark awesome
- Nikolay Fominykh (https://github.com/tigrus)
- Gabe Limon (https://github.com/gabelimon)
- Olle Hellgren (https://github.com/anulaibar)
- Harry Khanna (https://www.khanna.law/)

(Did I miss anyone?)
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ mail = EmailMultiAlternatives(subject, msg_plain, from_email, recipients, connec
mail.tag = tag
mail.send()
```

#### Setting the `message_stream`
```python
from django.core.mail import get_connection, EmailMultiAlternatives

mail = EmailMultiAlternatives(subject, msg_plain, from_email, recipients, connection=connection, headers=headers)

mail.message_stream = "broadcast"
mail.send()
```


Tornado
-------
Expand Down
17 changes: 16 additions & 1 deletion postmark/django_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def __init__(self, *args, **kwargs):
else:
self.track_opens = getattr(settings, 'POSTMARK_TRACK_OPENS', False)

if 'message_stream' in kwargs:
self.message_stream = kwargs['message_stream']
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did you also test the case where you can pass message_stream into the EmailMessage kwargs? As opposed to setting it on the instance after instantiation? I ask because these other kwargs track_opens and tag del the kwarg after

del kwargs['tag']

Thinking we probably need to do the same here as it might raise an error upstream.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch! I've added it. But note that I can't find any reference to these classes anywhere, including the README.

I'm guessing users can use these as a convenience if they want to pass args to the __init__() function of an EmailMultiAlternatives subclass. But the way the README suggests usage is just to set tag or message_stream as an attribute on EmailMultiAlternatives after instantiation.

del kwargs['message_stream']
else:
self.message_stream = None

super(PMEmailMessage, self).__init__(*args, **kwargs)


Expand All @@ -38,6 +44,13 @@ def __init__(self, *args, **kwargs):
else:
self.track_opens = getattr(settings, 'POSTMARK_TRACK_OPENS', False)

if 'message_stream' in kwargs:
self.message_stream = kwargs['message_stream']
del kwargs['message_stream']
else:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment as above ^

self.message_stream = None


super(PMEmailMultiAlternatives, self).__init__(*args, **kwargs)


Expand Down Expand Up @@ -117,6 +130,7 @@ def _build_message(self, message):
else:
attachments.append(item)

message_stream = getattr(message, 'message_stream', None)
postmark_message = PMMail(api_key=self.api_key,
subject=message.subject,
sender=message.from_email,
Expand All @@ -127,7 +141,8 @@ def _build_message(self, message):
html_body=html_body,
reply_to=reply_to,
custom_headers=custom_headers,
attachments=attachments)
attachments=attachments,
message_stream=message_stream)

postmark_message.tag = getattr(message, 'tag', None)
postmark_message.track_opens = getattr(message, 'track_opens', False)
Expand Down
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@ def test_send_attachment_bytes(self):
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)):
message.send()

def test_message_stream(self):
message = EmailMultiAlternatives(
connection=EmailBackend(api_key='dummy'),
from_email='from@test.com', to=['recipient@test.com'], subject='html test', body='hello there'
)
message.attach_alternative('<b>hello</b> there', 'text/html')
message.message_stream = 'broadcast'

with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport:
message.send()
data = json.loads(transport.call_args[0][0].data.decode('utf-8'))
self.assertEqual('broadcast', data['MessageStream'])
self.assertEqual('hello there', data['TextBody'])
self.assertEqual('<b>hello</b> there', data['HtmlBody'])



if __name__ == '__main__':
Expand Down