Created
July 28, 2015 18:01
-
-
Save heynemann/f3aaac2e1593b15889bc to your computer and use it in GitHub Desktop.
send mail.py
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import argparse | |
import logging | |
import json | |
import requests | |
from flask import ( | |
url_for, render_template | |
) | |
from hack_in_poa.app import create_app | |
from hack_in_poa.models.user import User | |
def main(): | |
logging.getLogger("requests").setLevel(logging.WARNING) | |
args = parse_arguments() | |
app = create_app(args.conf, debug=False, initialize_assets=False) | |
template = "subscription.html" | |
if args.resend: | |
template = "subscription_again.html" | |
if args.confirmed: | |
template = "subscription_confirmed.html" | |
if args.reproved: | |
template = "subscription_reproved.html" | |
with app.test_request_context('/register.html'): | |
if args.confirmed: | |
all_users = list(User.objects.filter(will_go=True, received_confirmation_email=None)) | |
for user in User.objects.filter(will_go=True, received_confirmation_email=False): | |
all_users.append(user) | |
else: | |
if args.reproved: | |
all_users = list(User.objects.filter(will_go=False)) | |
for user in User.objects.filter(will_go=None): | |
all_users.append(user) | |
else: | |
user_emails = app.config['AUTHORIZED_PARTICIPANTS'] | |
all_users = User.objects.filter(email__in=user_emails) | |
for user in all_users: | |
send_email_to(app, user, template) | |
if args.confirmed: | |
user.received_confirmation_email = True | |
user.save() | |
def parse_arguments(args=None): | |
if args is None: | |
args = sys.argv[1:] | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--conf', '-c', default='hack_in_poa/config/local.conf', help="Path to configuration file.") | |
parser.add_argument( | |
'--resend', '-r', action="store_true", default=False, help="Send to users that already received the first e-mail." | |
) | |
parser.add_argument( | |
'--confirmed', '-f', action="store_true", default=False, help="Send to users that confirmed participation." | |
) | |
parser.add_argument( | |
'--reproved', '-v', action="store_true", default=False, help="Send to users that didn't confirm participation." | |
) | |
options = parser.parse_args(args) | |
return options | |
def send_email_to(app, user, template): | |
url = "http://hackinpoa.globo.com%s" % url_for('index.register') | |
message_text = render_template(template, user=user, referral_url=url) | |
message = { | |
'key': app.config['MAIL_API_KEY'], | |
'async': False, | |
'message': { | |
'auto_html': False, | |
'auto_text': False, | |
'bcc_address': '[email protected]', | |
'from_email': '[email protected]', | |
'from_name': 'Hack In PoA 2015', | |
'google_analytics_campaign': '[email protected]', | |
'google_analytics_domains': ['hackinpoa.globo.com'], | |
'headers': {'Reply-To': '[email protected]'}, | |
'html': message_text, | |
'text': '', | |
'important': True, | |
'metadata': {'website': 'hackinpoa.globo.com'}, | |
'return_path_domain': 'hackinpoa.globo.com', | |
'subject': u'Hack In PoA 2015', | |
'tags': ['hackinpoa'], | |
'to': [{'email': user.email, | |
'name': user.name, | |
'type': 'to'}], | |
'track_clicks': True, | |
'track_opens': True, | |
'tracking_domain': 'hackinpoa.globo.com', | |
} | |
} | |
logging.info('Sending confirmation message to %s...' % user.email) | |
url = 'https://mandrillapp.com/api/1.0/messages/send.json' | |
r = requests.post(url, data=json.dumps(message)) | |
if r.status_code != 200: | |
# Mandrill errors are thrown as exceptions | |
logging.error('A mandrill error occurred: %s' % r.text) | |
return False | |
result = json.loads(r.text) | |
if result[0]['status'] in ['rejected', 'invalid']: | |
logging.info('Sending message to %s failed with: %s' % ( | |
user.email, result[0] | |
)) | |
return False | |
logging.info('Message sent successfully to %s' % user.email) | |
return True | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment