diff --git a/app/__init__.py b/app/__init__.py index 4e5b345..f5a8a91 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,6 +3,7 @@ from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.login import LoginManager from flask.ext.openid import OpenID +from flask.ext.mail import Mail from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD app = Flask(__name__) @@ -12,6 +13,7 @@ lm = LoginManager() lm.setup_app(app) lm.login_view = 'login' oid = OpenID(app, os.path.join(basedir, 'tmp')) +mail = Mail(app) if not app.debug: import logging diff --git a/app/decorators.py b/app/decorators.py new file mode 100644 index 0000000..65c1411 --- /dev/null +++ b/app/decorators.py @@ -0,0 +1,8 @@ +from threading import Thread + +def async(f): + def wrapper(*args, **kwargs): + thr = Thread(target = f, args = args, kwargs = kwargs) + thr.start() + return wrapper + diff --git a/app/emails.py b/app/emails.py new file mode 100644 index 0000000..e511119 --- /dev/null +++ b/app/emails.py @@ -0,0 +1,28 @@ +from flask import render_template +from flask.ext.mail import Message +from app import mail +from decorators import async +from config import ADMINS + +@async +def send_async_email(msg): + mail.send(msg) + +def send_email(subject, sender, recipients, text_body, html_body): + msg = Message(subject, sender = sender, recipients = recipients) + msg.body = text_body + msg.html = html_body + send_async_email(msg) + #thr = threading.Thread(target = send_async_email, args = [msg]) + #thr.start() + + +def follower_notification(followed, follower): + send_email("[microblog] %s is now following you!" % follower.nickname, + ADMINS[0], + [followed.email], + render_template("follower_email.txt", + user = followed, follower = follower), + render_template("follower_email.html", + user = followed, follower = follower)) + \ No newline at end of file diff --git a/app/templates/follower_email.html b/app/templates/follower_email.html new file mode 100644 index 0000000..b00e00d --- /dev/null +++ b/app/templates/follower_email.html @@ -0,0 +1,13 @@ +
Dear {{user.nickname}},
+{{follower.nickname}} is now a follower.
++ |
+ {{follower.nickname}} + {{follower.about_me}} + |
+
Regards,
+The microblog
admin