From db190a8332a6eeb32ba0276fe45aced56bfee328 Mon Sep 17 00:00:00 2001 From: Marian Sigler Date: Fri, 21 Sep 2012 06:14:00 +0200 Subject: add send_mail function --- default_settings.py | 7 ++++++- utils.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/default_settings.py b/default_settings.py index 61b0dee..6fc80c3 100644 --- a/default_settings.py +++ b/default_settings.py @@ -1,4 +1,9 @@ -SECRET_KEY = 'remember to change this to something more random and private' +SECRET_KEY = 'remember to change this to something more random and secret' # CHANGE THIS! (e.g. os.urandom(32) ) PASSWORD_ENCRYPTION_KEY = '.\x14\xa7\x1b\xa2:\x1b\xb7\xbck\x1bD w\xab\x87a\xb4\xb7\xca\xf1\x06\xb0\x9f?q\x13\x05\x8dY\xe5<' + +MAIL_DEFAULT_SENDER = 'help-me-help-me-please@spline.de' +MAIL_CONFIRM_SENDER = 'spline accounts ' + +SENDMAIL_COMMAND = '/usr/sbin/sendmail' diff --git a/utils.py b/utils.py index bdb7f18..889a65f 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,7 @@ import ldap import pickle import re from Crypto.Cipher import AES +from email.mime.text import MIMEText from functools import wraps from flask import flash, g, redirect, render_template, request, session, url_for from hashlib import sha1 @@ -117,6 +118,24 @@ def verify_confirmation(realm, token): return pickle.loads(payload) +def send_mail(recipient, subject, body, sender=None): + if sender is None: + sender = current_app.config['MAIL_DEFAULT_SENDER'] + + safe = lambda s: s.split('\n', 1)[0] + + msg = MIMEText(body, _charset='utf-8') + msg['Subject'] = safe(subject) + msg['To'] = safe(recipient) + msg['From'] = safe(sender) + + p = subprocess.Popen([current_app.config['SENDMAIL_COMMAND'], '-t'], + stdin=subprocess.PIPE) + p.stdin.write(msg.as_string()) + p.stdin.close() + + if p.wait() != 0: + raise RuntimeError('sendmail terminated with %d' % p.returncode) # circular import from app import app -- cgit v1.2.3-1-g7c22