-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.py
More file actions
117 lines (91 loc) · 3.18 KB
/
Copy pathsend.py
File metadata and controls
117 lines (91 loc) · 3.18 KB
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
import os
import sys
import json
import time
import datetime
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from config import (
max_emails_per_hour,
mail_password,
mail_ssl_port,
mail_sender,
mail_host
)
def _send_email(recipient, subject, content):
context = ssl.create_default_context()
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = mail_sender
message["To"] = recipient
message.attach(
MIMEText(content, "html")
)
with smtplib.SMTP_SSL(mail_host, mail_ssl_port, context=context) as server:
server.login(mail_sender, mail_password)
server.sendmail(
mail_sender, recipient, message.as_string()
)
# Getting arguments
try:
template = sys.argv[1]
email_title = sys.argv[2]
email_subject = sys.argv[3]
except IndexError:
sys.exit("Required arguments not defined. Template file, email title and email subject.")
# Defining paths
template_full_path = os.path.join("templates", template)
default_template_full_path = os.path.join("templates", "default.html")
# Checking if files exists
if not os.path.exists("templates/"):
sys.exit("Templates folder not found.")
if not os.path.exists("recipients.json"):
sys.exit("Recipients file not found.")
if not os.path.exists(template_full_path):
use_default = int(input("Template not found. Use the default template? 1 = True, 2 = False \n"))
if use_default == 1:
if not os.path.exists(default_template_full_path):
sys.exit("Template file not found and no default file defined.")
template_full_path = default_template_full_path
if use_default == 2:
sys.exit("Aborting.")
# Getting content
content = open(template_full_path, "r")
content = content.read()
content = content.replace("{{ email_title }}", email_title)
# Getting recipients list
recipients_file = open("recipients.json", "r")
recipients = json.load(recipients_file)
responses = []
send_delay = 600/100
for recipient in recipients:
content = content.replace(
"{{ recipient_name }}",
recipient["name"]
)
try:
_send_email(recipient["email"], email_subject, content)
responses.append(
{
"recipient": recipient["email"],
"status": "success",
"delivery_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
}
)
except Exception as err:
responses.append(
{
"recipient": recipient["email"],
"status": "failure",
"delivery_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
"error_message": str(err)
}
)
if len(recipients) > max_emails_per_hour:
send_delay = int(3600 / max_emails_per_hour) + 1
time.sleep(send_delay)
response_file_name = f"{ email_subject }_{ datetime.datetime.now().strftime('%Y_%m_%d_%H_%M') }.json"
response_file_name_full_path = os.path.join("responses", response_file_name)
with open(response_file_name_full_path, "w") as response_file:
json.dump(responses, response_file)