Skip to content
Merged
Changes from 11 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f7732dd
Fixed syntax errors in Kitchen sink Python example code
vinayak42 Oct 5, 2018
a25435e
Fixed ModuleNotFoundError
vinayak42 Oct 5, 2018
5fc6178
Reverted changes to files in /sendgrid
vinayak42 Oct 5, 2018
88ddc58
fixed changes suggested by grammerly
xeon-zolt Oct 5, 2018
f90ece8
Test when env format is invalid, it should not set env
Oct 6, 2018
2ba4666
Correct assertion and improve Python coding style a bit
Oct 6, 2018
fb8d334
troubleshooting broken link fix
arshadkazmi42 Oct 6, 2018
935a4d7
fixed changes suggested
xeon-zolt Oct 15, 2018
9ffe918
Create README.md
tulikavijay Oct 16, 2018
46b7e4d
Update verbiage from to "Authentication"
crweiner Oct 17, 2018
8ea9a44
Change link to Domain Authentication
crweiner Oct 17, 2018
2793955
removed similar data and kept fixes
xeon-zolt Oct 17, 2018
6551f9a
Remove "Whitelist" and replace with other terms
crweiner Oct 17, 2018
f49af64
Correct spelling and anchor links
crweiner Oct 17, 2018
ad7a96a
Change path and file to "senderauthentication"
crweiner Oct 17, 2018
54f1d30
corrected links in CodeofConduct. Hacktoberfest.
bhavinjawade Oct 18, 2018
3363e01
Add unit tests for spam check
pyasi Oct 19, 2018
0f7e836
Test description
pyasi Oct 19, 2018
c9325ea
PEP8 Fixes and String Formatting Enhancement
vkmrishad Oct 20, 2018
568e5b0
Update examples/helpers/README.md
misterdorm Oct 20, 2018
c0b5081
Update examples/helpers/README.md
misterdorm Oct 20, 2018
b0778e8
Update examples/helpers/README.md
misterdorm Oct 20, 2018
d923a4b
Update examples/helpers/README.md
misterdorm Oct 20, 2018
759abbb
Update examples/helpers/README.md
misterdorm Oct 20, 2018
073fd38
Update examples/helpers/README.md
misterdorm Oct 20, 2018
682d7df
Update examples/helpers/README.md
misterdorm Oct 20, 2018
6b27879
Update examples/helpers/README.md
misterdorm Oct 20, 2018
f4ac98c
Add remaining sections
tulikavijay Oct 20, 2018
22c5b05
Merge pull request #692 from pyasi/add-tests-for-spam-check
thinkingserious Oct 22, 2018
3de0d15
Merge pull request #690 from bhavinjawade/master
thinkingserious Oct 22, 2018
b046de1
Merge pull request #687 from crweiner/whitelabel
thinkingserious Oct 22, 2018
d1cc74a
Merge pull request #638 from vinayak42/master
thinkingserious Oct 23, 2018
dd4c66a
Merge pull request #643 from zkan/add_more_tests_to_increase_code_cov…
thinkingserious Oct 23, 2018
cedb4cc
Merge pull request #647 from arshadkazmi42/troubleshooting-broken-link
thinkingserious Oct 23, 2018
5599acf
Merge branch 'master' into master
xeon-zolt Oct 25, 2018
4762039
Updated file with Master branch
vkmrishad Oct 30, 2018
1536f51
updated README.md
rahulpuroht Oct 30, 2018
2b6f07e
Merge pull request #697 from vkmrishad/vkmrishad-patch
thinkingserious Oct 30, 2018
d3791b0
Merge pull request #683 from tulikavijay/patch-1
thinkingserious Oct 30, 2018
90cfbf2
Merge pull request #640 from xeon-zolt/master
thinkingserious Oct 30, 2018
49b9e3f
Merge pull request #722 from rahulpuroht/rahulpuroht-patch-1
thinkingserious Nov 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions examples/helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Using helper class to send emails
You can use helper classes to customize the process of sending emails using SendGrid. Each process (such as sending a mock email,
building attachments, configuring settings, building personalizations, etc.) are made easy using helpers. All you need is a file with
all the classes imported and you can start sending emails!

> Note: You will need move this file to the root directory of this project to execute properly.

### Creating a simple email object and sending it
The example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L9)
defines minimum requirement to send an email.
```
from_email = Email("test@example.com")
subject = "Hello World from the SendGrid Python Library"
to_email = Email("test@example.com")
```
You can use `Email` class to define a mail id.

```
content = Content("text/plain", "some text here")
```
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.

```
mail = Mail(from_email, subject, to_email, content)
```
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.
For more information on parameters and usage, see [here](https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py)

### Creating Personalizations

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)
After creating a dictionary, you can go ahead and create a `Personalization` object.
```
mock_personalization = Personalization()
for to_addr in personalization['to_list']:
mock_personalization.add_to(to_addr)
```

### Creating Attachments

To create attachments, we use the `Attachment` class and make sure the content is base64 encoded before passing it into attachment.content.
```
attachment = Attachment()
attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl"
"Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")
```
Another example: [Link](https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md)

### Managing Settings

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)

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).

### Sending email

After you have configured every component and added your own functions, you can send emails.
```
sg = SendGridAPIClient()
data = build_kitchen_sink()
response = sg.client.mail.send.post(request_body=data)
```
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!
Full example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L203).

### Using Dynamic Templates
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.

See Full example [here](https://github.com/sendgrid/sendgrid-python/blob/0b683169b08d3a7c204107cd333be33053297e74/examples/helpers/mail_example.py#L221).