|
| 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 | +``` |
0 commit comments