You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails, but the Web API has less communication overhead. For an explanation of the benefits of each, refer to http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/.
24
25
25
26
This library implements a common interface to make it very easy to use either API.
26
27
27
28
## Mail Pre-Usage ##
28
29
29
-
Before we begin using the library, its important to understand a few things about the library architecture...
30
+
Before we begin using the library, its important to understand a few things about the library architecture:
30
31
31
32
* Sending an email is as simple as :
32
33
1. Creating a SendGrid Instance
33
34
1. Creating a SendGrid Mail object, and setting its data
34
-
1. Sending the mail using either SMTP API or Web API.
35
+
1. Sending the mail using either SMTP API or Web API
35
36
36
37
## Mail Usage ##
37
38
38
39
```python
39
40
import sendgrid
40
41
42
+
# make a secure connection to SendGrid
41
43
s = sendgrid.Sendgrid('username', 'password', secure=True)
Using the message.add_to() method, you can add recipient email address (optionally with names), but you can also add CC/BCC recipient addresses (without names) using message.add_cc() and message.add_bcc().
79
+
80
+
Note: Only the SMTP API supports CC at this time, though we have code and hooks in place for the Web API implementation of this library for future use.
81
+
82
+
Both the Web API and SMTP API support BCC.
83
+
84
+
The message.add_cc() and message.add_bcc() calls support passing a single address, or a list of addresses, as shown in the examples below.
# send message to To, CC and BCC recipients using SMTP API
51
116
s.smtp.send(message)
117
+
118
+
# send message to To and BCC recipients using Web API (no CC support)
119
+
s.web.send(message)
120
+
```
121
+
Note: Using CC/BCC with SMTP API requires v0.1.3 or higher
122
+
123
+
124
+
## Using Attachments ###
125
+
126
+
Attaching files to your message uses the message.add_attachment() method. This method takes two parameters, the intended name of the attachment you want your recipients to see, and the full file system path to the file. Note: File attachments are limited to 7 MB per file, and your total message size must be under 20MB.
A common problem with file attachments is that the second parameter does not exist as a file, in which case the library will attach a 0-byte blank file. Here's a simple check to assist you:
140
+
141
+
```python
142
+
import sendgrid
143
+
import os
144
+
145
+
s = sendgrid.Sendgrid('username', 'password', secure=True)
An optional third parameter can be passed to the message.add_attachment() call which lets you specify a Content-ID header for each file. The Content-ID is used to reference attached files (typically images) within the HTML message. For example:
To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.
165
+
166
+
## Using Categories ###
167
+
168
+
You can mark messages with optional categories to give better visibility to email statistics (opens, clicks, etc.). You can add up to 10 categories per email message. You can read more about Categories here: http://docs.sendgrid.com/documentation/delivery-metrics/categories/
169
+
170
+
To add categories to your message, use the message.add_category() method and pass a list of one or more category names. SendGrid will begin tracking statistics with these category names if the category name is new, or aggregate statistics for existing category names.
Unique Arguments are used for tracking purposes on the message, and can be seen in the Email Activity screen on your account dashboard or through the Event API. Use the message.add_unique_argument() method, which takes two parameters, a key and a value. To pass multiple keys/values, use message.add_unique_arguments() (note the plural method name) and pass a dictionary of key/value pairs. More information can be found here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/unique-arguments/
Substitutions can be used to customize multi-recipient emails, and tailor them for the user
197
+
## Using Substitutions ###
198
+
199
+
SendGrid also allows you to send multi-recipient messages with unique information per recipient. This is commonly used for sending unique URLs or codes to a list of recipients in a single batch. You simply expand the data you pass to the message.add_to() method like the example below. You can read more about Substitutions here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/substitution-tags/
78
200
79
201
```python
80
202
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, your code is %code%", "<b>Hello %name%, your code is %code%</b>")
@@ -88,7 +210,7 @@ message.add_to(
88
210
89
211
### Using Sections ###
90
212
91
-
Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.
213
+
Used in conjunction with Substitutions, Sections can be used to further customize messages for the end users, and acts like a second tier of substitution data. You can use message.set_section() to add a single section, or a pluralized message.set_sections() method to add several sections. You can read more about using Sections here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/section-tags/
92
214
93
215
```python
94
216
message = sendgrid.Message("from@mydomain.com", "subject", "Hello %name%, you work at %place%",
Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
238
+
## Using Filter Settings ###
239
+
240
+
Filter Settings are used to enable and disable apps, and to pass parameters to those apps. You can read more here: http://docs.sendgrid.com/documentation/api/smtp-api/filter-settings/
241
+
Here's an example of passing content to the 'footer' app:
0 commit comments