-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail.py
49 lines (38 loc) · 1.01 KB
/
sendmail.py
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
"""
Drop in replacement for `sendmail`
to pass emails to the Anope mailer daemon.
"""
import datetime
import email
import sys
import traceback
import uuid
from email.message import Message
from pathlib import Path
script_dir = Path(__file__).resolve().parent
cwd = Path().resolve()
def main():
"""
Main function\
"""
if len(sys.argv) > 1:
mail_dir = sys.argv[1]
else:
mail_dir = script_dir / 'emails'
text = sys.stdin.read()
mail_dir = Path(mail_dir).resolve()
msg = email.message_from_string(text) # type: Message
file = (mail_dir / f'{uuid.uuid4()}.mail')
file.touch(0o600)
with file.open('w', encoding='utf-8') as file:
file.write(msg.as_string())
def run():
"""Main wrapper"""
try:
main()
except Exception as err:
with (script_dir / 'log.txt').open('a', encoding='utf8') as file:
print(datetime.datetime.now().isoformat(), traceback.format_exc(), file=file)
raise err
if __name__ == '__main__':
run()