Help: MIME Attachments
M.-A. Lemburg
mal at lemburg.com
Fri Jun 11 09:07:47 EDT 1999
k_mcdermott at my-deja.com wrote:
>
> Hi,
>
> 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
> headers...?
>
> TIA
>
> Kevin
>
> 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")
> w.flushheaders()
> w.startmultipartbody("mixed")
> subwriter = w.nextpart()
> f = subwriter.startbody('application/octet-stream;
> name="DiaryFile.notes"')
>
> >>> This is the puzzling bit...
>
> subwriter.addheader("Content-Transfer-Encoding", "base64")
> subwriter.addheader("Content-Disposition", 'attachment;
> filename="DiaryFile.notes"')
> >>>
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.
> subwriter.flushheaders()
> base64.encode(open('./DiaryFile.notes', 'r'), f)
> w.lastpart()
> # s = SMTP("localhost")
> # s.sendmail("diary at system.com", ["my at email.address.com"],
> outputfp.getvalue())
> # 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.
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 203 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
More information about the Python-list
mailing list