This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailbot.py
executable file
·88 lines (59 loc) · 2.62 KB
/
mailbot.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
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
#!/usr/bin/env python3
import argparse
import os
from os.path import exists
from src import mail_sender, utils
def send_mails(config: dict) -> None:
"""Prepares users' data and sends mails to receivers."""
users_data: list = utils.read_json(f"data/{config['mails_json_file']}")
if exists("logs/sent.log"):
all_mails = [sub['mail'] for sub in users_data]
with open("logs/sent.log") as file:
log_mails = file.readlines()
sent_mails = [line.rstrip() for line in log_mails]
diff = list(set(all_mails) - set(sent_mails))
if diff:
users_data = [x for x in users_data if x['mail'] in diff]
else:
print("No new e-mails have been sent!")
utils.log(f"{utils.current_time()} No new e-mails sent")
return
mail_sender.send_mails(config, users_data)
print("E-mails have been sent!")
utils.log(f"{utils.current_time()} e-mails sent")
def send_mail_with_attachments(config: dict) -> None:
"""Sends mail with results of bot usage to original sender"""
mail_sender.send_mail_with_attachments(config)
print("E-mails with attachments have been sent!")
utils.log(f"{utils.current_time()} e-mail with attachments sent")
def send_emails(config: dict) -> None:
"""Sends e-mails, skips those addresses which already received a message."""
if os.path.getsize(f"data/{config['mails_json_file']}") > 0:
send_mails(config)
send_mail_with_attachments(config)
def main() -> None:
config = utils.load_config()
utils.setup_dirs()
parser = argparse.ArgumentParser(description='Handles sending emails')
parser.add_argument('-c', '--clean', dest='clean', action='store_true',
help='clean logs and UUIDs of the handled e-mail addresses')
parser.add_argument('-g', '--generate', dest='generate', action='store_true',
help='generate new UUIDs for each e-mail account')
parser.add_argument('-s', '--send', dest='send', action='store_true',
help='send a message for each e-mail account')
args = parser.parse_args()
if args.clean:
utils.clean_logs_and_uuids(config)
print("Logs and UUIDs cleaned")
elif args.generate:
utils.convert_mails(config["mails_txt_file"],
config["mails_json_file"])
utils.generate_uuids(config)
print("New UUIDs generated")
elif args.send:
send_emails(config)
print("E-mails have been sent!")
else:
parser.parse_args(['-h'])
if __name__ == "__main__":
main()