Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Created August 31, 2016 08:00
Show Gist options
  • Save cnicodeme/0fe6c7cd79aa14e2e3acd5f1d66221b7 to your computer and use it in GitHub Desktop.
Save cnicodeme/0fe6c7cd79aa14e2e3acd5f1d66221b7 to your computer and use it in GitHub Desktop.
SparkPost email sending script
from sparkpost import EmailMessage
email = EmailMessage('New message in your inbox!', template='welcome')
email.send()
import os, requests, mimetypes,
mimetypes.init()
def guess_mimetype(name):
type_found = mimetypes.guess_type(name)
if type_found:
return type_found[0] # Best guess
return 'text/plain'
class EmailMessage(object):
def __init__(self, subject, template=None, text=None, html=None):
self.data = {
'options': {
'open_tracking': True,
'click_tracking': True,
'transactional': False,
'inline_css': True
},
'recipients': [],
'content': {
'subject': subject,
'attachments': [],
'inline_images': [],
'text': None,
'html': None,
'headers': {
'X-VoilaNorbert-Version': 'v1'
}
}
}
self.set_from(os.getenv('CONTACT_NAME'), os.getenv('CONTACT_EMAIL')) # FIXME adapt this to your needs
if template:
# set html and text
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'emails') # FIXME adapt this to your needs
try:
txt_file = open(os.path.join(path, '{0}.txt'.format(template)))
self.data['content']['text'] = txt_file.read()
txt_file.close()
except IOError:
pass
try:
html_file = open(os.path.join(path, '{0}.html'.format(template)))
self.data['content']['html'] = html_file.read()
html_file.close()
except IOError:
pass
if text:
self.data['content']['text'] = text
if html:
self.data['content']['html'] = html
if not self.data['content']['text'] and not self.data['content']['html']:
raise ValueError('Unable to find the specified template.')
if self.data['content']['text'] and not self.data['content']['html']:
self.data['content']['html'] = markdown.markdown(self.data['content']['text'])
def set_from(self, from_name, from_email):
self.data['content']['from'] = {
'name': from_name,
'email': from_email
}
self.data['content']['reply-to'] = from_email
def _add(self, kind, name, content, type=None):
if not type:
type = guess_mimetype(name)
self.data['content'][kind].append({
'type': type,
'name': name,
'data': content.encode('base64')
})
def add_attachment(self, name, content, type=None):
self._add('attachments', name, content, type)
def add_inline(self, name, content, type=None):
self._add('inline_images', name, content, type)
def queue(self, name, email, substitution=None):
self.data['recipients'].append({
'address': {
'name': name,
'email': email
},
'substitution_data': substitution
})
def send_to(self, name, email, substitution=None, send_at=None):
self.data['recipients'] = []
self.queue(name, email, substitution)
return self.send(send_at)
# TODO Manage sending limit
def send(self, send_at=None):
if send_at:
self.data['options']['start_time'] = send_at.replace(microsecond=0).isoformat()
try:
requests.packages.urllib3.disable_warnings()
r = requests.post(
'https://api.sparkpost.com/api/v1/transmissions',
verify=False,
timeout=10,
headers={'Authorization': os.getenv('SPARKPOST_API_KEY')}, # FIXME adapt this to your needs
json=self.data
)
if r.status_code == 200:
return r.json() # ['results']['id']
except requests.exceptions.RequestException:
logging.exception('[SparkPost]')
return None
<p>Hi {{name}},<p>
<p>Just a simple message to show you how this script works :)<p>
<p>Best,<br />
Cyril</p>
Hi {{name}},
Just a simple message to show you how this script works :)
Best,
Cyril
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment