[mail] Début de templating, internationalisation, html et plain text

le corps du message est régigé en markdown <http://daringfireball.net/projects/markdown/syntax>
que l'on utilise ensuite pour générer à la fois le plain text du
message et la partie html. On a donc besoin du module python markdown
packagé dans python-markdown.
Les headers sont en plain text.
On utilise jinja2 comme moteur de template (le même que celui de django)

Le mail de bienvenue est là en exemple.
This commit is contained in:
Valentin Samir 2013-08-27 20:41:38 +02:00
parent cbbf060fb2
commit bde23e7ca2
8 changed files with 199 additions and 0 deletions

90
gestion/mail/mail.py Normal file
View file

@ -0,0 +1,90 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import jinja2
import sys
from email.header import Header
from email.message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from markdown import markdown
sys.path.append('/usr/scripts/')
from gestion.email_tools import format_sender
sys.path.pop()
default_language = 'fr'
template_path = '/usr/scripts/gestion/mail/template/'
html_template = template_path + 'html'
html_mutilang_template = template_path + 'html_multilang'
text_mutilang_template = template_path + 'text_multilang'
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
templateEnv = jinja2.Environment( loader=templateLoader )
### For an example:
### print generate('bienvenue', {'From':'respbats@crans.org', 'To':'admin@genua.fr', 'lang_info':'English version below'}).as_string()
def submessage(playload, type, charset='utf-8'):
"""Renvois un sous message à mettre dans un message multipart"""
submsg=MIMEText('', type, charset)
del(submsg['Content-Transfer-Encoding'])
submsg['Content-Transfer-Encoding']='8bit'
submsg.set_payload(playload)
return submsg
def htmlmultilang(mail, lang1, lang2, params, lang_info='', charset='utf-8'):
"""Génère un html bilingue"""
file1 = template_path + mail + '/body/' + lang1
file2 = template_path + mail + '/body/' + lang2
if lang1 == lang2 or not os.path.isfile(file2):
txt = templateEnv.get_template(file1).render(params)
html = templateEnv.get_template(html_template).render({'body': markdown(txt)})
return submessage(html.encode(charset), 'html', charset)
else:
txt1 = templateEnv.get_template(file1).render(params)
txt2 = templateEnv.get_template(file2).render(params)
params.update({'lang1':lang1, 'lang2':lang2, 'body1': markdown(txt1), 'body2':markdown(txt2)})
html = templateEnv.get_template(html_mutilang_template).render(params)
return submessage(html.encode(charset), 'html', charset)
def textmultilang(mail, lang1, lang2, params, charset='utf-8'):
"""Génère un plain text bilingue"""
file1 = template_path + mail + '/body/' + lang1
file2 = template_path + mail + '/body/' + lang2
if lang1 == lang2 or not os.path.isfile(file2):
txt = templateEnv.get_template(file1).render(params)
return submessage(txt.encode(charset), 'plain', charset)
else:
txt1 = templateEnv.get_template(file1).render(params)
txt2 = templateEnv.get_template(file2).render(params)
params.update({'body1': txt1, 'body2':txt2})
txt = templateEnv.get_template(text_mutilang_template).render(params)
return submessage(txt.encode(charset), 'plain', charset)
def generate(mail, params, lang=default_language, lang_fallback=default_language, lang_alt='en', charset='utf-8'):
"""Génère un message multipart"""
msg = MIMEMultipart('alternative')
if os.path.isdir(template_path + mail):
for filename in [dir for dir in os.listdir(template_path + mail) if os.path.isdir(template_path + mail + '/' + dir)]:
if os.path.isfile(template_path + mail + '/' + filename + '/' + lang):
lang_tmp = lang
file = template_path + mail + '/' + filename + '/' + lang
else:
lang_tmp = lang_fallback
file = template_path + mail + '/' + filename + '/' + lang_tmp
if filename == 'body':
msg.attach(textmultilang(mail, lang_tmp, lang_alt, params, charset))
msg.attach(htmlmultilang(mail, lang_tmp, lang_alt, params, charset))
else:
txt = templateEnv.get_template(file).render(params)
if filename in ['From', 'To', 'Cc', 'Bcc']:
msg[filename] = format_sender(txt, charset)
else:
msg[filename]=Header(txt.encode(charset), charset)
return msg