I use the following class in several of my Turbogears project. Doesn't 
require anything that isn't in the standard library.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from tg import config
default_from = config.get('default_from')

class emailMessage(MIMEMultipart):
    """A simple e-mail message class"""
    def __init__(self, subject='', msgbody='', toaddr='', ccaddr='', 
fromaddr=default_from):
        if type(toaddr) != type(list()):
            toaddr = [toaddr]
        if type(ccaddr) != type(list()):
            ccaddr = [ccaddr]
        super(emailMessage, self).__init__()
        self['From'] = fromaddr
        self['Subject'] = subject
        self['To'] = ';'.join(toaddr)
        self['CC'] = ';'.join(ccaddr)
        self.preamble = subject
        self.smtp_server = config.get('smtp_server')
        self.recipients = toaddr + ccaddr
        self.body = MIMEText(msgbody)

    def sendMessage(self):
    self.attach(self.body)
        s = smtplib.SMTP()
        if self.smtp_server and self.smtp_server != 'disabled':
            s.connect(host=self.smtp_server, port=25)
            s.sendmail(self['From'], self.recipients, self.as_string())
            s.close()


I can then use this class like so:
msg = emailMessage(subject='A sample message', msgbody='This is the body of 
the message', toaddr='[email protected]')
msg.sendMessage()

 - Scott


On Thursday, September 12, 2013 5:41:01 PM UTC-5, Craig Small wrote:
>
> I've got a requirement now to send emails when certain things happen. 
> Is Turbomail still the way to do it, it seems it hasnt changed for 
> quite some time and still needs pylons. 
>
>  - Craig 
> -- 
> Craig Small VK2XLZ   http://enc.com.au/          csmall at : enc.com.au 
> Debian GNU/Linux     http://www.debian.org/      csmall at : debian.org 
> GPG fingerprint:     5D2F B320 B825 D939 04D2  0519 3938 F96B DF50 FEA5 
>

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to