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.
The example here 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, to_email, subject, 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
To create personalizations, you need a dictionary to store all your email components. See example here
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)
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
To configure settings in mail, you can use the MailSettings class. The class takes some parameters(such as bcc_settings, bypass_list_management, footer_settings, sandbox_mode)
To add tracking settings, you can add TrackingSettings class. See example here and parameters and usage here.
After you have configured every component and added your own functions, you can send emails.
sg = SendGridAPIClient()
data = build_kitchen_sink()
response = sg.send(data)
Make sure you have environment variable set up! Full example here.
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.