all repos — listfix @ 072a6a4514d58922c5848206369cbe209c765b2b

Postfix Mailing List Software; Maintained on behalf the of Agency Economy Incorporated NFP.

listfix/email.py (view raw)

 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
import re
import os

re_header_cont = re.compile("^\s+\S+")
re_header_end = re.compile("^\s*$")
re_email_parts = re.compile("(\S+)@(\S+\.\S+)")
re_sender_info = re.compile("^From:\s+\"?([^<>\"]*?)\"?\s*<?(([^<>\"\s]+)@\S+\.[^<>\"\s]+)>?$")
re_auto_reply = re.compile("^Auto-Submitted: (auto-generated|auto-replied)", re.IGNORECASE)

class Email:

    def __init__(self):
        self.content = []
        return

    def set_content(self, content):
        if (type(content) is list):
            if (len(content) > 0):
                self.content = content[:]
            else:
                return False
        else:
            return False

        return True

    def get_content(self):
        if (len(self.content) == 0):
            return False

        return self.content[:]


    def get_header(self, header):
        rval = None

        if (len(self.content) == 0):
            return False

        header = header.rstrip()
        if (header[-1] == ":"):
            header = header[0:-1]

        re_header = re.compile("^" + header + ": ", re.IGNORECASE)

        append_next = False
        in_header = True
        for line in self.content:
            if (in_header):
                if (re_header_cont.match(line) and append_next):
                    rval = rval + " " + line.rstrip().lstrip()
                    continue

                append_next = False
                if (re_header.match(line)):
                    rval = line.rstrip()
                    append_next = True
                elif (re_header_end.match(line)):
                    in_header = False
            else:
                break

        return rval

    def get_sender_email(self):
        sender_email = None

        if (len(self.content) == 0):
            return False

        from_line = self.get_header("From")
        if (from_line):
            if (re_sender_info.match(from_line)):
                results = re_sender_info.search(from_line)
                sender_email = results.group(2)
            else:
                return False
        else:
            return False

        return sender_email

    def get_sender_name(self):
        sender_name = None

        if (len(self.content) == 0):
            return False

        from_line = self.get_header("From")
        if (from_line):
            if (re_sender_info.match(from_line)):
                results = re_sender_info.search(from_line)
                sender_name = results.group(1) if results.group(1) else results.group(3)
            else:
                return False
        else:
            return False

        return sender_name

    def is_auto_reply(self):
        if (len(self.content) == 0):
            return False

        auto_sub_line = self.get_header("Auto-Submitted")
        if (auto_sub_line and re_auto_reply.match(auto_sub_line)):
            return True

        return False

    def strip_headers(self, exclude):
        if (len(self.content) == 0):
            return False

        stripped_content = []

        append_next = False
        in_header = True
        for line in self.content:
            if (in_header):
                if (re_header_cont.match(line) and append_next):
                    stripped_content.append(line)
                    continue

                if (re_header_end.match(line)):
                    stripped_content.append(line)
                    in_header = False
                    continue

                append_next = False
                for ex in exclude:
                    if (re.match("^" + ex + ": ", line, re.IGNORECASE)):
                        stripped_content.append(line)
                        append_next = True
                        break
            else:
                stripped_content.append(line)

        self.content = stripped_content[:]

        return True

    def add_header(self, header):
        if (len(self.content) == 0):
            return False

        header = header.rstrip() + "\n"

        self.content.append(header)

        return

    def add_header_prepend(self, header):
        if (len(self.content) == 0):
            return False

        header = header.rstrip() + "\n"

        self.content.insert(0, header)

        return

    def send(self, recipient):
        if (len(self.content) == 0):
            return False

        p = os.popen(f"/usr/sbin/sendmail -G -i {recipient}", "w")
        for line in self.content:
            p.write(line)
        p.close()

        return True