email_tools: passe par mail/mail.py

Pour le debug, et pour passer par un seul endroit.
This commit is contained in:
Daniel STAN 2014-11-07 00:25:58 +01:00
parent 7169e50fd2
commit 113684d0f8
2 changed files with 26 additions and 28 deletions

View file

@ -10,27 +10,13 @@ Autres outils relatifs aux mails.
format_sender et send_email adaptés depuis /usr/scripts/impression/crans_backend.py. format_sender et send_email adaptés depuis /usr/scripts/impression/crans_backend.py.
""" """
import re import re
import gestion.mail as mail_module
# TODO deprecated
def format_sender(sender, header_charset='utf-8'): format_sender = mail_module.format_sender
"""
Check and format sender for header.
"""
from email.Header import Header
from email.Utils import parseaddr, formataddr
# Split real name (which is optional) and email address parts
sender_name, sender_addr = parseaddr(sender)
# We must always pass Unicode strings to Header, otherwise it will
# use RFC 2047 encoding even on plain ASCII strings.
sender_name = str(Header(unicode(sender_name), header_charset))
# Make sure email addresses do not contain non-ASCII characters
sender_addr = sender_addr.encode('ascii')
return formataddr((sender_name, sender_addr))
def send_email(sender, recipient, subject, body, server='localhost', cc=None, debug=False, actual_sender=None): def send_email(sender, recipient, subject, body, server='localhost', cc=None, debug=False, actual_sender=None):
@ -44,13 +30,11 @@ def send_email(sender, recipient, subject, body, server='localhost', cc=None, de
The charset of the email will be the first one out of US-ASCII or UTF-8 The charset of the email will be the first one out of US-ASCII or UTF-8
that can represent all the characters occurring in the email. that can represent all the characters occurring in the email.
Argument server maybe a string, indicating the name of the SMTP server, or Argument server is ignored.
directly an instance of smtplib.SMTP.
The resulting mail will be sent to debug if not False. The resulting mail will be sent to debug if not False.
Otherwise, it will be sent to recipient and cc. Otherwise, it will be sent to recipient and cc.
""" """
from smtplib import SMTP
from email.MIMEText import MIMEText from email.MIMEText import MIMEText
from email.Header import Header from email.Header import Header
@ -90,13 +74,8 @@ def send_email(sender, recipient, subject, body, server='localhost', cc=None, de
actual_sender = sender actual_sender = sender
# Send the message # Send the message
if isinstance(server, SMTP): with mail_module.ServerConnection() as smtp:
server.sendmail(actual_sender, actual_recipient, msg.as_string())
else:
smtp = SMTP()
smtp.connect(server)
smtp.sendmail(actual_sender, actual_recipient, msg.as_string()) smtp.sendmail(actual_sender, actual_recipient, msg.as_string())
smtp.quit()
def parse_mail_template(fichier): def parse_mail_template(fichier):

View file

@ -18,7 +18,6 @@ from locale_util import setlocale
if '/usr/scripts' not in sys.path: if '/usr/scripts' not in sys.path:
sys.path.append('/usr/scripts') sys.path.append('/usr/scripts')
from gestion.email_tools import format_sender
from gestion import secrets_new as secrets from gestion import secrets_new as secrets
default_language = 'fr' default_language = 'fr'
@ -157,6 +156,26 @@ def validation_url(view_name, data='', debug=False):
return ROOT + req.text.encode('utf-8') return ROOT + req.text.encode('utf-8')
def format_sender(sender, header_charset='utf-8'):
"""
Check and format sender for header.
"""
from email.Header import Header
from email.Utils import parseaddr, formataddr
# Split real name (which is optional) and email address parts
sender_name, sender_addr = parseaddr(sender)
# We must always pass Unicode strings to Header, otherwise it will
# use RFC 2047 encoding even on plain ASCII strings.
sender_name = str(Header(unicode(sender_name), header_charset))
# Make sure email addresses do not contain non-ASCII characters
sender_addr = sender_addr.encode('ascii')
return formataddr((sender_name, sender_addr))
class ServerConnection(object): class ServerConnection(object):
"""Connexion au serveur smtp""" """Connexion au serveur smtp"""
_conn = None _conn = None