[Zope-CVS] CVS: Packages/TestScripts - mailer.py:1.1

Chris Withers chrisw@nipltd.com
Thu, 31 Jan 2002 05:06:36 -0500


Update of /cvs-repository/Packages/TestScripts
In directory cvs.zope.org:/tmp/cvs-serv6309

Added Files:
	mailer.py 
Log Message:
mailer module used by autotester.py

=== Added File Packages/TestScripts/mailer.py ===
# Copyright (c) 2001 New Information Paradigms Ltd
#
# This Software is realease under the MIT License:
# http://www.opensource.org/licenses/mit-license.html
# See license.txt for more details.
#
# $Id: mailer.py,v 1.1 2002/01/31 10:06:35 chrisw Exp $

from smtplib import SMTP
from rfc822 import Message
from StringIO import StringIO
from string import join

template = """From: %(from)s
To: %(to)s
Subject: %(subject)s

%(body)s
"""

# mail a message
def send(address,subject='',body='',template=template,smtp_server='localhost'):
    message = template % {'from':address,
                          'to':address,
                          'subject':subject,
                          'body':body}
    mfile=StringIO(message.strip())
    mo=Message(mfile)

    to_a=[]
    for header in (mo.getaddrlist('to'),
                   mo.getaddrlist('cc'),
                   mo.getaddrlist('bcc')):
        if not header: continue
        for name, addr in header:
            to_a.append(addr)

    from_a=mo.getaddr('from')[1]

    server = SMTP(smtp_server)
    server.sendmail(from_a,to_a,message)