#Copyright ReportLab Europe Ltd. 2000-2004
#history www.reportlab.co.uk/rl-cgi/viewcvs.cgi/rlextra/utils/sendmail.py
"Utilities for sending mail"

def sendMail(smtpServer, sender, receiver, subject, text, fromaddr=None):
    "Sends a simple mail"
    import smtplib
    if fromaddr is None: fromaddr=sender
    msg = "To: %s\nFrom: %s\nSubject: %s\n\n%s" % (receiver,fromaddr,subject, text)
    server = smtplib.SMTP(smtpServer)
    server.sendmail(sender, receiver, msg)
    server.quit()

def sendHtmlMail(smtpServer, sender, receiver, subject, html):
    "Sends an html email (html argument is an HTML string)"
    import MimeWriter
    import mimetools
    import cStringIO
    import smtplib

    # Produce an approximate textual rendering of the HTML string,
    # unless you have been given a better version as an argument
    import htmllib, formatter
    textout = cStringIO.StringIO()
    formtext = formatter.AbstractFormatter(formatter.DumbWriter(textout))
    parser = htmllib.HTMLParser(formtext)
    parser.feed(html)
    parser.close()
    text = textout.getvalue()
    del textout, formtext, parser

    out = cStringIO.StringIO() # output buffer for our message
    htmlin = cStringIO.StringIO(html)
    txtin = cStringIO.StringIO(text)

    writer = MimeWriter.MimeWriter(out)

    # Set up some basic headers. Place subject here
    # because smtplib.sendmail expects it to be in the
    # message body, as relevant RFCs prescribe.
    writer.addheader("Subject", subject)
    writer.addheader("MIME-Version", "1.0")

    # Start the multipart section of the message.
    # Multipart/alternative seems to work better
    # on some MUAs than multipart/mixed.
    writer.startmultipartbody("alternative")
    writer.flushheaders()

    # the plain-text section: just copied through, assuming iso-8859-1
    subpart = writer.nextpart()
    pout = subpart.startbody("text/plain", [("charset", 'iso-8859-1')])
    pout.write(txtin.read())
    txtin.close()

    # the HTML subpart of the message: quoted-printable, just in case
    subpart = writer.nextpart()
    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
    pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
    mimetools.encode(htmlin, pout, 'quoted-printable')
    htmlin.close()

    # You're done; close your writer and return the message body
    writer.lastpart()
    msg = out.getvalue()
    out.close()
    
    server = smtplib.SMTP(smtpServer)
    server.sendmail(sender, receiver, msg)
    server.quit()

# any other utilities
def validateEmail(addr):
    "regex that checks for email validity( '@' sign etc..)"
    import re
    email = re.compile("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$",re.I)
    return email.match(addr)

def escapeHTML(s):
    """
    Escape all characters with a special meaning in HTML
    to appropriate character tags
    """
    _ESCAPE_HTML_CHARS=list('&<>":={}()')
    _ESCAPE_HTML_CHARS_TRANS = [(c,'&#%d;' % ord(c)) for c in _ESCAPE_HTML_CHARS]    
    for c,e in _ESCAPE_HTML_CHARS_TRANS:
        s = s.replace(c,e)
    return s
