openpgp-rollover / src /

#!/usr/bin/env python

'''A helper module that defines necessary things for sending email'''

class MTText(MIMEText):
    def __init__(self, _text, _subtype='plain', _charset='utf-8'):
        if not isinstance(_charset, Charset):
            _charset = Charset(_charset)
        if isinstance(_text,unicode):
            _text = _text.encode(_charset.input_charset)
        MIMENonMultipart.__init__(self, 'text', _subtype,
                                       **{'charset': _charset.input_charset})
        self.set_payload(_text, _charset)

def send_email(to, subject, emailtext, from_address = None, smtp_server = 'localhost'):
    log = logging.getLogger('eMail')
    s = None
    from_address = from_address or "GNOME Membership and Elections Committee <>"
    msg = MTText(emailtext)
    msg['To'] = to
    msg['From'] = from_address
    msg['Subject'] = subject
    msgstr = msg.as_string()

    if s is None:
        s = smtplib.SMTP()
    try:'Trying to send to %s, %s', to, subject)
        s.sendmail(from_address, [to,], msgstr)
    except smtplib.SMTPException,e :
        log.warning("Error: Could not send to %s!" % (to,))
        raise e