Python / CGI / E-mail problem

Sreekant Kodela sreekant at uklinux.net
Wed May 17 13:25:18 EDT 2000


Oleg Broytmann wrote:

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

Hi there

Hope I'm not being a nuisance on this ng. I tried your way. At the end
smtplib.SMTP('localhost').sendmail(to_address,from_address,???)

What should I use in place of ???. I actually used a StringIO object to create the
mime message. Should I use the os.popen() eventhough the ensuing code will end up on
someone elses server!

Thanks for the answers and patience
Sreekant




More information about the Python-list mailing list