Code Monkey home page Code Monkey logo

sendgrid-python's Introduction

sendgrid-python

This library allows you to quickly and easily send emails through SendGrid using Python.

License

Licensed under the MIT License.

Install

Using Github:

git clone [email protected]:sendgrid/sendgrid-python.git

Using Pypi:

easy_install sendgrid

SendGrid APIs

SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails. For an explanation of the benefits of each, refer to http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/.

This library implements a common interface to make it very easy to use either API.

Mail Pre-Usage

Before we begin using the library, its important to understand a few things about the library architecture...

  • Sending an email is as simple as :
    1. Creating a SendGrid Instance
    2. Creating a SendGrid Mail object, and setting its data
    3. Sending the mail using either SMTP API or Web API.

Mail Usage

import sendgrid

s = sendgrid.Sendgrid('username', 'password', secure=True)
message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_to("[email protected]", "John Doe")

s.web.send(message)

Or

s.smtp.send(message)

To set the "from name", you must pass the address and name in a tuple, like this:

message = sendgrid.Message(("[email protected]", "Joe Smith"), "subject", "plain body", "<b>Html here</b>")

Using Categories

Categories are used to group email statistics provided by SendGrid.

To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.

message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_category(["Category 1", "Category 2"])

Using Attachments

File attachments are limited to 7 MB per file.

message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_attachment("file1.doc", "/path/to/file.doc").add_attachment("file2.nfo", "File 2 content")

Using Substitutions

Substitutions can be used to customize multi-recipient emails, and tailor them for the user

message = sendgrid.Message("[email protected]", "subject", "Hello %name%, your code is %code%", "<b>Hello %name%, your code is %code%</b>")
message.add_to(
    {
        '[email protected]': {'%name%': 'Name 1', '%code%': 'Code 1'},
        '[email protected]': {'%name%': 'Name 2', '%code%': 'Code 2'},
    }
)

Using Sections

Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.

message = sendgrid.Message("[email protected]", "subject", "Hello %name%, you work at %place%",
    "<b>Hello %name%, you work at %place%</b>")
message.add_to(
    {
        '[email protected]': {'%name%': 'Name 1', '%place%': '%home%'},
        '[email protected]': {'%name%': 'Name 2', '%place%': '%office%'},
    }
).set_sections({"%office%": "an office", "%home%": "your house"})

Using Unique Arguments

Unique Arguments are used for tracking purposes

message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_unique_argument("Customer", "Someone")

Using Filter Settings

Filter Settings are used to enable and disable apps, and to pass parameters to those apps.

message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_filter_setting("footer", "text/plain", "Here is a plain text footer")
message.add_filter_setting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>")

Using Headers

Custom headers can be added as necessary.

message = sendgrid.Message("[email protected]", "subject", "plain body", "<b>Html here</b>")
message.add_header("X-Mailer", "MyApp")

sendgrid-python's People

Contributors

theycallmeswift avatar faulkner avatar iandouglas avatar

Watchers

James Cloos avatar Fresh Relevance 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.