"Mikhail Astafiev" <mikle at tomsk.net> wrote in message news:mailman.988393790.7853.python-list at python.org... > [Env: Win2k, Python 1.52] > > Hi! > > I'm not familiar with Python. The code below works OK excluding that > message body is empty. I've tried a lot of variants but I'm always > getting either message without body, or message with body but attached > zip file is corrupted! :(( > May be someone can tell me how to make the code below working, i.e. > generate message with body and proper zip file attached?.. > > Thanks in advance. > > def generate_mime_message( from_address, to_address, subject, > file_to_send, file_name, write_to ): > """Generate multipart MIME message with attached file in base64 encoding > Params: string from_address, to_address, subject > string file_to_send - path to existent ZIP file > string file_name - send zip with specified name > string write_to - store completed message to this file > Return: -""" > > try: > fp=open( write_to, "wc") > except(Exception),err: > print "can't open file for writing '%s':%s. Abnormal function termination"%(write_to,err)) > return > > toplevel=MimeWriter.MimeWriter(fp) > > toplevel.addheader("Date", time.ctime(time.time())) > toplevel.addheader("From", from_address ) > toplevel.addheader("To", to_address ) > toplevel.addheader("Subject" , subject ) > toplevel.addheader("MIME-Version", "1.0") > toplevel.addheader("X-Mailer" , XMAILER ) > toplevel.flushheaders() > > f=toplevel.startmultipartbody("mixed", > mimetools.choose_boundary(), > prefix=0) > > f.write("MESSAGE_BODY") > > # first toplevel body part > mfp = toplevel.nextpart() > > mfp.addheader("Content-Transfer-Encoding", "base64") > mfp.addheader("Content-Disposition", > "attachment; filename=\"%s\""%file_name) > > m = MimeWriter.MimeWriter (mfp.startbody("application/zip")) > > try: > fpin=open( file_to_send , "rb") > base64.encode(fpin, f ) > fpin.close() > except(Exception),err: > print "can't open file '%s' to send:%s. Empty section created!"%(file_to_send,err)) > > toplevel.lastpart() > fp.close() > Here's a tested example which may show you wehre you're going wrong. regards Steve import MimeWriter, base64 # # Writes a multipart mail message: text plus associated graphic # mfile = "multimsg.eml" f = open(mfile, "w") # Create a MimeWriter mail = MimeWriter.MimeWriter(f) mail.addheader("From", "Steve Holden <sholden at holdenweb.com") mail.addheader("To", """Gentle Reader <bookuser at holdenweb.com>, Steve Holden <sholden at holdenweb.com>""") mail.addheader("Subject", "The Python You Wanted") mail.addheader("Received", """from thinker [64.134.121.94] by mail.holdenweb.com (SMTPD32-6.04) id A244C78500BA; Fri, 09 Mar 2001 07:33:38 -0500""") # Mail will be multi-part: First part explains format part1 = mail.startmultipartbody ("mixed") part1.write("This is a MIME-encoded message, with attachments. " "If you are seeing this message your mail program probably cannot " "show you the attachments. Please try another program, or read 'Web " "Programming in Python' to see the attached picture." """ Sorry ... Steve Holden """) # Second part is intended to be read part2 = mail.nextpart() f = part2.startbody("text/plain") f.write("Here we have a multipart message. This " "means that the message body must be processed " "as MIME-encoded content where possible [which " "it clearly is in Outlook Express]." """ regards Your Humble Author """) # Third part is a graphic, which we encode in base64 part3 = mail.nextpart() part3.addheader("Content-Transfer-Encoding", "base64") f = part3.startbody("image/gif", [["Name", "python.gif"]]) b64 = base64.encodestring(open("pythonwin.gif", "rb").read()) f.write(b64) # Never forget to call lastpart! mail.lastpart()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4