Source

openpgp-rollover / src / sendemail.py

#!/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 <membership-committee@gnome.org>"
    
    msg = MTText(emailtext)
    msg['To'] = to
    msg['From'] = from_address
    msg['Subject'] = subject
    msgstr = msg.as_string()

    if s is None:
        s = smtplib.SMTP()
        s.connect(smtp_server)
    try:
        log.info('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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.