Created
November 21, 2014 04:39
-
-
Save kpx-dev/99599c636b87f9a1b952 to your computer and use it in GitHub Desktop.
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
""" | |
This Python code snippet will show you how to get work with SendGrid filter | |
settings | |
install the amazing requests library | |
$ pip install requests==2.4.3 | |
export your SendGrid credential from command line | |
$ export SG_USERNAME=your_sg_username | |
$ export SG_PASSWORD=your_sg_password | |
save this file as python filter_settings.py and run it | |
$ python filter_settings.py | |
gist link: | |
""" | |
import requests | |
import os | |
SG_URL = "https://api.sendgrid.com/api" | |
def build_url(endpoint, params=None): | |
url = "{}/{}.json?api_user={}&api_key={}".format( | |
SG_URL, | |
endpoint, | |
os.environ['SG_USERNAME'], | |
os.environ['SG_PASSWORD']) | |
if params: | |
for k, v in params.iteritems(): | |
url += "&{}={}".format(k, v) | |
return url | |
if __name__ == '__main__': | |
url = build_url('filter.setup') | |
# Address whitelist setting | |
print "Updating address whitelist setting..." | |
data = {"name": "addresswhitelist", "list": ["[email protected]", | |
"[email protected]"]} | |
res = requests.post(url, data) | |
# print res.json() | |
# BCC setting | |
print "Updating BCC setting..." | |
data = {"name": "bcc", "email": "[email protected]"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Click tracking setting | |
print "Updating Click tracking setting..." | |
data = {"name": "clicktrack", "enable_text": 1} | |
res = requests.post(url, data) | |
# print res.json() | |
# DKIM setting | |
print "Updating DKIM setting..." | |
data = {"name": "dkim", "domain": "example.com"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Domain keys setting | |
print "Updating domain keys setting..." | |
data = {"name": "domainkeys", "domain": "example.com"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Event notify setting | |
print "Updating event notify setting..." | |
data = {"name": "eventnotify", "processed": 1, "url": "http://example.com"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Footer setting | |
print "Updating footer setting..." | |
data = {"name": "footer", "text/html": "<h1>Hi</h1>"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Google Analytics setting | |
print "Updating Google Analytics setting..." | |
data = {"name": "ganalytics", "utm_source": "transactional"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Spam check setting | |
print "Updating spam check setting..." | |
data = {"name": "spamcheck", "url": "http://example.com"} | |
res = requests.post(url, data) | |
# print res.json() | |
# Subscription tracking setting | |
print "Updating subscription tracking setting..." | |
data = {"name": "subscriptiontrack", "url": "http://example.com"} | |
res = requests.post(url, data) | |
# print res.json() | |
# SendGrid for New Relic | |
print "Updating New Relic setting..." | |
data = {"name": "newrelic", "license_key": "xxxx"} | |
res = requests.post(url, data) | |
# print res.json() | |
# SendGrid for Sendwithus | |
print "Updating Sendwithus setting..." | |
data = {"name": "sendwithus", "license_key": "xxxx"} | |
res = requests.post(url, data) | |
# print res.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment