diff --git a/gestion/email_tools.py b/gestion/email_tools.py index 2a3f302f..57422878 100644 --- a/gestion/email_tools.py +++ b/gestion/email_tools.py @@ -10,27 +10,13 @@ Autres outils relatifs aux mails. format_sender et send_email adaptés depuis /usr/scripts/impression/crans_backend.py. """ + import re +import gestion.mail as mail_module +# TODO deprecated -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)) +format_sender = mail_module.format_sender 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 that can represent all the characters occurring in the email. - Argument server maybe a string, indicating the name of the SMTP server, or - directly an instance of smtplib.SMTP. + Argument server is ignored. The resulting mail will be sent to debug if not False. Otherwise, it will be sent to recipient and cc. """ - from smtplib import SMTP from email.MIMEText import MIMEText from email.Header import Header @@ -90,13 +74,8 @@ def send_email(sender, recipient, subject, body, server='localhost', cc=None, de actual_sender = sender # Send the message - if isinstance(server, SMTP): - server.sendmail(actual_sender, actual_recipient, msg.as_string()) - else: - smtp = SMTP() - smtp.connect(server) + with mail_module.ServerConnection() as smtp: smtp.sendmail(actual_sender, actual_recipient, msg.as_string()) - smtp.quit() def parse_mail_template(fichier): diff --git a/gestion/mail/mail.py b/gestion/mail/mail.py index bfded41a..3e73a4af 100644 --- a/gestion/mail/mail.py +++ b/gestion/mail/mail.py @@ -18,7 +18,6 @@ from locale_util import setlocale if '/usr/scripts' not in sys.path: sys.path.append('/usr/scripts') -from gestion.email_tools import format_sender from gestion import secrets_new as secrets default_language = 'fr' @@ -157,6 +156,26 @@ def validation_url(view_name, data='', debug=False): 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): """Connexion au serveur smtp""" _conn = None