1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| def FICHIER( chemin ):
"""Guess the content type based on the file's extension. Encoding
will be ignored, altough we should check for simple things like
gzip'd or compressed files."""
ctype, encoding = mimetypes.guess_type(chemin)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compresses), so
# use a generic bag-of-bits type.
ctype = 'application.octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
fp = open(chemin)
# Note : we should handle calculating the charset
msg = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'image':
fp = open(chemin, 'rb')
msg = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'audio':
fp = open(chemin, 'rb')
msg = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
elif maintype != None and subtype != None:
fp = open(chemin, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
Encoders.encode_base64(msg)
else:
fp = open(chemin, 'rb')
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
Encoders.encode_base64(msg)
# Set the filename parameter
fichier = os.path.basename(chemin)
msg.add_header('Content-Disposition','attachment',filename=fichier)
return msg |
Partager