|
| 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 | +``` |
0 commit comments