forked from kossiitkgp/mailing-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-to-one.py
133 lines (110 loc) · 4.8 KB
/
one-to-one.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
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
import csv
import getpass
import sys
import os
import base64
import re
from email.utils import formataddr
from email.header import Header
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from templates.variable_mappings import variable_column_mapping
# Help and number of argument passed checker
if len(sys.argv) < 3:
print("USAGE: python3 one-to-one.py <template_file> <csv_file> (OPTIONAL)<variables_with_same_value_for_all_mails>")
print("python3 one-to-one.py selections/onboarding onboarding.csv")
print("python3 one-to-one.py selections/onboarding onboarding.csv number_of_applicants='250+'")
sys.exit(1)
# If modifying SCOPES, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/gmail.send"]
# Various files being used
template_file = "./templates/" + sys.argv[1]
csv_file = "./csv/" + sys.argv[2]
signature_file = "./templates/signature"
# Getting subject and mail body
lines = []
with open(template_file, "r") as file:
subject_template = file.readline().strip()
lines = file.readlines()[1:] # Slice the list starting from index 2 (line number 3)
email_body_template = "".join(lines)
# Getting signature
with open(signature_file) as file:
signature = file.read()
def create_message(sender, to, subject, message):
formatted_sender = formataddr((str(Header('KOSS IIT Kharagpur', 'utf-8')), sender))
message = (
f"From: {formatted_sender}\n"
f"To: {to}\n"
f"Subject: {subject}\n"
f"MIME-Version: 1.0\n"
f"Content-Type: text/html; charset=utf-8\n"
f"\n"
f"{message}"
)
return base64.urlsafe_b64encode(message.encode("utf-8")).decode("utf-8")
def send_message(service, user_id, message):
try:
message = service.users().messages().send(userId=user_id, body=message).execute()
except Exception as e:
print(f"An error occurred while sending the message: {e}")
def fill_variables(content, variables):
for variable, value in variables.items():
placeholder = "{" + variable + "}"
content = content.replace(placeholder, value)
return content
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(pattern, email):
return True
else:
return False
def main(subject_template, email_body_template, signature):
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
with open("token.json", "w") as token:
token.write(creds.to_json())
service = build("gmail", "v1", credentials=creds)
# Getting extra static variables(those which are same for all mails) if required by the template - from arguments
if len(sys.argv) > 2:
variables = {}
for arg in sys.argv[3:]:
variable, value = arg.split("=")
variables[variable] = value.strip()
email_body_template = fill_variables(email_body_template, variables)
subject_template = fill_variables(subject_template, variables)
with open(csv_file, newline="") as file:
reader = csv.DictReader(file)
for row in reader:
# Getting unique variables values - from the CSV file
required_columns = set([column for variables in variable_column_mapping.values() for column in variables if column in row])
variables = {}
for variable, columns in variable_column_mapping.items():
for column in columns:
if column in required_columns:
variables[variable] = row.get(column, "").strip()
break
email = variables['email'].strip()
if not validate_email(email):
print(f'Invalid mail provided: {email}')
continue
email_body = fill_variables(email_body_template, variables)
subject = fill_variables(subject_template, variables)
sender = "[email protected]"
email_content = email_body + signature
message = create_message(sender, email, subject, email_content)
send_message(service, "me", {"raw": message})
print(f'Message sent to: {email}')
print("Script execution completed.")
if __name__ == "__main__":
main(subject_template, email_body_template, signature)