zipfile as email-attachment
Rudy Schockaert
rudy.schockaert at pandora.be
Fri Dec 13 08:47:24 EST 2002
Hi Jurgen,
below you will find an extract of a python script I wrote once to test a
mailserver. The getAttachment function is generic.
HTH,
Rudy
def getAttachment(path, filename):
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(path, 'rb')
if maintype == 'text':
attach = MIMEText(fp.read(),_subtype=subtype)
elif maintype == 'message':
attach = email.message_from_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.read(),_subtype=subtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.read(),_subtype=subtype)
else:
print maintype, subtype
attach = MIMEBase(maintype, subtype)
attach.set_payload(fp.read())
encode_base64(attach)
fp.close
attach.add_header('Content-Disposition', 'attachment',
filename=filename)
return attach
def sendWithAttachmentsZipped(Subject, folder):
msg = MIMEMultipart()
dir = os.path.join(curDir,folder)
for filename in os.listdir(dir):
path = os.path.join(dir, filename)
if not os.path.isfile(path):
continue
zipfilename = path + '.zip'
zipfile = ZipFile(zipfilename, 'w')
zipfile.write(path)
zipfile.close()
print ' %s' % zipfilename
attach = getAttachment(zipfilename, filename + '.zip')
msg.attach(attach)
os.remove(zipfilename)
send(Subject, msg)
"Jürgen Kareta" <jkareta at vanilia.de> schreef in bericht
news:atahhb$11mbv6$1 at ID-116640.news.dfncis.de...
> Hello,
> i try to send a mail with a zipfile attachment. If i use MIMEText i get a
> Textfile and if i use MIMEImage i get a exeption. Can anybody give me some
> hints, how i can transfer the zipfile
>
>
More information about the Python-list
mailing list