Commits

Tobias Mueller committed 5b8f6f2

Imported sendemail module from my other GNOME script

Hm. One might create a Pypi package to depend on.

Comments (0)

Files changed (1)

+#!/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