diff options
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -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 |