Skip to content

Commit 4b5c605

Browse files
feat: Add reply_to_list functionality (#1062)
1 parent c8076fa commit 4b5c605

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

sendgrid/helpers/mail/mail.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(
6060
self._ip_pool_name = None
6161
self._mail_settings = None
6262
self._reply_to = None
63+
self._reply_to_list = None
6364
self._send_at = None
6465
self._subject = None
6566
self._template_id = None
@@ -695,6 +696,32 @@ def reply_to(self, value):
695696
value = ReplyTo(value[0], value[1])
696697
self._reply_to = value
697698

699+
@property
700+
def reply_to_list(self):
701+
"""A list of ReplyTo email addresses
702+
703+
:rtype: list(ReplyTo), tuple
704+
"""
705+
return self._reply_to_list
706+
707+
@reply_to_list.setter
708+
def reply_to_list(self, value):
709+
"""A list of ReplyTo email addresses
710+
711+
:param value: A list of ReplyTo email addresses
712+
:type value: list(ReplyTo), tuple
713+
"""
714+
if isinstance(value, list):
715+
for reply in value:
716+
if isinstance(reply, ReplyTo):
717+
if not isinstance(reply.email, str):
718+
raise ValueError('You must provide an email for each entry in a reply_to_list')
719+
else:
720+
raise ValueError(
721+
'Please use a list of ReplyTos for a reply_to_list.'
722+
)
723+
self._reply_to_list = value
724+
698725
@property
699726
def contents(self):
700727
"""The contents of the email
@@ -981,6 +1008,7 @@ def get(self):
9811008
'mail_settings': self._get_or_none(self.mail_settings),
9821009
'tracking_settings': self._get_or_none(self.tracking_settings),
9831010
'reply_to': self._get_or_none(self.reply_to),
1011+
'reply_to_list': [r.get() for r in self.reply_to_list or []],
9841012
}
9851013

9861014
return {key: value for key, value in mail.items()

test/unit/test_mail_helpers.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,58 @@ def test_send_a_single_email_to_multiple_recipients(self):
235235
}''')
236236
)
237237

238+
def test_send_a_single_email_with_multiple_reply_to_addresses(self):
239+
from sendgrid.helpers.mail import (Mail, From, ReplyTo, To, Subject,
240+
PlainTextContent, HtmlContent)
241+
self.maxDiff = None
242+
message = Mail(
243+
from_email=From('test+from@example.com', 'Example From Name'),
244+
to_emails=To('test+to0@example.com', 'Example To Name'),
245+
subject=Subject('Sending with SendGrid is Fun'),
246+
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
247+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
248+
249+
message.reply_to_list = [ReplyTo(email = 'test+reply_to_1@example.com'), ReplyTo(email = 'test+reply_to_2@example.com')]
250+
251+
self.assertEqual(
252+
message.get(),
253+
json.loads(r'''{
254+
"content": [
255+
{
256+
"type": "text/plain",
257+
"value": "and easy to do anywhere, even with Python"
258+
},
259+
{
260+
"type": "text/html",
261+
"value": "<strong>and easy to do anywhere, even with Python</strong>"
262+
}
263+
],
264+
"from": {
265+
"email": "test+from@example.com",
266+
"name": "Example From Name"
267+
},
268+
"personalizations": [
269+
{
270+
"to": [
271+
{
272+
"email": "test+to0@example.com",
273+
"name": "Example To Name"
274+
}
275+
]
276+
}
277+
],
278+
"reply_to_list": [
279+
{
280+
"email": "test+reply_to_1@example.com"
281+
},
282+
{
283+
"email": "test+reply_to_2@example.com"
284+
}
285+
],
286+
"subject": "Sending with SendGrid is Fun"
287+
}''')
288+
)
289+
238290
def test_multiple_emails_to_multiple_recipients(self):
239291
from sendgrid.helpers.mail import (Mail, From, To, Subject,
240292
PlainTextContent, HtmlContent,
@@ -568,6 +620,44 @@ def test_value_error_is_raised_on_to_emails_set_to_list_of_lists(self):
568620
'and easy to do anywhere, even with Python'),
569621
html_content=HtmlContent(
570622
'<strong>and easy to do anywhere, even with Python</strong>'))
623+
624+
def test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_strs(self):
625+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
626+
self.maxDiff = None
627+
to_emails = [
628+
('test+to0@example.com', 'Example To Name 0'),
629+
('test+to1@example.com', 'Example To Name 1')
630+
]
631+
632+
mail = Mail(
633+
from_email=From('test+from@example.com', 'Example From Name'),
634+
to_emails=to_emails,
635+
subject=Subject('Sending with SendGrid is Fun'),
636+
plain_text_content=PlainTextContent(
637+
'and easy to do anywhere, even with Python'),
638+
html_content=HtmlContent(
639+
'<strong>and easy to do anywhere, even with Python</strong>'))
640+
with self.assertRaises(ValueError):
641+
mail.reply_to_list = ['test+reply_to0@example.com', 'test+reply_to1@example.com']
642+
643+
def test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_tuples(self):
644+
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
645+
self.maxDiff = None
646+
to_emails = [
647+
('test+to0@example.com', 'Example To Name 0'),
648+
('test+to1@example.com', 'Example To Name 1')
649+
]
650+
651+
mail = Mail(
652+
from_email=From('test+from@example.com', 'Example From Name'),
653+
to_emails=to_emails,
654+
subject=Subject('Sending with SendGrid is Fun'),
655+
plain_text_content=PlainTextContent(
656+
'and easy to do anywhere, even with Python'),
657+
html_content=HtmlContent(
658+
'<strong>and easy to do anywhere, even with Python</strong>'))
659+
with self.assertRaises(ValueError):
660+
mail.reply_to_list = [('test+reply_to@example.com', 'Test Name')]
571661

572662
def test_error_is_not_raised_on_to_emails_set_to_list_of_tuples(self):
573663
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```python
2+
import os
3+
from sendgrid import SendGridAPIClient
4+
from sendgrid.helpers.mail import Mail
5+
6+
message = Mail(
7+
from_email='from_email@example.com',
8+
to_emails='to@example.com',
9+
subject='Sending with Twilio SendGrid is Fun',
10+
html_content='<strong>and easy to do anywhere, even with Python</strong>')
11+
message.reply_to_list = [
12+
ReplyTo(
13+
email='reply-to-1@example.com',
14+
name="Reply To Name 1",
15+
),
16+
ReplyTo(
17+
email='reply-to-2@example.com',
18+
name="Reply To Name 2",
19+
)
20+
]
21+
try:
22+
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
23+
response = sendgrid_client.send(message)
24+
print(response.status_code)
25+
print(response.body)
26+
print(response.headers)
27+
except Exception as e:
28+
print(e)
29+
```

0 commit comments

Comments
 (0)