Git Product home page Git Product logo

sendgrid-python's Introduction

SendGrid-Python

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.

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 <[email protected]>')
message.set_subject('Example')
message.set_html('Body')
message.set_text('Body')
message.set_from('Doe John <[email protected]>')
status, msg = sg.send(message)

#or

message = sendgrid.Mail(to='[email protected]', subject='Example', html='Body', text='Body', from_email='[email protected]')
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('[email protected]')
# or
message.add_to('Example Dude <[email protected]>')
# or
message.add_to(['Example Dude <[email protected]>', '[email protected]'])

Adding BCC Recipients

message = sendgrid.Mail()
message.add_bcc('[email protected]')
# or
message.add_bcc(['Example Dude <[email protected]>', '[email protected]'])

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('[email protected]')

Set ReplyTo

message.sendgrid.Mail()
message.set_replyto('[email protected]')

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

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.

Substitution

message = sendgrid.Mail()
message.add_substitution("key", "value")

Section

message = sendgrid.Mail()
message.add_section("section", "value")

Category

message = sendgrid.Mail()
message.add_category("category")

Unique Arguments

message = sendgrid.Mail()
message.add_unique_arg("key", "value")

Filter

message = sendgrid.Mail()
message.add_filter("filter", "setting", "value")

TODO:

  • Add support for CID

Tests

python test/__init__.py

MIT License

sendgrid-python's People

Contributors

brandonmwest avatar faulkner avatar heitortsergent avatar iandouglas avatar jaymon avatar jhuang314 avatar keleshev avatar martyndavies avatar melandory avatar miphreal avatar motdotla avatar nathancahill avatar nquinlan avatar punkdata avatar tcg avatar tgornak avatar thechamp avatar theycallmeswift avatar userappio avatar yred avatar zmetcalf avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.