SendGrid library for Python
Project description
This library allows you to quickly and easily send emails through SendGrid using Python.
Warning
If you upgrade to version 1.2.x, the add_to method behaves differently. In the past this method defaulted to using the SMTPAPI header. Now you must explicitly call the smtpapi.add_to method. More on the SMTPAPI section.
Install
pip install sendgrid
# or
easy_install sendgrid
Example
import sendgrid
sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD')
message = sendgrid.Mail()
message.add_to('John Doe <john@email.com>')
message.set_subject('Example')
message.set_html('Body')
message.set_text('Body')
message.set_from('Doe John <doe@email.com>')
status, msg = sg.send(message)
#or
message = sendgrid.Mail(to='john@email.com', subject='Example', html='Body', text='Body', from_email='doe@email.com')
status, msg = sg.send(message)
Error handling
By default, .send method returns a tuple (http_status_code, message), however you can pass raise_errors=True to SendGridClient constructor, then .send method will raise SendGridClientError for 4xx errors, and SendGridServerError for 5xx errors.
from sendgrid import SendGridError, SendGridClientError, SendGridServerError
sg = sendgrid.SendGridClient(username, password, raise_errors=True)
try:
sg.send(message)
except SendGridClientError:
...
except SendGridServerError:
...
This behavior is going to be default from version 1.0.0. You are encouraged to set raise_errors to True for forwards compatibility.
SendGridError is a base-class for all SendGrid-related exceptions.
Adding Recipients
message = sendgrid.Mail()
message.add_to('example@email.com')
# or
message.add_to('Example Dude <example@email.com>')
# or
message.add_to(['Example Dude <example@email.com>', 'john@email.com'])
Adding BCC Recipients
message = sendgrid.Mail()
message.add_bcc('example@email.com')
# or
message.add_bcc(['Example Dude <example@email.com>', 'john@email.com'])
Setting the Subject
message = sendgrid.Mail()
message.set_subject('Example')
Set Text or HTML
message = sendgrid.Mail()
message.set_text('Body')
# or
message.set_html('<html><body>Stuff, you know?</body></html>')
Set From
message = sendgrid.Mail()
message.set_from('example@email.com')
Set ReplyTo
message.sendgrid.Mail()
message.set_replyto('example@email.com')
Set File Attachments
message = sendgrid.Mail()
message.add_attachment('stuff.txt', './stuff.txt')
# or
message.add_attachment('stuff.txt', open('./stuff.txt', 'rb'))
# or
message.add_attachment_stream('filename', 'somerandomcontentyouwant')
# strings, unicode, or BytesIO streams
Set Content ID’s
message = sendgrid.Mail()
message.add_attachment('image.png', open('./image.png', 'rb'))
message.add_content_id('image.png', 'ID_IN_HTML')
message.set_html('<html><body>TEXT BEFORE IMAGE<img src="cid:ID_IN_HTML"></img>AFTER IMAGE</body></html>')
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.
Recipients
message = sendgrid.Mail()
message.smtpapi.add_to('example@email.com')
Substitution
message = sendgrid.Mail()
message.smtpapi.add_substitution('key', 'value')
# or
message.add_substitution('key', 'value')
Section
message = sendgrid.Mail()
message.smtpapi.add_section('section', 'value')
# or
message.add_section('section', 'value')
Category
message = sendgrid.Mail()
message.smtpapi.add_category('category')
# or
message.add_category('category')
Unique Arguments
message = sendgrid.Mail()
message.smtpapi.add_unique_arg('key', 'value')
# or
message.add_unique_arg('key', 'value')
Filter
message = sendgrid.Mail()
message.smtpapi.add_filter('filter', 'setting', 'value')
# or
message.add_filter('filter', 'setting', 'value')
Tests
python test/__init__.py
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sendgrid-1.2.0.macosx-10.10-intel.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cc35f5d62463a765c64db56806cc60086fcf334d862ce0122b98d3884157b1e |
|
MD5 | 69bf0298c01d0049d93365ca6f0304bc |
|
BLAKE2b-256 | 000fa12cf136deca1a2b8ab9b226f0917b3b7ab573f4a1f71fe37b826188963f |