-
Notifications
You must be signed in to change notification settings - Fork 6
/
bcc.py
138 lines (114 loc) · 4.89 KB
/
bcc.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
134
135
136
137
138
import csv
import getpass
import sys
import os
import re
import base64
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 bcc.py <template_file> <csv_file> (OPTIONAL)<variables_with_same_value_for_all_mails>")
print("python3 bcc.py selections/rejection rejected.csv")
print("python3 bcc.py selections/rejection rejected.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 = file.readline().strip()
lines = file.readlines()[1:] # Slice the list starting from index 2 (line number 3)
email_body = "".join(lines)
# Getting signature
with open(signature_file) as file:
signature = file.read()
def create_message(sender, to, bcc, subject, message):
formatted_sender = formataddr((str(Header('KOSS IIT Kharagpur', 'utf-8')), sender))
message = (
f"From: {formatted_sender}\n"
f"To: {to}\n"
f"Bcc: {bcc}\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, email_body, 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 variables if required by the template - from cli arguments
if len(sys.argv) > 3:
variables = {}
for arg in sys.argv[3:]:
variable, value = arg.split("=")
variables[variable] = value.strip()
email_body = fill_variables(email_body, variables)
subject = fill_variables(subject, variables)
with open(csv_file, newline="") as f:
reader = csv.DictReader(f)
emails = [] # List to store the email addresses for BCC
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
emails.append(email) # Add the email to the BCC list
print(f"BCC TO: {email}")
# Create a single message with BCC recipients
sender = "[email protected]" # Replace with your email address
bcc = ", ".join(emails) # Join the emails with commas for BCC
email_content = email_body + signature
message = create_message(sender, "", bcc, subject, email_content) # Set "to" as an empty string
send_message(service, "me", {"raw": message})
print(f"Email sent to {len(emails)} recipients as BCC.")
print("Script execution completed.")
if __name__ == "__main__":
main(subject, email_body, signature)