Skip to content

Latest commit

 

History

History
124 lines (88 loc) · 3.2 KB

File metadata and controls

124 lines (88 loc) · 3.2 KB
layout page
weight 0
title Python
navigation
show
true

This library allows you to quickly and easily send emails through SendGrid using Python. Full documentation for this library can be found on GitHub.

Warning! This library was recently updated to bring it up to date with all of our other libraries. It behaves completely different from the previous release. Also, SMTP has been deprecated in support for the Web API.

Example

{% codeblock lang:python %} import sendgrid

sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD')

message = sendgrid.Mail() message.add_to('John Doe [email protected]') message.set_subject('Example') message.set_html('Body?') message.set_text('Body?') message.set_from('Doe John [email protected]') sg.send(message)

#or

message = sendgrid.Mail(to='John Doe <[email protected]', subject='Example', html='Body?', text='Body?', from_email='[email protected]') sg.send(message)

{% endcodeblock %}

Adding Recipients

{% codeblock lang:python %} message = sendgrid.Mail() message.add_to('[email protected]')

or

message.add_to('Example Dude [email protected]') {% endcodeblock %}

Adding BCC Recipients

{% codeblock lang:python %} message = sendgrid.Mail() message.add_bcc('[email protected]') {% endcodeblock %}

Setting the Subject

{% codeblock lang:python %} message = sendgrid.Mail() message.set_subject('New email') {% endcodeblock %}

Set Text or HTML

{% codeblock lang:python %} message = sendgrid.Mail() message.set_text('Body')

or

message.set_html('Stuff, you know?') {% endcodeblock %}

Set From

{% codeblock lang:python %} message = sendgrid.Mail() message.set_from('[email protected]') {% endcodeblock %}

Set File Attachments

{% codeblock lang:python %} message = sendgrid.Mail() message.add_attachment('./stuff.txt')

or

message.add_attachment_stream('filename', 'somerandomcontentyouwant') {% endcodeblock %}

SendGrid's X-SMTPAPI

If you wish to use the X-SMTPAPI on your own app, you can use the SMTPAPI Python library.

There are implementations for setter methods too.

{% codeblock lang:python %} message = sendgrid.Mail() message.add_substitution("key", "value") {% endcodeblock %}

{% codeblock lang:python %} message = sendgrid.Mail() message.add_section("section", "value") {% endcodeblock %}

{% codeblock lang:python %} message = sendgrid.Mail() message.add_category("category") {% endcodeblock %}

{% codeblock lang:python %} message = sendgrid.Mail() message.add_unique_arg("key", "value") {% endcodeblock %}

{% codeblock lang:python %} message = sendgrid.Mail() message.add_filter("filter", "setting", "value") {% endcodeblock %}