| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
pip install sendwithus
For all examples, assume:
import sendwithus
api = sendwithus.api(api_key='YOUR-API-KEY')By default, the API calls return a response object. However, you can use sendwithus.api(api_key='YOUR-API-KEY', raise_errors=True) which will raise the following errors:
Errors can be imported from the sendwithus.exceptions module.
api.templates()api.get_template(
'YOUR-TEMPLATE-ID'
)api.get_template(
'YOUR-TEMPLATE-ID',
version='YOUR-VERSION-ID'
)api.create_template(
name='Email Name',
subject='Email Subject',
html='<html><head></head><body>Valid HTML</body></html>',
text='Optional text content',
preheader='Optional preheader',
template_data={'optional': 'data'}
)api.create_new_locale(
'YOUR-TEMPLATE-ID',
locale='fr-FR',
version_name='Version Name',
subject='Email Subject',
html='<html><head></head><body>Valid HTML</body></html>',
text='Optional text content',
preheader='Optional preheader',
template_data={'optional': 'data'}
)api.create_new_version(
template_id='YOUR-TEMPLATE-ID',
name='Version Name',
subject='Email Subject',
html='<html><head></head><body>Valid HTML</body></html>',
text='Optional text content',
preheader='Optional preheader',
template_data={'optional': 'data'},
locale='fr-FR'
)api.update_template_version(
template_id='YOUR-TEMPLATE-ID',
version_id='YOUR-VERSION-ID',
name='Email Name'
subject='Email Subject',
html='<html><head></head><body>Valid HTML</body></html>',
text='Optional text content',
preheader='Optional preheader',
template_data={'optional': 'data'}
)We validate all HTML and will return an error if it's invalid.
r.status_code
# 400
r.content
# 'email html failed to validate'NOTE - If a customer does not exist by the specified email (recipient address), the send call will create a customer.
The email_data field is optional, but highly recommended!
r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'address': 'us@sendwithus.com'
}
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'address': 'us@sendwithus.com'
},
email_data={
'first_name': 'Matt'
}
)
print r.status_code
# 200The sender['address'] is a required sender field. sender['name'] and sender['reply_to'] are both optional.
r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
email_data={
'first_name': 'Matt'
},
sender={
'address': 'company@company.com',
'reply_to':'info@company.com', # Optional
'name': 'Company' # Optional
}
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
cc=[
{'address': 'company@company.com'},
{'address': 'info@company.com'}
]
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
bcc=[
{'address': 'company@company.com'},
{'address': 'info@company.com'}
]
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
headers={
'X-HEADER-ONE': 'header-value'
}
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
esp_account='YOUR-ESP-ID'
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
files=[
open('/home/Matt/report1.txt', 'r'),
open('/home/Matt/report2.txt', 'r')
]
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
files=[{
'file': open('/home/Matt/report1.txt', 'r'),
'filename': 'arbitrary_file_name.xyz'
}]
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
inline=open('image.jpg', 'r')
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
inline={
'file': open('/home/Matt/image.jpg', 'r'),
'filename': 'cool_image.jpg'
}
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
locale='fr-FR'
)
print r.status_code
# 200r = api.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'name': 'Matt',
'address': 'us@sendwithus.com'
},
email_version_name='Version A'
)
print r.status_code
# 200List all drip campaigns for the current profile
api.list_drip_campaigns()Starts a customer on the first step of a specified drip campaign
api.start_on_drip_campaign(
'dc_1234asdf1234',
{'address': 'customer@email.com'}
)You may specify extra data to be merged into the templates in the drip campaign.
Note &emdash; Any data provided in the email_data parameter for start_on_drip_campaign() will be used throughout the entire drip campaign.
api.start_on_drip_campaign(
'dc_1234asdf1234',
{'address': 'customer@email.com'},
email_data={'color': 'blue'},
sender={'address': 'from@email.com'},
cc=[{'address': 'cc@email.com'}],
tags=['tag_one', 'tag_two'],
esp_account='esp_1234',
locale='fr-FR'
)Deactivates all pending emails for a customer on a specified drip campaign
api.remove_from_drip_campaign(
'customer@email.com',
'dc_1234asdf1234'
)You can deactivate all pending drip campaign emails for a customer
api.drip_deactivate(
'customer@example.com'
)api.remove_from_drip_campaign(
'customer@example.com',
'dc_1234asdf1234'
)api.drip_campaign_details(
'dc_1234asdf1234'
)api.customer_details(
'customer@example.com'
)You can use the same endpoint to create or update a customer. This is primarily used to associate a locale with an email address so Sendwithus can automatically send them the correct template. Note that if your templates are only in one language then you don't need to use this feature.
api.customer_create(
'customer@example.com',
locale="fr-FR"
)api.customer_delete(
'customer@example.com'
)api.snippets()api.get_snippet(
'snp_1234asdf1234'
)api.create_snippet(
name='My Snippet',
body='<h1>Snippets!</h1>'
)api.update_snippet(
'snp_1234asdf1234',
name='My Snippet',
body='<h1>Snippets!</h1>'
)api.get_translation_template(
`my_translation_tag`
)with open('my_translation_file', 'rb') as f:
api.create_translation_file(
`my_translation_tag`,
f.read()
)The Render API allows you to render a template with data, using the exact same rendering workflow that Sendwithus uses when delivering your email. Strict is set to False as a default, if Strict=True this API call will fail on any missing email_data.
api.render(
email_id='YOUR-TEMPLATE-ID',
email_data={
'amount': '$12.00'
},
locale='fr-FR',
version_name='French-Version',
strict=False
)Sendwithus' API has a batch endpoint that can take multiple requests and execute them concurrently. Note that Sendwithus recommends keeping batch requests to 10 items or less.
api = sendwithus.api('YOUR-API-KEY')
batch = api.start_batch()
emails = [
'user+1@example.com',
'user+2@example.com',
# ...
'user+10@example.com'
]
# Generate the batch request
for email in emails:
batch.send(
email_id='YOUR-TEMPLATE-ID',
recipient={
'address': email
}
)
print batch.command_length() # show number of items in the batch request
results = batch.execute() # returns a `requests` Response Object >>> r.status_code
200
>>> r.json().get('success')
True
>>> r.json().get('status')
u'OK'
>>> r.json().get('receipt_id')
u'numeric-receipt-id' >>> r.status_code
400 >>> r.status_code
403Use tox to run the tests:
toxThis assumes you have tox installed and used pyenv to install multiple versions of python.
Once all the supported python versions are installed simply run:
toxThis will run the tests against all the versions specified in tox.ini.
Debug mode prints out the underlying request information as well as the data payload that gets sent to Sendwithus. You will most likely find this information in your logs. To enable it, simply put DEBUG=True as a parameter when instantiating the API object. Use the debug mode to compare the data payload getting sent to Sendwithus' API docs.
import sendwithus
api = sendwithus.api(api_key='YOUR-API-KEY', DEBUG=True)Sendwithus' API typically sends responses back in these ranges:
If you're receiving an error in the 400 response range follow these steps:
To package
python setup.py sdist bdist_wheel upload| Back | FazBrowse Home | New Git URL |