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.
"""
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):