-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add reply_to_list functionality (#1062)
- Loading branch information
1 parent
c8076fa
commit 4b5c605
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,58 @@ def test_send_a_single_email_to_multiple_recipients(self): | |
}''') | ||
) | ||
|
||
def test_send_a_single_email_with_multiple_reply_to_addresses(self): | ||
from sendgrid.helpers.mail import (Mail, From, ReplyTo, To, Subject, | ||
PlainTextContent, HtmlContent) | ||
self.maxDiff = None | ||
message = Mail( | ||
from_email=From('[email protected]', 'Example From Name'), | ||
to_emails=To('[email protected]', 'Example To Name'), | ||
subject=Subject('Sending with SendGrid is Fun'), | ||
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'), | ||
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>')) | ||
|
||
message.reply_to_list = [ReplyTo(email = '[email protected]'), ReplyTo(email = '[email protected]')] | ||
|
||
self.assertEqual( | ||
message.get(), | ||
json.loads(r'''{ | ||
"content": [ | ||
{ | ||
"type": "text/plain", | ||
"value": "and easy to do anywhere, even with Python" | ||
}, | ||
{ | ||
"type": "text/html", | ||
"value": "<strong>and easy to do anywhere, even with Python</strong>" | ||
} | ||
], | ||
"from": { | ||
"email": "[email protected]", | ||
"name": "Example From Name" | ||
}, | ||
"personalizations": [ | ||
{ | ||
"to": [ | ||
{ | ||
"email": "[email protected]", | ||
"name": "Example To Name" | ||
} | ||
] | ||
} | ||
], | ||
"reply_to_list": [ | ||
{ | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"email": "[email protected]" | ||
} | ||
], | ||
"subject": "Sending with SendGrid is Fun" | ||
}''') | ||
) | ||
|
||
def test_multiple_emails_to_multiple_recipients(self): | ||
from sendgrid.helpers.mail import (Mail, From, To, Subject, | ||
PlainTextContent, HtmlContent, | ||
|
@@ -568,6 +620,44 @@ def test_value_error_is_raised_on_to_emails_set_to_list_of_lists(self): | |
'and easy to do anywhere, even with Python'), | ||
html_content=HtmlContent( | ||
'<strong>and easy to do anywhere, even with Python</strong>')) | ||
|
||
def test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_strs(self): | ||
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) | ||
self.maxDiff = None | ||
to_emails = [ | ||
('[email protected]', 'Example To Name 0'), | ||
('[email protected]', 'Example To Name 1') | ||
] | ||
|
||
mail = Mail( | ||
from_email=From('[email protected]', 'Example From Name'), | ||
to_emails=to_emails, | ||
subject=Subject('Sending with SendGrid is Fun'), | ||
plain_text_content=PlainTextContent( | ||
'and easy to do anywhere, even with Python'), | ||
html_content=HtmlContent( | ||
'<strong>and easy to do anywhere, even with Python</strong>')) | ||
with self.assertRaises(ValueError): | ||
mail.reply_to_list = ['[email protected]', '[email protected]'] | ||
|
||
def test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_tuples(self): | ||
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) | ||
self.maxDiff = None | ||
to_emails = [ | ||
('[email protected]', 'Example To Name 0'), | ||
('[email protected]', 'Example To Name 1') | ||
] | ||
|
||
mail = Mail( | ||
from_email=From('[email protected]', 'Example From Name'), | ||
to_emails=to_emails, | ||
subject=Subject('Sending with SendGrid is Fun'), | ||
plain_text_content=PlainTextContent( | ||
'and easy to do anywhere, even with Python'), | ||
html_content=HtmlContent( | ||
'<strong>and easy to do anywhere, even with Python</strong>')) | ||
with self.assertRaises(ValueError): | ||
mail.reply_to_list = [('[email protected]', 'Test Name')] | ||
|
||
def test_error_is_not_raised_on_to_emails_set_to_list_of_tuples(self): | ||
from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) | ||
|
29 changes: 29 additions & 0 deletions
29
use_cases/send_a_single_email_with_multiple_reply_to_addresses.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
```python | ||
import os | ||
from sendgrid import SendGridAPIClient | ||
from sendgrid.helpers.mail import Mail | ||
|
||
message = Mail( | ||
from_email='[email protected]', | ||
to_emails='[email protected]', | ||
subject='Sending with Twilio SendGrid is Fun', | ||
html_content='<strong>and easy to do anywhere, even with Python</strong>') | ||
message.reply_to_list = [ | ||
ReplyTo( | ||
email='[email protected]', | ||
name="Reply To Name 1", | ||
), | ||
ReplyTo( | ||
email='[email protected]', | ||
name="Reply To Name 2", | ||
) | ||
] | ||
try: | ||
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) | ||
response = sendgrid_client.send(message) | ||
print(response.status_code) | ||
print(response.body) | ||
print(response.headers) | ||
except Exception as e: | ||
print(e) | ||
``` |