| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2e12f1c commit a97e83a
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,12 +28,16 @@ For more information on parameters and usage, see [here](../mail/mail.py) | |||
| 28 | 28 | ||
| 29 | 29 | ### Creating Personalizations | |
| 30 | 30 | ||
| 31 | - To create personalizations, you need a dictionary to store all your email components. See example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L47) | ||
| 32 | - After creating a dictionary, you can go ahead and create a `Personalization` object. | ||
| 31 | + The personalization helper can be used to create personalizations and customize various aspects of an email. See example [here](mail_example.py) in `build_multiple_emails_personalized()`, and refer [here](https://docs.sendgrid.com/for-developers/sending-email/personalizations) for more documentation. | ||
| 33 | 32 | ``` | |
| 34 | 33 | mock_personalization = Personalization() | |
| 35 | - for to_addr in personalization['to_list']: | ||
| 36 | - mock_personalization.add_to(to_addr) | ||
| 34 | + | ||
| 35 | + for to_addr in personalization['to_list']: | ||
| 36 | + mock_personalization.add_to(to_addr) | ||
| 37 | + | ||
| 38 | + mock_personalization.set_from(from_addr) | ||
| 39 | + mock_personalization.add_cc(cc_addr) | ||
| 40 | + # etc... | ||
| 37 | 41 | ``` | |
| 38 | 42 | ||
| 39 | 43 | ### Creating Attachments | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ def build_hello_email(): | |||
| 13 | 13 | from sendgrid import SendGridAPIClient | |
| 14 | 14 | from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException | |
| 15 | 15 | ||
| 16 | - message = Mail(from_email=From('from@example.com.com', 'Example From Name'), | ||
| 16 | + message = Mail(from_email=From('from@example.com', 'Example From Name'), | ||
| 17 | 17 | to_emails=To('to@example.com', 'Example To Name'), | |
| 18 | 18 | subject=Subject('Sending with SendGrid is Fun'), | |
| 19 | 19 | plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'), | |
@@ -26,25 +26,30 @@ def build_hello_email(): | |||
| 26 | 26 | except SendGridException as e: | |
| 27 | 27 | print(e.message) | |
| 28 | 28 | ||
| 29 | - for cc_addr in personalization['cc_list']: | ||
| 29 | + mock_personalization = Personalization() | ||
| 30 | + personalization_dict = get_mock_personalization_dict() | ||
| 31 | + | ||
| 32 | + for cc_addr in personalization_dict['cc_list']: | ||
| 30 | 33 | mock_personalization.add_to(cc_addr) | |
| 31 | 34 | ||
| 32 | - for bcc_addr in personalization['bcc_list']: | ||
| 35 | + for bcc_addr in personalization_dict['bcc_list']: | ||
| 33 | 36 | mock_personalization.add_bcc(bcc_addr) | |
| 34 | 37 | ||
| 35 | - for header in personalization['headers']: | ||
| 38 | + for header in personalization_dict['headers']: | ||
| 36 | 39 | mock_personalization.add_header(header) | |
| 37 | 40 | ||
| 38 | - for substitution in personalization['substitutions']: | ||
| 41 | + for substitution in personalization_dict['substitutions']: | ||
| 39 | 42 | mock_personalization.add_substitution(substitution) | |
| 40 | 43 | ||
| 41 | - for arg in personalization['custom_args']: | ||
| 44 | + for arg in personalization_dict['custom_args']: | ||
| 42 | 45 | mock_personalization.add_custom_arg(arg) | |
| 43 | 46 | ||
| 44 | - mock_personalization.subject = personalization['subject'] | ||
| 45 | - mock_personalization.send_at = personalization['send_at'] | ||
| 46 | - return mock_personalization | ||
| 47 | + mock_personalization.subject = personalization_dict['subject'] | ||
| 48 | + mock_personalization.send_at = personalization_dict['send_at'] | ||
| 49 | + | ||
| 50 | + message.add_personalization(mock_personalization) | ||
| 47 | 51 | ||
| 52 | + return message | ||
| 48 | 53 | ||
| 49 | 54 | def get_mock_personalization_dict(): | |
| 50 | 55 | """Get a dict of personalization mock.""" | |
@@ -78,6 +83,36 @@ def get_mock_personalization_dict(): | |||
| 78 | 83 | mock_pers['send_at'] = 1443636843 | |
| 79 | 84 | return mock_pers | |
| 80 | 85 | ||
| 86 | + def build_multiple_emails_personalized(): | ||
| 87 | + import json | ||
| 88 | + from sendgrid.helpers.mail import Mail, From, To, Cc, Bcc, Subject, PlainTextContent, \ | ||
| 89 | + HtmlContent, SendGridException, Personalization | ||
| 90 | + | ||
| 91 | + # Note that the domain for all From email addresses must match | ||
| 92 | + message = Mail(from_email=From('from@example.com', 'Example From Name'), | ||
| 93 | + subject=Subject('Sending with SendGrid is Fun'), | ||
| 94 | + plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'), | ||
| 95 | + html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>')) | ||
| 96 | + | ||
| 97 | + mock_personalization = Personalization() | ||
| 98 | + mock_personalization.add_to(To('test@example.com', 'Example User 1')) | ||
| 99 | + mock_personalization.add_cc(Cc('test1@example.com', 'Example User 2')) | ||
| 100 | + message.add_personalization(mock_personalization) | ||
| 101 | + | ||
| 102 | + mock_personalization_2 = Personalization() | ||
| 103 | + mock_personalization_2.add_to(To('test2@example.com', 'Example User 3')) | ||
| 104 | + mock_personalization_2.set_from(From('from@example.com', 'Example From Name 2')) | ||
| 105 | + mock_personalization_2.add_bcc(Bcc('test3@example.com', 'Example User 4')) | ||
| 106 | + message.add_personalization(mock_personalization_2) | ||
| 107 | + | ||
| 108 | + try: | ||
| 109 | + print(json.dumps(message.get(), sort_keys=True, indent=4)) | ||
| 110 | + return message.get() | ||
| 111 | + | ||
| 112 | + except SendGridException as e: | ||
| 113 | + print(e.message) | ||
| 114 | + | ||
| 115 | + return message | ||
| 81 | 116 | ||
| 82 | 117 | def build_attachment1(): | |
| 83 | 118 | """Build attachment mock. Make sure your content is base64 encoded before passing into attachment.content. | |
@@ -308,6 +343,15 @@ def build_kitchen_sink(): | |||
| 308 | 343 | ||
| 309 | 344 | return message | |
| 310 | 345 | ||
| 346 | + def send_multiple_emails_personalized(): | ||
| 347 | + # Assumes you set your environment variable: | ||
| 348 | + # https://github.com/sendgrid/sendgrid-python/blob/HEAD/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key | ||
| 349 | + message = build_multiple_emails_personalized() | ||
| 350 | + sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) | ||
| 351 | + response = sendgrid_client.send(message=message) | ||
| 352 | + print(response.status_code) | ||
| 353 | + print(response.body) | ||
| 354 | + print(response.headers) | ||
| 311 | 355 | ||
| 312 | 356 | def send_hello_email(): | |
| 313 | 357 | # Assumes you set your environment variable: | |
@@ -334,5 +378,8 @@ def send_kitchen_sink(): | |||
| 334 | 378 | ## this will actually send an email | |
| 335 | 379 | # send_hello_email() | |
| 336 | 380 | ||
| 381 | + ## this will send multiple emails | ||
| 382 | + # send_multiple_emails_personalized() | ||
| 383 | + | ||
| 337 | 384 | ## this will only send an email if you set SandBox Mode to False | |
| 338 | 385 | # send_kitchen_sink() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ class Personalization(object): | |||
| 6 | 6 | def __init__(self): | |
| 7 | 7 | """Create an empty Personalization and initialize member variables.""" | |
| 8 | 8 | self._tos = [] | |
| 9 | + self._from_email = None | ||
| 9 | 10 | self._ccs = [] | |
| 10 | 11 | self._bccs = [] | |
| 11 | 12 | self._subject = None | |
@@ -26,7 +27,10 @@ def add_email(self, email): | |||
| 26 | 27 | if email_type.__name__ == 'Bcc': | |
| 27 | 28 | self.add_bcc(email) | |
| 28 | 29 | return | |
| 29 | - raise ValueError('Please use a To, Cc or Bcc object.') | ||
| 30 | + if email_type.__name__ == 'From': | ||
| 31 | + self.from_email = email | ||
| 32 | + return | ||
| 33 | + raise ValueError('Please use a To, From, Cc or Bcc object.') | ||
| 30 | 34 | ||
| 31 | 35 | def _get_unique_recipients(self, recipients): | |
| 32 | 36 | unique_recipients = [] | |
@@ -77,6 +81,17 @@ def add_to(self, email): | |||
| 77 | 81 | ||
| 78 | 82 | self._tos.append(email.get()) | |
| 79 | 83 | ||
| 84 | + @property | ||
| 85 | + def from_email(self): | ||
| 86 | + return self._from_email | ||
| 87 | + | ||
| 88 | + @from_email.setter | ||
| 89 | + def from_email(self, value): | ||
| 90 | + self._from_email = value | ||
| 91 | + | ||
| 92 | + def set_from(self, email): | ||
| 93 | + self._from_email = email.get() | ||
| 94 | + | ||
| 80 | 95 | @property | |
| 81 | 96 | def ccs(self): | |
| 82 | 97 | """A list of recipients who will receive copies of this email. | |
@@ -236,6 +251,10 @@ def get(self): | |||
| 236 | 251 | if value: | |
| 237 | 252 | personalization[key[:-1]] = value | |
| 238 | 253 | ||
| 254 | + from_value = getattr(self, 'from_email') | ||
| 255 | + if from_value: | ||
| 256 | + personalization['from'] = from_value | ||
| 257 | + | ||
| 239 | 258 | for key in ['subject', 'send_at', 'dynamic_template_data']: | |
| 240 | 259 | value = getattr(self, key) | |
| 241 | 260 | if value: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -667,6 +667,15 @@ def test_personalization_add_email_filters_out_duplicate_to_emails_ignoring_case | |||
| 667 | 667 | ||
| 668 | 668 | self.assertEqual([to_email.get()], p.tos) | |
| 669 | 669 | ||
| 670 | + def test_personalization_set_from_email(self): | ||
| 671 | + self.maxDiff = None | ||
| 672 | + | ||
| 673 | + p = Personalization() | ||
| 674 | + from_email = From('test+from@example.com', 'Example From') | ||
| 675 | + p.set_from(from_email) | ||
| 676 | + | ||
| 677 | + self.assertEqual(from_email.get(), p.from_email) | ||
| 678 | + | ||
| 670 | 679 | def test_personalization_filters_out_duplicate_cc_emails(self): | |
| 671 | 680 | self.maxDiff = None | |
| 672 | 681 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ This directory provides examples for specific use cases of this library. Please | |||
| 8 | 8 | * [Send a Single Email to a Single Recipient](send_a_single_email_to_a_single_recipient.md) | |
| 9 | 9 | * [Send a Single Email to Multiple Recipients](send_a_single_email_to_multiple_recipients.md) | |
| 10 | 10 | * [Send Multiple Emails to Multiple Recipients](send_multiple_emails_to_multiple_recipients.md) | |
| 11 | + * [Send Multiple Emails with Personalizations](send_multiple_emails_personalizations.md) | ||
| 11 | 12 | * [Kitchen Sink - an example with all settings used](kitchen_sink.md) | |
| 12 | 13 | * [Transactional Templates](transactional_templates.md) | |
| 13 | 14 | * [Attachments](attachment.md) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + ```python | ||
| 2 | + import os | ||
| 3 | + import json | ||
| 4 | + from sendgrid import SendGridAPIClient | ||
| 5 | + from sendgrid.helpers.mail import Mail, Personalization, From, To, Cc, Bcc | ||
| 6 | + | ||
| 7 | + # Note that the domain for all From email addresses must match | ||
| 8 | + message = Mail( | ||
| 9 | + from_email=('from@example.com', 'Example From Name'), | ||
| 10 | + subject='Sending with Twilio SendGrid is Fun', | ||
| 11 | + html_content='<strong>and easy to do anywhere, even with Python</strong>') | ||
| 12 | + | ||
| 13 | + personalization1 = Personalization() | ||
| 14 | + personalization1.add_email(To('test0@example.com', 'Example Name 0')) | ||
| 15 | + personalization1.add_email(Cc('test1@example.com', 'Example Name 1')) | ||
| 16 | + message.add_personalization(personalization1) | ||
| 17 | + | ||
| 18 | + personalization2 = Personalization() | ||
| 19 | + personalization2.add_email(To('test2@example.com', 'Example Name 2')) | ||
| 20 | + personalization2.add_email(Bcc('test3@example.com', 'Example Name 3')) | ||
| 21 | + personalization2.add_email(From('from2@example.com', 'Example From Name 2')) | ||
| 22 | + message.add_personalization(personalization2) | ||
| 23 | + | ||
| 24 | + try: | ||
| 25 | + sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) | ||
| 26 | + response = sendgrid_client.send(message) | ||
| 27 | + print(response.status_code) | ||
| 28 | + print(response.body) | ||
| 29 | + print(response.headers) | ||
| 30 | + except Exception as e: | ||
| 31 | + print(e.message) | ||
| 32 | + ``` | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments