Help: MIME Attachments

k_mcdermott at k_mcdermott at
Fri Jun 11 11:37:05 CEST 1999


I am trying to send a plain text file, as an attachment through e-mail.

This is a hacked version of my test code below, basically the
program interprets a dump from a diary system, and then writes
Lotus Notes importable Calendar entries.  I want to then mail
them to users with an accompanying instruction header to allow them
to import them into their calendars...

The only problem is the mailing them to users bit, I either get
the plain text in the file (which I don't want, I just want them
to detach the text file and then import) or I get the Base64 encoded
text, either way there is no attachment...

My Problem is that the MIME headers indicating Base64 encoding
appears a line after the content type, and I believe that this blank
line terminates the headers...

So my question is, at what point should I be introducing these



ps Python has brought a project that was originally quoted at "tens of
thousands of pounds and hundreds of man-hours" down to around 4 hours
so far, and if I can crack this, ZERO outlay :-)

import base64
import sys
import StringIO
import MimeWriter
from smtplib import SMTP

# Mails the Diary Import file to the user in MIME format
# Based upon code by GVR
def mailFileToUser(_userName, _fileName):
	outputfp = StringIO.StringIO()
	w = MimeWriter.MimeWriter(outputfp)
	w.addheader("subject", "Diary Entries from Mainframe")
	subwriter = w.nextpart()
	f = subwriter.startbody('application/octet-stream;

>>> This is the puzzling bit...

	subwriter.addheader("Content-Transfer-Encoding", "base64")
	subwriter.addheader("Content-Disposition", 'attachment;

	base64.encode(open('./DiaryFile.notes', 'r'), f)
	# s = SMTP("localhost")
	# s.sendmail("diary at", ["my at"],
	# s.close()
	print outputfp.getvalue()
if __name__=='__main__':
	mailFileToUser('Kevin McDermott', 'TestFile.out')

Sent via
Share what you know. Learn what you don't.

More information about the Python-list mailing list