Code: Alles auswählen
import base64,md5,mimetypes,os,time
def create_msg(von,zu,betreff,nachricht,mailtyp="plain",dateinamenliste=[]):
boundary = md5.md5(time.asctime()).hexdigest()
msg= """From: %s
To: %s
Subject: %s
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
This is a MIME encapsulated multipart message -
please use a MIME-compliant e-mail program to open it.
Dies ist eine mehrteilige Nachricht im MIME-Format -
bitte verwenden Sie zum Lesen ein MIME-konformes Mailprogramm.
--%s
Content-Type: text/%s; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
%s
"""%(von,zu,betreff,boundary,boundary,mailtyp,nachricht)
dateinamenliste=filter(os.path.isfile,dateinamenliste)
for dateiname in dateinamenliste:
f=open(dateiname,'rb')
dateiname=os.path.basename(dateiname)
mimetype=mimetypes.guess_type(dateiname,0)[0] or "application/octet-stream"
if mimetype.split('/')[0]=="text":
encoding="7bit"
temp=f.read()
else:
encoding="base64"
temp=base64.encodestring(f.read())
f.close()
msg+="""--%s
Content-Type: %s; name="%s"
Content-Transfer-Encoding: %s
Content-Disposition: attachment; filename="%s"
%s
"""%(boundary,mimetype,dateiname,encoding,dateiname,temp)
msg+="--%s--" % boundary
return msg