diff options
author | Marian Sigler <m@qjym.de> | 2012-09-21 06:14:00 +0200 |
---|---|---|
committer | Marian Sigler <m@qjym.de> | 2012-09-21 06:14:50 +0200 |
commit | db190a8332a6eeb32ba0276fe45aced56bfee328 (patch) | |
tree | 78e856ebfa76b547973b94458fd96bb0f47a7a18 /utils.py | |
parent | ef5bf83da44a7e14b0cbe4733a19b2933e77671c (diff) | |
download | web-db190a8332a6eeb32ba0276fe45aced56bfee328.tar.gz web-db190a8332a6eeb32ba0276fe45aced56bfee328.tar.bz2 web-db190a8332a6eeb32ba0276fe45aced56bfee328.zip |
add send_mail function
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 |