summaryrefslogtreecommitdiffstats
path: root/lib/cnucnu/mail.py
blob: 7928973bfa547d0cd4792940730efdde2def30cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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")