diff --git a/.gitignore b/.gitignore index 6ca1d6d..3bf28f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.db +mock_emails/ # Credit: https://github.com/github/gitignore/blob/master/Python.gitignore diff --git a/mhvdb2/mailer.py b/mhvdb2/mailer.py new file mode 100644 index 0000000..00c5bb6 --- /dev/null +++ b/mhvdb2/mailer.py @@ -0,0 +1,41 @@ +from mhvdb2 import app +import os +from datetime import datetime +import requests + + +def send(to, subject, body): + if app.debug: + mock_send(to, subject, body) + else: + mailgun_send(to, subject, body) + + +def mailgun_send(to, subject, body): + endpoint = "https://api.mailgun.net/v2/{}/messages".format(app["MAILGUN_DOMAIN"]) + auth = ("api", app["MAILGUN_API_KEY"]) + data = {"from": "MakeHackVoid ", + "to": to, + "subject": subject, + "text": body} + + requests.post(endpoint, auth=auth, data=data) + + +def mock_send(to, subject, body): + # Calculate path to save mock email. Format: YY-MM-DD-SSSSS.txt + now = datetime.now() + midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) + file = "{0:%y-%m-%d}-{1:05d}.txt".format(now, (now - midnight).seconds) + folder = "mock_emails" + path = os.path.join(folder, file) + + # Make sure folder exists + if not os.path.exists(folder): + os.makedirs(folder) + + # Write mock email to file + f = open(path, 'w+') + f.write("To: " + to + "\n") + f.write("Subject: " + subject + "\n\n") + f.write(body) diff --git a/mhvdb2/routes.py b/mhvdb2/routes.py index e529528..dc124f1 100644 --- a/mhvdb2/routes.py +++ b/mhvdb2/routes.py @@ -1,4 +1,4 @@ -from mhvdb2 import app +from mhvdb2 import app, mailer from flask import render_template, request, flash from mhvdb2.resources import payments, members @@ -43,8 +43,10 @@ def signup_post(): phone=phone), 400 members.create(name, email, phone) - flash("Thanks for registering!", "success") + flash("Thanks for registering!", "success") + mailer.send(email, "Welcome to MakeHackVoid!", + render_template("emails/signup.txt", name=name)) return signup_get() diff --git a/mhvdb2/templates/emails/signup.txt b/mhvdb2/templates/emails/signup.txt new file mode 100644 index 0000000..8c2612c --- /dev/null +++ b/mhvdb2/templates/emails/signup.txt @@ -0,0 +1,14 @@ +Hi {{name}}, + +Thanks for becoming a member of MakeHackVoid! + +Keep in touch with the MHV community by joining our mailing list, IRC channel, and Facebook group, as well as following us on Twitter and Facebook. Find the details for all these and more on our contacts page: https://makehackvoid.com/contacts + +MHV has a wiki which contains lots of information about the space, upcoming workshops & training, project logs, and lots more: https://wiki.makehackvoid.com + +Finally, please take a moment to read our various rules and policies - they are a little dry, but they make sure that MHV is a fun and safe place for everyone: https://wiki.makehackvoid.com/policy:start + + +Thanks, +MaHaVo +The MakeHackVoid Robot \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 46adc53..953d1a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ MarkupSafe==0.18 Werkzeug==0.9.4 itsdangerous==0.23 peewee==2.2.1 +requests==2.3.0