aboutsummaryrefslogtreecommitdiff
path: root/sendmail-test.py
blob: 7d366fbdba32d91e231b9ee6b8e9966d294b80da (plain) (blame)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
#
# Send the Daily Bulletin the next morning
# Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

from __future__ import annotations
from configparser import ConfigParser
from typing import Optional
from pprint import pprint
import datetime
import zoneinfo
import argparse
import os

import requests
import msal  # type: ignore


def acquire_token(app: msal.PublicClientApplication, config: ConfigParser) -> str:
    result = app.acquire_token_by_username_password(
        config["credentials"]["username"],
        config["credentials"]["password"],
        scopes=config["credentials"]["scope"].split(" "),
    )

    if "access_token" in result:
        assert isinstance(result["access_token"], str)
        return result["access_token"]
    raise ValueError("Authentication error in password login")


def sendmail(
    token: str,
    subject: str,
    body: str,
    to: list[str],
    bcc: list[str],
    cc: list[str],
    when: Optional[datetime.datetime] = None,
    content_type: str = "HTML",
    importance: str = "Normal",
    reply_to: Optional[str] = None,
) -> str:
    data = {
        "subject": subject,
        "importance": importance,
        "body": {"contentType": content_type, "content": body},
        "toRecipients": [{"emailAddress": {"address": a}} for a in to],
        "ccRecipients": [{"emailAddress": {"address": a}} for a in cc],
        "bccRecipients": [{"emailAddress": {"address": a}} for a in bcc],
    }

    if when is not None:
        if when.tzinfo is None:
            raise TypeError("Naive datetimes are no longer supported")
        utcwhen = when.astimezone(datetime.timezone.utc)
        isoval = utcwhen.isoformat(timespec="seconds").replace("+00:00", "Z")
        data["singleValueExtendedProperties"] = [
            {"id": "SystemTime 0x3FEF", "value": isoval}
        ]

    if not reply_to:
        response = requests.post(
            "https://graph.microsoft.com/v1.0/me/messages",
            json=data,
            headers={
                "Authorization": "Bearer %s" % token,
                "Prefer": 'IdType="ImmutableId"',
            },
            timeout=20,
        ).json()
    else:
        response = requests.post(
            "https://graph.microsoft.com/v1.0/me/messages/%s/createReply" % reply_to,
            json=data,
            headers={
                "Authorization": "Bearer %s" % token,
                "Prefer": 'IdType="ImmutableId"',
            },
            timeout=20,
        ).json()

    try:
        msgid = response["id"]
    except KeyError:
        pprint(response)
        raise ValueError("Unable to add email to drafts")

    assert isinstance(msgid, str)

    response2 = requests.post(
        "https://graph.microsoft.com/v1.0/me/messages/%s/send" % msgid,
        headers={"Authorization": "Bearer " + token},
        timeout=20,
    )

    if response2.status_code != 202:
        pprint(response2.content.decode("utf-8", "replace"))
        raise ValueError(
            "Graph response to messages/%s/send returned something other than 202 Accepted"
            % response["id"],
        )

    return msgid


def main() -> None:
    parser = argparse.ArgumentParser(description="Daily Bulletin Sender")
    parser.add_argument(
        "-d",
        "--date",
        default=None,
        help="the date of the bulletin to send, in local time, in YYYY-MM-DD; defaults to tomorrow",
    )
    parser.add_argument(
        "-r",
        "--reply",
        action="store_true",
        help="Reply to the previous bulletin when sending (BROKEN)",
    )
    parser.add_argument(
        "--config", default="config.ini", help="path to the configuration file"
    )
    args = parser.parse_args()
    config = ConfigParser()
    config.read(args.config)
    if args.date:
        date = datetime.datetime.strptime(args.date, "%Y-%m-%d").replace(
            tzinfo=zoneinfo.ZoneInfo(config["general"]["timezone"])
        )
    else:
        date = datetime.datetime.now(
            zoneinfo.ZoneInfo(config["general"]["timezone"])
        ) + datetime.timedelta(days=1)

    os.chdir(config["general"]["build_path"])

    html_filename = "sjdb-%s.html" % date.strftime("%Y%m%d")
    with open(html_filename, "r", encoding="utf-8") as html_fd:
        html = html_fd.read()

    app = msal.PublicClientApplication(
        config["credentials"]["client_id"],
        authority=config["credentials"]["authority"],
    )
    token = acquire_token(app, config)

    if not args.reply:
        a = sendmail(
            token,
            subject=config["sendmail"]["subject_format"]
            % date.strftime(config["sendmail"]["subject_date_format"]),
            body=html,
            to=config["sendmail"]["to_1"].split(" "),
            cc=[],
            bcc=["s22537@ykpaoschool.cn"],
            when=date.replace(
                hour=17,
                minute=20,
                second=0,
                microsecond=0,
            ),
            content_type="HTML",
            importance="Normal",
        )
        assert a
        with open("last-a.txt", "w") as fd:
            fd.write(a)
        b = sendmail(
            token,
            subject=config["sendmail"]["subject_format"]
            % date.strftime(config["sendmail"]["subject_date_format"]),
            body=html,
            to=config["sendmail"]["to_2"].split(" "),
            cc=[],
            bcc=["s22537@ykpaoschool.cn"],
            when=date.replace(
                hour=17,
                minute=20,
                second=0,
                microsecond=0,
            ),
            content_type="HTML",
            importance="Normal",
        )
        assert b
        with open("last-b.txt", "w") as fd:
            fd.write(b)
    else:
        with open("last-a.txt", "r") as fd:
            last_a = fd.read().strip()
        a = sendmail(
            token,
            subject=config["sendmail"]["subject_format"]
            % date.strftime(config["sendmail"]["subject_date_format"]),
            body=html,
            to=config["sendmail"]["to_1"].split(" "),
            cc=[],
            bcc=["s22537@ykpaoschool.cn"],
            when=date.replace(
                hour=17,
                minute=20,
                second=0,
                microsecond=0,
            ),
            content_type="HTML",
            importance="Normal",
            reply_to=last_a,
        )
        assert a
        with open("last-a.txt", "w") as fd:
            fd.write(a)
        with open("last-b.txt", "r") as fd:
            last_b = fd.read().strip()
        b = sendmail(
            token,
            subject=config["sendmail"]["subject_format"]
            % date.strftime(config["sendmail"]["subject_date_format"]),
            body=html,
            to=config["sendmail"]["to_2"].split(" "),
            cc=[],
            bcc=["s22537@ykpaoschool.cn"],
            when=date.replace(
                hour=17,
                minute=20,
                second=0,
                microsecond=0,
            ),
            content_type="HTML",
            importance="Normal",
            reply_to=last_b,
        )
        assert b
        with open("last-b.txt", "w") as fd:
            fd.write(b)


if __name__ == "__main__":
    main()