Mailing List Archive

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@system.com", ["my@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.
Help: MIME Attachments [ In reply to ]
k_mcdermott@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@system.com", ["my@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/
Help: MIME Attachments [ In reply to ]
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@my-deja.com [SMTP:k_mcdermott@my-deja.com]
> Sent: Friday, June 11, 1999 5:37 AM
> To: python-list@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@system.com", ["my@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.
Help: MIME Attachments [ In reply to ]
Thanks Marc-Andre,

> 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.
>

It works great (good guess), Guido had posted a message almost a year
ago (which the code I wrote is a modification of) but I couldn't quite
figure it out...

If anybody wants the code, probably the only interesting bits are the
generation of calendar entries for Notes from a template file (using a
subclassed HTMLgen.TemplateDocument) and the bit for appending an
instruction file as e-mail body with a base64 encoded attachment let me
know and I'll post it (it's around 166 lines including extensive
documentation).

Thanks again

Kevin


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.