|
| 1 | +## Using helper class to send emails |
| 2 | +You can use helper classes to customize the process of sending emails using SendGrid. Each process (such as sending a mock email, |
| 3 | +building attachments, configuring settings, building personalizations, etc.) are made easy using helpers. All you need is a file with |
| 4 | +all the classes imported and you can start sending emails! |
| 5 | + |
| 6 | +> Note: You will need move this file to the root directory of this project to execute properly. |
| 7 | +
|
| 8 | +### Creating a simple email object and sending it |
| 9 | +The example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L9) |
| 10 | +defines minimum requirement to send an email. |
| 11 | +``` |
| 12 | + from_email = Email("test@example.com") |
| 13 | + subject = "Hello World from the SendGrid Python Library" |
| 14 | + to_email = Email("test@example.com") |
| 15 | +``` |
| 16 | +You can use `Email` class to define a mail id. |
| 17 | + |
| 18 | +``` |
| 19 | +content = Content("text/plain", "some text here") |
| 20 | +``` |
| 21 | +The `Content` class takes mainly two parameters: MIME type and the actual content of the email, it then returns the JSON-ready representation of this content. |
| 22 | + |
| 23 | +``` |
| 24 | + mail = Mail(from_email, subject, to_email, content) |
| 25 | +``` |
| 26 | +After adding the above we create a mail object using `Mail` class, it takes the following parameters: email address to send from, subject line of emails, email address to send to, content of the message. |
| 27 | +For more information on parameters and usage, see [here](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py) |
| 28 | + |
| 29 | +### Creating Personalizations |
| 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. |
| 33 | +``` |
| 34 | + mock_personalization = Personalization() |
| 35 | + for to_addr in personalization['to_list']: |
| 36 | + mock_personalization.add_to(to_addr) |
| 37 | +``` |
| 38 | + |
| 39 | +### Creating Attachments |
| 40 | + |
| 41 | +To create attachments, we use the `Attachment` class and make sure the content is base64 encoded before passing it into attachment.content. |
| 42 | +``` |
| 43 | + attachment = Attachment() |
| 44 | + attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" |
| 45 | + "Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") |
| 46 | +``` |
| 47 | +Another example: [Link](https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md) |
| 48 | + |
| 49 | +### Managing Settings |
| 50 | + |
| 51 | +To configure settings in mail, you can use the `MailSettings` class. The class takes some [parameters](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail_settings.py#L1)(such as bcc_settings, bypass_list_management, footer_settings, sandbox_mode) |
| 52 | + |
| 53 | +To add tracking settings, you can add `TrackingSettings` class. See example [here](https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail_example.py#L118) and parameters and usage [here](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/tracking_settings.py). |
| 54 | + |
| 55 | +### Sending email |
| 56 | + |
| 57 | +After you have configured every component and added your own functions, you can send emails. |
| 58 | +``` |
| 59 | + sg = SendGridAPIClient() |
| 60 | + data = build_kitchen_sink() |
| 61 | + response = sg.client.mail.send.post(request_body=data) |
| 62 | +``` |
| 63 | +Make sure you have [environment variable](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key) set up! |
| 64 | +Full example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L203). |
| 65 | + |
| 66 | +### Using Dynamic Templates |
| 67 | +You can use dynamic (handlebars) transactional templates to make things easy and less time taking. To make this work, you should have dynamic template created within your SendGrid account. |
| 68 | + |
| 69 | +See Full example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L221). |
0 commit comments