This library allows you to quickly and easily send emails through SendGrid using Python.
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.
pip install sendgrid
# or
easy_install sendgridimport 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)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.
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'])message = sendgrid.Mail()
message.add_bcc('example@email.com')
# or
message.add_bcc(['Example Dude <example@email.com>', 'john@email.com'])message = sendgrid.Mail()
message.set_subject('Example')message = sendgrid.Mail()
message.set_text('Body')
# or
message.set_html('<html><body>Stuff, you know?</body></html>')message = sendgrid.Mail()
message.set_from('example@email.com')message.sendgrid.Mail()
message.set_replyto('example@email.com')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 streamsSendGrid'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.
message = sendgrid.Mail()
message.add_substitution("key", "value")message = sendgrid.Mail()
message.add_section("section", "value")message = sendgrid.Mail()
message.add_category("category")message = sendgrid.Mail()
message.add_unique_arg("key", "value")message = sendgrid.Mail()
message.add_filter("filter", "setting", "value")- Add support for CID
python test/__init__.py