Automatic text extraction and title extraction is working.
Still working on the template problem, it should be simple but I am
confused by some of the magic of turbogears with regards to path names.
Alvin
import turbogears
import cherrypy
import smtplib
from email.MIMEText import MIMEText
import kid
import sys, os, htmllib, formatter
# 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')
if text == None:
text = self.html2text(html)
if subject == None:
subject = self.get_title(html)
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
def html2text(self, html):
import formatter
import htmllib
from cStringIO import StringIO
outstream = StringIO()
w = formatter.DumbWriter(outstream)
f = formatter.AbstractFormatter(w)
p = htmllib.HTMLParser(f)
p.feed(html)
p.close()
text = outstream.getvalue()
outstream.close()
return text
# use regular expressions to extract the title for subject line
def get_title(self, html):
import re
p = re.compile('<title[^>]*>(.*?)</title>')
m = p.search(html)
if m:
p = re.compile( '<[^>]*>')
title = p.sub('', m.group(0))
return title
# set your own default
return "Email from Turbogears"