parent
b29e4f21b1
commit
677750f50b
|
@ -5,6 +5,11 @@ u"""%(scriptname)s: Afficher un mail correctement encodé pour son envoi
|
|||
|
||||
USAGE
|
||||
%(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
|
||||
-f FROM
|
||||
|
@ -19,6 +24,8 @@ OPTIONS
|
|||
BODY
|
||||
Corps du mail"""
|
||||
|
||||
DEFAULT_FROM = 'no-reploy@univ-reunion.fr'
|
||||
|
||||
import i_need_py25 # nécessite module email version 4.0
|
||||
|
||||
import os, sys, mimetypes
|
||||
|
@ -45,7 +52,7 @@ def run_umail():
|
|||
('a:', 'attach=', "Attacher un fichier"),
|
||||
])
|
||||
options, args = get_args(None, options, longoptions)
|
||||
mfrom = 'no-reply@univ-reunion.fr'
|
||||
mfrom = DEFAULT_FROM
|
||||
subject = None
|
||||
mtos = []
|
||||
body = None
|
||||
|
@ -67,52 +74,58 @@ def run_umail():
|
|||
lines = BLines()
|
||||
lines.readlines(sys.stdin)
|
||||
|
||||
msg = MIMEMultipart()
|
||||
#msg.set_charset('utf-8')
|
||||
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)
|
||||
msg['From'] = mfrom
|
||||
msg['To'] = ', '.join(mtos)
|
||||
msg['Subject'] = Header(subject, '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)
|
||||
|
||||
Generator(sys.stdout).flatten(msg)
|
||||
|
||||
|
|
Loading…
Reference in New Issue