Python / CGI / E-mail problem

Oleg Broytmann phd at phd.russ.ru
Wed May 17 04:14:26 EDT 2000


On Tue, 16 May 2000, Sreekant Kodela wrote:
> I tried using the MimeWriter but failed. Do you have any simple exampe by
> any chance.

#! /usr/local/bin/python -O


import sys, os, string
from mimetools import choose_boundary
from MimeWriter import MimeWriter
import base64

try:
   from cStringIO import StringIO
except ImportError:
   from StringIO import StringIO
   

def gen_mime(zip_file):
   email_from = "Oleg Broytmann <phd at phd.russ.ru>"
   email_to = "phd at sun.med.ru"

   #sendmail = open("sendmail", 'w')
   sendmail = os.popen("/usr/sbin/sendmail '%s'" % email_to, 'w')
   sendmail.write("""From: %s
To: %s
Subject: %s
Precedence: bulk
MIME-Version: 1.0
""" % (email_from, email_to, zip_file))

   mime = MimeWriter(sendmail)

   firstpart = mime.startmultipartbody("related")
   firstpart.write("""\
   Your mailer does not support MIME encoding. Please upgarde to MIME-enabled
mailer (almost every modern mailer is MIME-capable).
""")

   subwriter = mime.nextpart()
   subwriter.addheader("Content-Transfer-Encoding", "base64")
   subwriter.addheader("Content-Disposition", "inline; filename=\"%s\"" % zip_file)
   subwriter.startbody("application/zip")

   zip_file = open(zip_file, 'r')
   base64.encode(StringIO(zip_file.read()), sendmail)
   zip_file.close()

   mime.lastpart()
   sendmail.close()


def run():
   zip_file = sys.argv[1]
   gen_mime(zip_file)


if __name__ == '__main__':
   run()

Oleg.            (All opinions are mine and not of my employer)
---- 
    Oleg Broytmann      Foundation for Effective Policies      phd at phd.russ.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list