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 --- utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'utils.py') 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