The idea was that I could write:
import turboemail
email = TurboEmail()
email.sendkidemail(destinationemail', 'kidtemplate', dict(ARGS))

It is mostly working but there are a few bugs.  I will try to figure it
out tomorrow morning but it might be trivial for more experienced
Python programmers.

It does send html email.

Thanks
Alvin

Bugs
Kid errors - I can not get kid to give me the HTML.  It can not find
the template.
subject line default - I would like to use the title as the default
subject by parsing it from the HTML
Text default - for those pitiful few that can not read html email, I
would like to produce text.

Future enhancements
- produce MHT format to include graphics
- Bulk email support


Filename=turboemail.py
-----------------------------------------------------------------------------------------
import turbogears
import cherrypy
import smtplib
from email.MIMEText import MIMEText
import kid

# from xml.dom.ext.reader.Sax  import FromXmlStream

# Chunks of code taken from Python Cookbook - Art Gillespie then
updated to 2.4 with SMTP authentication
#
# email.smtp_server ="YOUR SMTP SERVER"
# email.smtp_user = "YOUR SMTP USER ID"
# email.smtp_password ="YOUR SMTP PASSWORD"
# email.smtp_fromaddr ="YOUR Default From Address"

class TurboEmail:

        def testmail(self, to_addr, from_addr=None):
                html = "<html><body><h1>It works</h1></body></html>"
                text = "Text Message - It Works"
                subject = "TurboEmail Test"

                if from_addr ==None:
                        from_addr = cherrypy.config.get('email.from_addr')
                self.sendemail( to_addr, html, text, subject, from_addr)
                return

        def sendkidemail(self, to_addr, kid_template, args=None, subject=None,
from_addr=None):
                # set defaults
                import time
#               t = kid.Template(file=kid_template, now=time.ctime())
                t = kid.Template(file='t4modules.templates.welcome',
now=time.ctime())
                html =  t.serialize()
                self.sendemail( to_addr, html, text, subject, from_addr)
                return

        def sendemail(self, to_addr, html, text=None, subject=None,
from_addr=None):
                # set defaults
                smtp_server = cherrypy.config.get('email.smtp_server', 
'localhost')
                smtp_user = cherrypy.config.get('email.smtp_user')
                smtp_password = cherrypy.config.get('email.smtp_password')

                if from_addr ==None:
                        from_addr = cherrypy.config.get('email.from_addr')

# BUG - want to convert html to text
                if text == None:
                        text =
"http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52297 or
http://www.aaronsw.com/2002/html2text/";

                if subject == None:
                        subject = "Turbogear Default Subject"
#       BUG - Want to extract title node
#                       doc = FromXmlStream(html)
#                       for subj_node in doc.getElementsByTagName("title"):
#                               subject = subj_node.firstChild.data
                msg = self.createhtmlmail(from_addr, to_addr, html, text, 
subject)
                s = smtplib.SMTP(smtp_server)
# Remove to get more debug messages
#               s.set_debuglevel(1)

# None - not tested, my smtp server has a password
                if smtp_user != None:
                        s.login(smtp_user, smtp_password)
                s.sendmail( from_addr, to_addr, msg)
                s.close()
                return


# returns a message to send
        def createhtmlmail(self, from_addr, to_addr, html, text, subject):
                import MimeWriter
                import mimetools
                import cStringIO

                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... we put subject here
                # because smtplib.sendmail expects it to be in the
                # message body
                #
                writer.addheader("Subject", subject)
                writer.addheader("To", to_addr)
                writer.addheader("From", from_addr)
                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
                #
                subpart = writer.nextpart()
                subpart.addheader("Content-Transfer-Encoding", 
"quoted-printable")
                pout = subpart.startbody("text/plain", [("charset", 
'us-ascii')])
                mimetools.encode(txtin, pout, 'quoted-printable')
                txtin.close()
                #
                # start the html subpart of the message
                #
                subpart = writer.nextpart()
                subpart.addheader("Content-Transfer-Encoding", 
"quoted-printable")
                #
                # returns us a file-ish object we can write to
                #
                pout = subpart.startbody("text/html", [("charset", 
'ISO-8859-1')])
                mimetools.encode(htmlin, pout, 'quoted-printable')
                htmlin.close()
                #
                # Now that we're done, close our writer and
                # return the message body
                #
                writer.lastpart()
                msg = out.getvalue()
                out.close()
                return msg
-----------------------------------------------------------------------------------------------------

Reply via email to