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

\ No newline at end of file diff --git a/app/templates/follower_email.txt b/app/templates/follower_email.txt new file mode 100644 index 0000000..f4c49a2 --- /dev/null +++ b/app/templates/follower_email.txt @@ -0,0 +1,9 @@ +Dear {{user.nickname}}, + +{{follower.nickname}} is now a follower. Click on the following link to visit {{follower.nickname}}'s profile page: + +{{url_for("user", nickname = follower.nickname, _external = True)}} + +Regards, + +The microblog admin \ No newline at end of file diff --git a/app/views.py b/app/views.py index e47363b..71ebf7b 100644 --- a/app/views.py +++ b/app/views.py @@ -4,6 +4,7 @@ from app import app, db, lm, oid from forms import LoginForm, EditForm, PostForm, SearchForm from models import User, ROLE_USER, ROLE_ADMIN, Post from datetime import datetime +from emails import follower_notification from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS @lm.user_loader @@ -135,6 +136,7 @@ def follow(nickname): db.session.add(u) db.session.commit() flash('You are now following ' + nickname + '!') + follower_notification(user, g.user) return redirect(url_for('user', nickname = nickname)) @app.route('/unfollow/') diff --git a/config.py b/config.py index 9adb163..ad29d77 100644 --- a/config.py +++ b/config.py @@ -15,11 +15,13 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db') SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') WHOOSH_BASE = os.path.join(basedir, 'search.db') -# mail server settings -MAIL_SERVER = 'localhost' +# email server +MAIL_SERVER = 'your.mailserver.com' MAIL_PORT = 25 -MAIL_USERNAME = None -MAIL_PASSWORD = None +MAIL_USE_TLS = False +MAIL_USE_SSL = False +MAIL_USERNAME = 'you' +MAIL_PASSWORD = 'your-password' # administrator list ADMINS = ['you@example.com']