[Zope] need help - python problem

Kam Nejad knejad@codeit.com
Mon, 26 Feb 2001 17:48:04 -0800


I am writing a program that would allow me to send email with
attachments.
The code works, however instead of attachments I get the actual contects

of the attachments. If anybody knows how to solve this problem in python

I would be grateful for your help.



#!/bin/usr/env python

import smtplib
import pdb
import MimeWriter
import quopri, base64
import StringIO
import sys


def shoot(msgFrom, msgTo, msgSubj, msgBody):
   mailSvr = ""
   server = smtplib.SMTP(mailSvr)
   msg1 = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (msgFrom, msgTo,
msgSubj, msgBody)
   retVal = server.sendmail(from_addr=msgFrom, to_addrs=msgTo, msg=msg1)

   server.quit()

   return (retVal)


def main():

   inputFile  = open("readit.dat", "r")
   attachment = open("result.jpg", "rb")
   mimefile = open("output.msg", "w")     #create output file object
needed by quopri.encode()


   mimemsg = MimeWriter.MimeWriter(sys.stdout)
   mimemsg.addheader("Mime-Version", "1.0")
   mimemsg.startmultipartbody("mixed")

   msgpart = mimemsg.nextpart()
   msgpart.addheader("Content-Transfer-Encoding", "quoted-printable")
   msgpart.startbody("text/plain")
   quopri.encode(inputFile, mimefile, 0)

   msgpart = mimemsg.nextpart()
   msgpart.addheader("Content-Transfer-Encoding", "base64")
   msgpart.startbody("image/jpeg")
   quopri.encode(attachment, mimefile, 0)

   mimemsg.lastpart()
   mimefile.close()

   messageFile = open("output.msg", "r")
   messageBody = messageFile.read()

   shoot("knejad@codeit.com", "knejad@codeit.com", "test", messageBody)

   inputFile.close()
   attachment.close()
   messageFile.close()


#pdb.set_trace()
main()
#pdb.set_trace()