diff options
-rwxr-xr-x | lib/cnucnu/mail.py | 82 | ||||
-rwxr-xr-x | lib/cnucnu/tests/mail_test.py | 51 | ||||
-rwxr-xr-x | runtests.sh | 1 |
3 files changed, 134 insertions, 0 deletions
diff --git a/lib/cnucnu/mail.py b/lib/cnucnu/mail.py new file mode 100755 index 0000000..7928973 --- /dev/null +++ b/lib/cnucnu/mail.py @@ -0,0 +1,82 @@ +#!/usr/bin/python +# vim: fileencoding=utf8 foldmethod=marker +#{{{ License header: GPLv2+ +# This file is part of cnucnu. +# +# Cnucnu is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Cnucnu is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cnucnu. If not, see <http://www.gnu.org/licenses/>. +#}}} + +import smtplib +from email.mime.text import MIMEText +from email.header import Header +from email.utils import parseaddr, formataddr + + +def encode_addr(addr): + real_name, email_address = parseaddr(addr) + try: + # check for non-ascii characters + real_name = real_name.encode("ascii") + except (UnicodeEncodeError, UnicodeDecodeError), e: + real_name = str(Header(ensure_encoding(real_name), "UTF-8")) + return formataddr((real_name, email_address)) + +def ensure_encoding(text, encoding="UTF-8"): + try: + text = text.encode("UTF-8") + # text not a unicode string like e.g. u"unicode string" + # Assume that it is encoded in UTF-8 for convenience + except UnicodeDecodeError, e: + text = text.decode("UTF-8").encode("UTF-8") + + return text + +class Message: + def __init__(self, sender, receipient, subject, message): + self.sender = sender + self.receipient = receipient + + msg = MIMEText(ensure_encoding(message), _charset="UTF-8") + msg['Subject'] = Header(ensure_encoding(subject), "UTF-8") + msg['From'] = encode_addr(sender) + msg['To'] = encode_addr(receipient) + + self.msg = msg + self.as_string = msg.as_string + + +class Mailer: + def __init__(self, smtp_host): + self.smtp_host = smtp_host + + def send(self, message): + + s = smtplib.SMTP(self.smtp_host) + s.sendmail(message.sender, [message.receipient], mesagge.as_string()) + s.quit() + +message_template_outdated = """ The latest upstream release for %(name)s is %(latest_upstream)s, but Fedora Rawhide only contains %(repo_version)s. + +This mail is sent, because your package is listed at: +https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes + +Eventually, there will be bugs filed for outdated packages, but this is not yet implemented. +""" + + +if __name__ == "__main__": + import pickle + mailer = Mailer(smtp_host="") + + data_file = open("../../data.pickle") diff --git a/lib/cnucnu/tests/mail_test.py b/lib/cnucnu/tests/mail_test.py new file mode 100755 index 0000000..f623bd0 --- /dev/null +++ b/lib/cnucnu/tests/mail_test.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# vim: fileencoding=utf8 foldmethod=marker +#{{{ License header: GPLv2+ +# This file is part of cnucnu. +# +# Cnucnu is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Cnucnu is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cnucnu. If not, see <http://www.gnu.org/licenses/>. +#}}} + +import unittest + +import sys +sys.path.insert(0, '../..') +sys.path.insert(0, '..') +sys.path.insert(0, './lib') + + +from cnucnu.mail import Message + +class MailTest(unittest.TestCase): + + def testCreateMessage(self): + m = Message("me@example.com", "you@example.com", "subject", "message") + + def testMessageEncoding(self): + sender = "Mr. Umläut <u@example.com>" + receipient = "ß <l@example.com>" + subject = "ä" + message = "Ö" + + m = Message(sender, receipient, subject, message) + self.assertEqual(m.receipient, receipient) + self.assertEqual(m.sender, sender) + message_string = 'MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: base64\nSubject: =?utf-8?b?w6Q=?=\nFrom: =?utf-8?b?TXIuIFVtbMOkdXQ=?= <u@example.com>\nTo: =?utf-8?b?w58=?= <l@example.com>\n\nw5Y=\n' + self.assertEqual(message_string, m.as_string()) + + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromTestCase(MailTest) + unittest.TextTestRunner(verbosity=2).run(suite) + #unittest.main() diff --git a/runtests.sh b/runtests.sh index 4b350fd..41b99ed 100755 --- a/runtests.sh +++ b/runtests.sh @@ -18,3 +18,4 @@ #}}} lib/cnucnu/tests/package_list_test.py +lib/cnucnu/tests/mail_test.py |