2013-11-14 10:05:10 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
|
|
|
|
u"""%(scriptname)s: Afficher un mail correctement encodé pour son envoi
|
|
|
|
|
|
|
|
USAGE
|
2013-11-15 16:47:27 +04:00
|
|
|
%(scriptname)s [options] <SUBJECT> RECIPIENTS... <<<"BODY" | /usr/sbin/sendmail -i
|
2013-11-16 17:31:42 +04:00
|
|
|
%(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.
|
2013-11-15 16:47:27 +04:00
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
-f FROM
|
|
|
|
Spécifier l'adresse de l'expéditeur. Par défaut, utiliser no-reply@univ-reunion.fr
|
|
|
|
-a FILE
|
2013-11-15 16:49:49 +04:00
|
|
|
Attacher un fichier au mail. Cette option peut être spécifiée autant de
|
|
|
|
fois que nécessaire pour attacher plusieurs fichiers.
|
2013-11-15 16:47:27 +04:00
|
|
|
SUBJECT
|
|
|
|
Sujet du mail. Cette valeur est requise.
|
|
|
|
RECIPIENTS
|
|
|
|
Destinataires du mail. Il faut au moins un destinataire.
|
|
|
|
BODY
|
|
|
|
Corps du mail"""
|
2013-11-14 10:05:10 +04:00
|
|
|
|
2013-11-16 17:31:42 +04:00
|
|
|
DEFAULT_FROM = 'no-reploy@univ-reunion.fr'
|
|
|
|
|
2013-11-15 16:35:04 +04:00
|
|
|
import i_need_py25 # nécessite module email version 4.0
|
|
|
|
|
|
|
|
import os, sys, mimetypes
|
2013-11-14 10:05:10 +04:00
|
|
|
from os import path
|
|
|
|
|
2013-11-15 16:35:04 +04:00
|
|
|
from email import encoders
|
2013-11-14 10:05:10 +04:00
|
|
|
from email.header import Header
|
2013-11-15 16:35:04 +04:00
|
|
|
from email.mime.base import MIMEBase
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.mime.image import MIMEImage
|
|
|
|
from email.mime.audio import MIMEAudio
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.generator import Generator
|
2013-11-14 10:05:10 +04:00
|
|
|
|
|
|
|
from ulib.all import *
|
|
|
|
|
|
|
|
def display_help():
|
|
|
|
uprint(__doc__ % globals())
|
|
|
|
|
2013-11-15 16:49:49 +04:00
|
|
|
def run_umail():
|
2013-11-14 10:05:10 +04:00
|
|
|
options, longoptions = build_options([
|
|
|
|
('h', 'help', "Afficher l'aide"),
|
|
|
|
('f:', 'from=', "Spécifier l'expéditeur"),
|
2013-11-15 16:35:04 +04:00
|
|
|
('a:', 'attach=', "Attacher un fichier"),
|
2013-11-14 10:05:10 +04:00
|
|
|
])
|
|
|
|
options, args = get_args(None, options, longoptions)
|
2013-11-16 17:31:42 +04:00
|
|
|
mfrom = DEFAULT_FROM
|
2013-11-14 10:05:10 +04:00
|
|
|
subject = None
|
|
|
|
mtos = []
|
|
|
|
body = None
|
2013-11-15 16:35:04 +04:00
|
|
|
afiles = []
|
2013-11-14 10:05:10 +04:00
|
|
|
for option, value in options:
|
|
|
|
if option in ('-h', '--help'):
|
|
|
|
display_help()
|
|
|
|
sys.exit(0)
|
|
|
|
elif option in ('-f', '--from'):
|
|
|
|
mfrom = value
|
2013-11-15 16:35:04 +04:00
|
|
|
elif option in ('-a', '--attach'):
|
|
|
|
afiles.append(value)
|
2013-11-14 10:05:10 +04:00
|
|
|
|
|
|
|
if not args[0:1]: die("Vous devez spécifier le sujet")
|
|
|
|
subject = args[0]
|
|
|
|
if not args[1:]: die("Vous devez spécifier les destinataires")
|
|
|
|
mtos = args[1:]
|
|
|
|
|
|
|
|
lines = BLines()
|
|
|
|
lines.readlines(sys.stdin)
|
|
|
|
|
2013-11-16 17:31:42 +04:00
|
|
|
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.set_charset('utf-8')
|
|
|
|
if lines:
|
|
|
|
body = MIMEText('\n'.join(lines), 'plain')
|
|
|
|
body.set_charset('utf-8')
|
|
|
|
msg.attach(body)
|
|
|
|
for afile in afiles:
|
|
|
|
if not path.isfile(afile): die("%s: Fichier introuvable" % afile)
|
|
|
|
mimetype, encoding = mimetypes.guess_type(afile)
|
|
|
|
if mimetype is None or encoding is not None:
|
|
|
|
mimetype = 'application/octet-stream'
|
|
|
|
maintype, subtype = mimetype.split('/', 1)
|
|
|
|
if maintype == 'text':
|
|
|
|
inf = open(afile, 'rb')
|
|
|
|
try:
|
|
|
|
#XXX on considère que tous les fichiers texte sont encodés en
|
|
|
|
# utf-8. Ce n'est pas forcément vrai, mais cette approximation
|
|
|
|
# devrait être suffisante pour le moment
|
|
|
|
part = MIMEText(inf.read(), _subtype=subtype, _charset='utf-8')
|
|
|
|
finally:
|
|
|
|
inf.close()
|
|
|
|
elif maintype == 'image':
|
|
|
|
inf = open(afile, 'rb')
|
|
|
|
try:
|
|
|
|
part = MIMEImage(inf.read(), _subtype=subtype)
|
|
|
|
finally:
|
|
|
|
inf.close()
|
|
|
|
elif maintype == 'audio':
|
|
|
|
inf = open(afile, 'rb')
|
|
|
|
try:
|
|
|
|
part = MIMEAudio(inf.read(), _subtype=subtype)
|
|
|
|
finally:
|
|
|
|
inf.close()
|
|
|
|
else:
|
|
|
|
part = MIMEBase(maintype, subtype)
|
|
|
|
inf = open(afile, 'rb')
|
|
|
|
try:
|
|
|
|
part.set_payload(inf.read())
|
|
|
|
finally:
|
|
|
|
inf.close()
|
|
|
|
encoders.encode_base64(part)
|
|
|
|
part.add_header('Content-Disposition', 'attachment', filename=path.basename(afile))
|
|
|
|
msg.attach(part)
|
2013-11-14 10:05:10 +04:00
|
|
|
msg['From'] = mfrom
|
|
|
|
msg['To'] = ', '.join(mtos)
|
|
|
|
msg['Subject'] = Header(subject, 'utf-8')
|
2013-11-15 16:35:04 +04:00
|
|
|
|
|
|
|
Generator(sys.stdout).flatten(msg)
|
2013-11-14 10:05:10 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-11-15 16:49:49 +04:00
|
|
|
run_umail()
|