Skip to content

Commit ad7ce66

Browse files
author
Kane Kim
committed
initial commit
0 parents  commit ad7ce66

17 files changed

Lines changed: 1058 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

LICENSE.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Copyright (c) 2012 SendGrid
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
6+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
9+
the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
12+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
14+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
15+
DEALINGS IN THE SOFTWARE.

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include README.md
2+
recursive-exclude test *

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# sendgrid-python #
2+
This library allows you to quickly and easily send emails through SendGrid using Python.
3+
4+
## License ##
5+
Licensed under the MIT License.
6+
7+
## Install ##
8+
9+
Using Github:
10+
11+
```
12+
git clone git@github.com:sendgrid/sendgrid-python.git
13+
```
14+
15+
Using Pypi:
16+
17+
```
18+
easy_install sendgrid-python
19+
```
20+
21+
## SendGrid APIs ##
22+
SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails.
23+
For an explanation of the benefits of each, refer to http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/.
24+
25+
This library implements a common interface to make it very easy to use either API.
26+
27+
## Mail Pre-Usage ##
28+
29+
Before we begin using the library, its important to understand a few things about the library architecture...
30+
31+
* Sending an email is as simple as :
32+
1. Creating a SendGrid Instance
33+
1. Creating a SendGrid Mail object, and setting its data
34+
1. Sending the mail using either SMTP API or Web API.
35+
36+
## Mail Usage ##
37+
38+
```python
39+
import sendgrid
40+
41+
s = sendgrid.Sendgrid('username', 'password', secure=True)
42+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
43+
message.add_to("someone@example.com", "John Doe")
44+
45+
s.web.send(message)
46+
```
47+
48+
Or
49+
50+
```python
51+
s.smtp.send(message)
52+
```
53+
54+
### Using Categories ###
55+
56+
Categories are used to group email statistics provided by SendGrid.
57+
58+
To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.
59+
60+
```python
61+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
62+
message.add_category(["Category 1", "Category 2"])
63+
```
64+
65+
66+
### Using Attachments ###
67+
68+
File attachments are limited to 7 MB per file.
69+
70+
```python
71+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
72+
message.add_attachment("file1.doc", "/path/to/file.doc").add_attachment("file2.nfo", "File 2 content")
73+
```
74+
75+
### Using Substitutions ###
76+
77+
Substitutions can be used to customize multi-recipient emails, and tailor them for the user
78+
79+
```python
80+
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, your code is %code%", "<b>Hello %name%, your code is %code%</b>")
81+
message.add_to(
82+
{
83+
'example1@example.com': {'%name%': 'Name 1', '%code%': 'Code 1'},
84+
'example2@example.com': {'%name%': 'Name 2', '%code%': 'Code 2'},
85+
}
86+
)
87+
```
88+
89+
### Using Sections ###
90+
91+
Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.
92+
93+
```python
94+
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, you work at %place%",
95+
"<b>Hello %name%, your code is %code%, you work at %place%</b>")
96+
message.add_to(
97+
{
98+
'example1@example.com': {'%name%': 'Name 1', '%place%': '%home%'},
99+
'example2@example.com': {'%name%': 'Name 2', '%place%': '%office%'},
100+
}
101+
).set_sections({"%office%": "an office", "%home%": "your house"})
102+
```
103+
104+
### Using Unique Arguments ###
105+
106+
Unique Arguments are used for tracking purposes
107+
108+
```python
109+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
110+
message.add_unique_argument("Customer", "Someone")
111+
```
112+
113+
### Using Filter Settings ###
114+
115+
Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
116+
117+
```python
118+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
119+
message.add_filter_setting("footer", "text/plain", "Here is a plain text footer")
120+
message.add_filter_setting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>")
121+
```
122+
123+
### Using Headers ###
124+
125+
Headers can be used to add existing sendgrid functionality (such as for categories or filters), or custom headers can be added as necessary.
126+
127+
```python
128+
message = sendgrid.Message("from@mydomain.com", "subject", "plain body", "<b>Html here</b>")
129+
message.add_header("category", "My New Category")
130+
```

sendgrid/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sendgrid import *
2+
from message import *
3+
4+
del sendgrid, message
5+
6+
__version__ = "0.1.0"
7+
version_info = (0, 1, 0)

sendgrid/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class SGServiceException(Exception):
2+
"""
3+
Sendgrid service error
4+
"""
5+
pass

sendgrid/header.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
try:
2+
import json
3+
except ImportError:
4+
import simplejson as json
5+
import re
6+
import textwrap
7+
8+
class SmtpApiHeader(object):
9+
def __init__(self):
10+
self.data = {}
11+
self.re_split = re.compile('(["\]}])([,:])(["\[{])')
12+
13+
14+
def add_to(self, to):
15+
if not self.data.has_key('to'):
16+
self.data['to'] = []
17+
if type(to) is str:
18+
self.data['to'] += [to]
19+
else:
20+
self.data['to'] += to
21+
22+
23+
def add_sub_val(self, var, val):
24+
if not self.data.has_key('sub'):
25+
self.data['sub'] = {}
26+
if type(val) is str:
27+
self.data['sub'][var] = [val]
28+
else:
29+
self.data['sub'][var] = val
30+
31+
32+
def set_unique_args(self, val):
33+
if type(val) is dict:
34+
self.data['unique_args'] = val
35+
36+
37+
def add_unique_arg(self, key, val):
38+
if not self.data.has_key('unique_args'):
39+
self.data['unique_args'] = {}
40+
self.data['unique_args'][key] = val
41+
42+
43+
def set_category(self, cat):
44+
self.data['category'] = [cat]
45+
46+
47+
def add_category(self, cat):
48+
if not self.data.has_key('category'):
49+
self.data['category'] = []
50+
self.data['category'].append(cat)
51+
52+
53+
def add_section(self, key, section):
54+
if not self.data.has_key('section'):
55+
self.data['section'] = {}
56+
self.data['section'][key] = section
57+
58+
59+
def set_section(self, val):
60+
self.data['section'] = val
61+
62+
63+
def add_filter_setting(self, fltr, setting, val):
64+
if not self.data.has_key('filters'):
65+
self.data['filters'] = {}
66+
if not self.data['filters'].has_key(fltr):
67+
self.data['filters'][fltr] = {}
68+
if not self.data['filters'][fltr].has_key('settings'):
69+
self.data['filters'][fltr]['settings'] = {}
70+
self.data['filters'][fltr]['settings'][setting] = val
71+
72+
73+
def as_json(self):
74+
j = json.dumps(self.data)
75+
return self.re_split.sub('\1\2 \3', j)
76+
77+
78+
def as_string(self):
79+
j = self.as_json()
80+
str = 'X-SMTPAPI: %s' % textwrap.fill(j, subsequent_indent=' ', width=72)
81+
return str

0 commit comments

Comments
 (0)