Skip to content

Instantly share code, notes, and snippets.

@arikfr

arikfr/mailer.py Secret

Created February 9, 2016 20:45
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save arikfr/a730451c01d1dd326412 to your computer and use it in GitHub Desktop.
Save arikfr/a730451c01d1dd326412 to your computer and use it in GitHub Desktop.
Re:dash CSV mailer
"""
Example execution:
python mailer.py --query-id 2226 --query-api-key 6764573853b4065aac3258b8c7bc561dc4179092 --org-slug default \
--smtp-username "..." --smtp-password '...' --smtp-host 'smtp.mailgun.org' \
--sender 'Arik <arik@redash.io>' --to 'arik@redash.io'
--subject "Test" --body-html-file html --body-text-file text
"""
import argparse
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import requests
def get_query_csv(org_slug, query_id, api_key):
path = 'https://app.redash.io/{}/api/queries/{}/results.csv?api_key={}'.format(org_slug, query_id, api_key)
return requests.get(path).text
def _fix_recipients_list(recipients_list):
if not isinstance(recipients_list, basestring):
recipients_list = ','.join(recipients_list)
return recipients_list
def create_mime_message(sender, to, subject, html_body, text_body, attachments=None, bcc=None):
attachments = attachments or {}
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = _fix_recipients_list(to)
if bcc:
msg['Bcc'] = _fix_recipients_list(bcc)
text_part = MIMEText(text_body, 'plain')
html_part = MIMEText(html_body, 'html')
msg.attach(text_part)
msg.attach(html_part)
for filename, attachment in attachments.iteritems():
part = MIMEApplication(attachment.encode('utf-8'))
part.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(part)
return msg
def send_mime_message(host, user, password, sender, recipients, mime_message):
smtp = smtplib.SMTP(host)
smtp.starttls()
smtp.login(user, password)
smtp.sendmail(sender, recipients, mime_message.as_string())
smtp.quit()
def load_file(path):
with open(path) as f:
return f.read()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Mail Re:dash CSV query by email.')
parser.add_argument('--smtp-username')
parser.add_argument('--smtp-password')
parser.add_argument('--smtp-host')
parser.add_argument('--sender', help='From (sender) address')
parser.add_argument('--to', help='To address(es)', nargs='+')
parser.add_argument('--bcc', help='Bcc address(es)', nargs='*', default=[])
parser.add_argument('--subject', help='Subject line.')
parser.add_argument('--body-html-file', help='Body html file path.')
parser.add_argument('--body-text-file', help='Body text file path.')
parser.add_argument('--org-slug', help='Query id to export')
parser.add_argument('--query-id', help='Query id to export')
parser.add_argument('--query-api-key', help='API key for the query')
args = parser.parse_args()
query_csv = get_query_csv(args.org_slug, args.query_id, args.query_api_key)
attachment_name = 'report_{}.csv'.format(datetime.datetime.now().strftime('%Y_%m_%d'))
attachments = {
attachment_name: query_csv
}
html = load_file(args.body_html_file)
text = load_file(args.body_text_file)
message = create_mime_message(args.sender, args.to, args.subject, html, text, attachments, args.bcc)
send_mime_message(args.smtp_host, args.smtp_username, args.smtp_password,
args.sender, args.to + args.bcc, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment