Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Mail class to make sending emails easier

  • Loading branch information...
commit bb8c54053cea2d90a969425acbca980f0019c212 1 parent cffe4e5
@opatut opatut authored
View
10 Makefile
@@ -0,0 +1,10 @@
+default: run
+
+setup:
+ ./setup.sh
+
+run:
+ ./run.sh
+
+kill:
+ . env/bin/activate && python2 kill-database.py
View
4 flamejam.cfg.example
@@ -2,6 +2,9 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/flamejam.db'
SQLALCHEMY_ECHO = False
SECRET_KEY = 'SUPERSECRET'
+MAIL_ENABLED = False
+#MAIL_SENDER = ("BaconGameJam", "noreply@bacongamejam.org")
+
# See http://packages.python.org/Flask-Mail/
#MAIL_SERVER = 'localhost'
#MAIL_PORT = 25
@@ -14,3 +17,4 @@ SECRET_KEY = 'SUPERSECRET'
#DEFAULT_MAX_EMAILS = None
#MAIL_FAIL_SILENTLY = True
#MAIL_SUPPRESS_SEND = False
+
View
59 flamejam/mail.py
@@ -0,0 +1,59 @@
+from flamejam import app
+from flamejam.models import Participant
+from flaskext.mail import Message
+from flask import render_template
+
+class Mail(object):
+ def __init__(self, subject):
+ self.recipients = []
+ self.content = ""
+ self.subject = subject
+
+ def addRecipients(self, recipients):
+ self.addRecipient(recipients)
+
+ def addRecipient(self, recipient):
+ t = type(recipient)
+ if t is list:
+ for r in recipient:
+ self.addRecipients(r)
+ elif t is Participant:
+ self.addRecipient(recipient.email)
+ elif t is str or str(recipient):
+ self.recipients.append(str(recipient))
+ else:
+ raise Exception("Only User objects, email address strings or lists of these are allowed.")
+
+ def setContent(self, content):
+ self.content = content
+
+ def render(self, template, *args, **kwargs):
+ self.content = render_template(template, *args, **kwargs)
+
+
+ def send(self):
+ if not self.recipients:
+ raise Exception("No email recipients set.")
+ if not self.subject:
+ raise Exception("No email subject set.")
+ if not self.content:
+ raise Exception("No email content set.")
+
+ if app.config["MAIL_ENABLED"]:
+ if not app.config["MAIL_SENDER"]:
+ raise Exception("No email sender set in config (MAIL_SENDER).")
+
+ with mail.connect() as connection:
+ for recipient in self.recipients:
+ msg = Message(self.subject, recipients = [recipient], sender = app.config["MAIL_SENDER"])
+ msg.html = self.content
+ connection.send(msg)
+ else:
+ print "Sending mail to: "
+ for p in self.recipients:
+ print " - " + p
+ print "=" * 40
+ print "Mail Content:"
+ print "=" * 40
+ print self.content
+ print "=" * 40
View
62 flamejam/views.py
@@ -7,12 +7,12 @@
from flask import session, redirect, url_for, escape, request, \
render_template, flash, abort
-from flaskext.mail import Message
from flamejam import app
from flamejam.models import *
from flamejam.forms import *
from flamejam.login import *
+from flamejam.mail import *
@app.route('/')
def index():
@@ -75,14 +75,10 @@ def register():
False, # is verified
receive_emails)
- msg = Message("Welcome to Bacon Game Jam, " + username,
- recipients=[email],
- sender=("bgj","noreply@bacongamejam.org"))
-
- msg.html = render_template("emails/verification.html",
- recipient=new_participant)
- msg.recipients = [new_participant.email]
- mail.send(msg)
+ m = Mail("Welcome to Bacon Game Jam, " + username)
+ m.addRecipients(new_participant)
+ m.render("emails/verification.html", recipient = new_participant)
+ m.send()
db.session.add(new_participant)
db.session.commit()
@@ -104,15 +100,11 @@ def reset_request():
participant = Participant.query.filter_by(username=form.username.data).first()
participant.token = randint(0, sys.maxint)
db.session.commit()
- msg = Message("Welcome to Bacon Game Jam, " + participant.username,
- recipients=[participant.email],
- sender=("bgj","noreply@bacongamejam.org"))
-
- msg.html = render_template("emails/reset_password.html",
- recipient=participant)
- msg.recipients = [participant.email]
- mail.send(msg)
+ m = Mail("Welcome to Bacon Game Jam, " + participant.username)
+ m.addRecipients(participant.email)
+ m.render("emails/reset_password.html", recipient = participant)
+ m.send()
flash('Your password has been reset, check your email')
return render_template('reset_request.html', form=form, error=error)
@@ -152,15 +144,10 @@ def verify_send():
return redirect(url_for('index'))
- msg = Message("Welcome to Bacon Game Jam, " + username,
- recipients=[participant.email],
- sender=("bgj","noreply@bacongamejam.org"))
-
- msg.html = render_template("emails/verification.html",
- recipient=participant)
-
- msg.recipients = [participant.email]
- mail.send(msg)
+ m = Mail("Welcome to Bacon Game Jam, " + username)
+ m.render("emails/verification.html", recipient = participant)
+ m.addRecipients(participant)
+ m.send()
flash('Verification has been resent, check your email')
return redirect(url_for('verify_status', username=username))
@@ -226,14 +213,12 @@ def new_jam():
flash('New jam added.')
# Send out mails to all interested users.
- with mail.connect() as conn:
- participants = Participant.query.filter_by(receive_emails=True).all()
- for participant in participants:
- msg = Message("BaconGameJam: Jam \"%s\" announced" % title)
- msg.html = render_template("emails/jam_announced.html", jam = new_jam, recipient = participant)
- msg.recipients = [participant.email]
- conn.send(msg)
- flash("Email notifications have been sent.")
+ participants = Participant.query.filter_by(receive_emails=True).all()
+ m = Mail("BaconGameJam: Jam \"%s\" announced" % title)
+ m.render("emails/jam_announced.html", jam = new_jam, recipient = participant)
+ m.addRecipients(participants)
+ m.send()
+ flash("Email notifications have been sent.")
#return render_template("emails/jam_announced.html", jam = new_jam, recipient = get_current_user())
return redirect(new_jam.url())
@@ -304,11 +289,10 @@ def edit_jam(jam_slug):
if form.email.data:
with mail.connect() as conn:
participants = Participant.query.filter_by(receive_emails=True).all()
- for participant in participants:
- msg = Message("BaconGameJam: Jam \"%s\" changed" % changes["title"][1])
- msg.html = render_template("emails/jam_changed.html", jam = jam, changes = changes, recipient = participant)
- msg.recipients = [participant.email]
- conn.send(msg)
+ m = Mail("BaconGameJam: Jam \"%s\" changed" % changes["title"][1])
+ m.render("emails/jam_changed.html", jam = jam, changes = changes, recipient = participant)
+ m.addRecipients(participants)
+ m.send()
flash("Email notifications have been sent.")
flash("The jam has been changed.")

2 comments on commit bb8c540

@svenstaro
Owner

Dunno about this one. It already was fairly easy and simple to send mails and you just introduced a whole class do it in 60 more lines. Let's hope it's worth it. Did you test this change?

@opatut
Collaborator

I cannot test the mail server stuff since I have no mail server. Could you test this for me?

Also, this class handles debug mode for mail and will care about notification settings of the users at some point. Like this we don't have duplicate "low level" code everywhere in the views.

Please sign in to comment.
Something went wrong with that request. Please try again.