Merge branch 'master' of vcs.univ.run:modules/nutools

This commit is contained in:
Jephté Clain 2013-11-19 12:24:49 +04:00
commit cafe570fee
1 changed files with 57 additions and 44 deletions

View File

@ -5,6 +5,11 @@ u"""%(scriptname)s: Afficher un mail correctement encodé pour son envoi
USAGE USAGE
%(scriptname)s [options] <SUBJECT> RECIPIENTS... <<<"BODY" | /usr/sbin/sendmail -i %(scriptname)s [options] <SUBJECT> RECIPIENTS... <<<"BODY" | /usr/sbin/sendmail -i
%(scriptname)s [options] <SUBJECT> RECIPIENTS... <<<"BODY" | /usr/sbin/sendmail RECIPIENTS...
La deuxième forme est pour les implémentations compatible de sendmail qui
ignorent l'argument -i. Il faut alors spécifier tous les destinataires en
argument.
OPTIONS OPTIONS
-f FROM -f FROM
@ -19,6 +24,8 @@ OPTIONS
BODY BODY
Corps du mail""" Corps du mail"""
DEFAULT_FROM = 'no-reploy@univ-reunion.fr'
import i_need_py25 # nécessite module email version 4.0 import i_need_py25 # nécessite module email version 4.0
import os, sys, mimetypes import os, sys, mimetypes
@ -45,7 +52,7 @@ def run_umail():
('a:', 'attach=', "Attacher un fichier"), ('a:', 'attach=', "Attacher un fichier"),
]) ])
options, args = get_args(None, options, longoptions) options, args = get_args(None, options, longoptions)
mfrom = 'no-reply@univ-reunion.fr' mfrom = DEFAULT_FROM
subject = None subject = None
mtos = [] mtos = []
body = None body = None
@ -67,11 +74,14 @@ def run_umail():
lines = BLines() lines = BLines()
lines.readlines(sys.stdin) lines.readlines(sys.stdin)
if not afiles:
# Sans attachement, faire un message simple
msg = MIMEText('\n'.join(lines), 'plain')
msg.set_charset('utf-8')
else:
# Il y a des attachement, faire un multipart
msg = MIMEMultipart() msg = MIMEMultipart()
#msg.set_charset('utf-8') #msg.set_charset('utf-8')
msg['From'] = mfrom
msg['To'] = ', '.join(mtos)
msg['Subject'] = Header(subject, 'utf-8')
if lines: if lines:
body = MIMEText('\n'.join(lines), 'plain') body = MIMEText('\n'.join(lines), 'plain')
body.set_charset('utf-8') body.set_charset('utf-8')
@ -113,6 +123,9 @@ def run_umail():
encoders.encode_base64(part) encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=path.basename(afile)) part.add_header('Content-Disposition', 'attachment', filename=path.basename(afile))
msg.attach(part) msg.attach(part)
msg['From'] = mfrom
msg['To'] = ', '.join(mtos)
msg['Subject'] = Header(subject, 'utf-8')
Generator(sys.stdout).flatten(msg) Generator(sys.stdout).flatten(msg)