Help: MIME Attachments
Kolosov, Victor
vkolosov at unitedmedia.com
Fri Jun 11 10:06:32 EDT 1999
Here is the right sequence(first write header then body):
subwriter = w.nextpart()
subwriter.addheader("Content-Transfer-Encoding", "base64")
subwriter.addheader("Content-Disposition", 'attachment;
filename="DiaryFile.notes"')
subwriter.flushheaders()
f = subwriter.startbody('application/octet-stream;
name="DiaryFile.notes"')
base64.encode(open('./DiaryFile.notes', 'r'), f)
w.lastpart()
I could not quite figure where do you write the encoded file to your message
and I whould not use 'base64' directry but 'encode' function from mimetools.
If you want here is the my function that does it for me:
def encode_me(file):
from StringIO import StringIO
from mimetools import encode
file_to_send=open(file,'rb')
encoded_object = StringIO()
encode(file_to_send,encoded_object,'base64')
file_to_send.close()
file = encoded_object.getvalue()
return file
So if you have this function - you just do:
subwriter = w.nextpart()
subwriter.addheader("Content-Transfer-Encoding", "base64")
subwriter.addheader("Content-Disposition", 'attachment;
filename="DiaryFile.notes"')
subwriter.flushheaders()
f = subwriter.startbody('application/octet-stream;
name="DiaryFile.notes"')
f.write(encode_me('DiaryFile.notes'))
w.lastpart()
--
Hope it helps
> -----Original Message-----
> From: k_mcdermott at my-deja.com [SMTP:k_mcdermott at my-deja.com]
> Sent: Friday, June 11, 1999 5:37 AM
> To: python-list at python.org
> Subject: Help: MIME Attachments
>
> 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"')
> >>>
>
> 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.
More information about the Python-list
mailing list