Help: MIME Attachments
mal at lemburg.com
Fri Jun 11 09:07:47 EDT 1999
k_mcdermott at my-deja.com wrote:
> 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;
Reading the source in MimeWrite.py I'd suggest moving these lines
up just behind the subwrite = ... line. The .startbody()
call flushes the headers for you and also terminates the headers
(by inserting the blank line you already noticed).
Should work then, I guess.
> base64.encode(open('./DiaryFile.notes', 'r'), f)
> # s = SMTP("localhost")
> # s.sendmail("diary at system.com", ["my at email.address.com"],
> # s.close()
> print outputfp.getvalue()
> if __name__=='__main__':
> mailFileToUser('Kevin McDermott', 'TestFile.out')
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
Y2000: 203 days left
Python Pages: http://www.lemburg.com/python/
More information about the Python-list