-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kane Kim
committed
Feb 28, 2012
0 parents
commit ad7ce66
Showing
17 changed files
with
1,058 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Copyright (c) 2012 SendGrid | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | ||
and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include README.md | ||
recursive-exclude test * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# 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 git@github.com:sendgrid/sendgrid-python.git | ||
``` | ||
|
||
Using Pypi: | ||
|
||
``` | ||
easy_install sendgrid-python | ||
``` | ||
|
||
## 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 | ||
1. Creating a SendGrid Mail object, and setting its data | ||
1. Sending the mail using either SMTP API or Web API. | ||
|
||
## Mail Usage ## | ||
|
||
```python | ||
import sendgrid | ||
|
||
s = sendgrid.Sendgrid('username', 'password', secure=True) | ||
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>") | ||
message.add_to("someone@example.com", "John Doe") | ||
|
||
s.web.send(message) | ||
``` | ||
|
||
Or | ||
|
||
```python | ||
s.smtp.send(message) | ||
``` | ||
|
||
### 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. | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "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. | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "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 | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, your code is %code%", "<b>Hello %name%, your code is %code%</b>") | ||
message.add_to( | ||
{ | ||
'example1@example.com': {'%name%': 'Name 1', '%code%': 'Code 1'}, | ||
'example2@example.com': {'%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. | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, you work at %place%", | ||
"<b>Hello %name%, your code is %code%, you work at %place%</b>") | ||
message.add_to( | ||
{ | ||
'example1@example.com': {'%name%': 'Name 1', '%place%': '%home%'}, | ||
'example2@example.com': {'%name%': 'Name 2', '%place%': '%office%'}, | ||
} | ||
).set_sections({"%office%": "an office", "%home%": "your house"}) | ||
``` | ||
|
||
### Using Unique Arguments ### | ||
|
||
Unique Arguments are used for tracking purposes | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "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. | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "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 ### | ||
|
||
Headers can be used to add existing sendgrid functionality (such as for categories or filters), or custom headers can be added as necessary. | ||
|
||
```python | ||
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>") | ||
message.add_header("category", "My New Category") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from sendgrid import * | ||
from message import * | ||
|
||
del sendgrid, message | ||
|
||
__version__ = "0.1.0" | ||
version_info = (0, 1, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SGServiceException(Exception): | ||
""" | ||
Sendgrid service error | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
try: | ||
import json | ||
except ImportError: | ||
import simplejson as json | ||
import re | ||
import textwrap | ||
|
||
class SmtpApiHeader(object): | ||
def __init__(self): | ||
self.data = {} | ||
self.re_split = re.compile('(["\]}])([,:])(["\[{])') | ||
|
||
|
||
def add_to(self, to): | ||
if not self.data.has_key('to'): | ||
self.data['to'] = [] | ||
if type(to) is str: | ||
self.data['to'] += [to] | ||
else: | ||
self.data['to'] += to | ||
|
||
|
||
def add_sub_val(self, var, val): | ||
if not self.data.has_key('sub'): | ||
self.data['sub'] = {} | ||
if type(val) is str: | ||
self.data['sub'][var] = [val] | ||
else: | ||
self.data['sub'][var] = val | ||
|
||
|
||
def set_unique_args(self, val): | ||
if type(val) is dict: | ||
self.data['unique_args'] = val | ||
|
||
|
||
def add_unique_arg(self, key, val): | ||
if not self.data.has_key('unique_args'): | ||
self.data['unique_args'] = {} | ||
self.data['unique_args'][key] = val | ||
|
||
|
||
def set_category(self, cat): | ||
self.data['category'] = [cat] | ||
|
||
|
||
def add_category(self, cat): | ||
if not self.data.has_key('category'): | ||
self.data['category'] = [] | ||
self.data['category'].append(cat) | ||
|
||
|
||
def add_section(self, key, section): | ||
if not self.data.has_key('section'): | ||
self.data['section'] = {} | ||
self.data['section'][key] = section | ||
|
||
|
||
def set_section(self, val): | ||
self.data['section'] = val | ||
|
||
|
||
def add_filter_setting(self, fltr, setting, val): | ||
if not self.data.has_key('filters'): | ||
self.data['filters'] = {} | ||
if not self.data['filters'].has_key(fltr): | ||
self.data['filters'][fltr] = {} | ||
if not self.data['filters'][fltr].has_key('settings'): | ||
self.data['filters'][fltr]['settings'] = {} | ||
self.data['filters'][fltr]['settings'][setting] = val | ||
|
||
|
||
def as_json(self): | ||
j = json.dumps(self.data) | ||
return self.re_split.sub('\1\2 \3', j) | ||
|
||
|
||
def as_string(self): | ||
j = self.as_json() | ||
str = 'X-SMTPAPI: %s' % textwrap.fill(j, subsequent_indent=' ', width=72) | ||
return str |
Oops, something went wrong.