-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
88 lines (65 loc) · 2.34 KB
/
index.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
# This is a Mailmerge Bot to send multiple Mails with dynamic content to multiple recepient
# Importing Libraries
from Google import Create_Service
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import re
import time
# Variables
CLIENT_SECRET_FILE = "Client/client_secret.json"
API_NAME = "gmail"
API_VERSION = "v1"
SCOPE = ["https://mail.google.com/"]
DELAY = 10 # in seconds, delay after each message
def get_variable(variable_name, count):
# Variable Fetching System
f = open("Variables/" + variable_name + ".txt", "r")
content = f.readlines()
return content[count].replace("\n", "")
def load_email_layout_subject():
# Load Email Layout - Subject
f = open("Layout/subject.txt", "r")
content = f.read()
return content
def load_email_layout_body():
# Load Email Layout - Body
f = open("Layout/body.txt", "r")
content = f.read()
return content
def send_email(to, subject, content):
# Logic to send email via Gmail API
emailMsh = content
mimeMessage = MIMEMultipart()
mimeMessage["to"] = to
mimeMessage["subject"] = subject
mimeMessage.attach(MIMEText(emailMsh, "html"))
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
message = service.users().messages().send(
userId='me', body={'raw': raw_string}).execute()
# Logging
print("[+] Email : " + to + " Subject : " +
subject + " - Email Delivered Successfully !")
print("[ --- Gmail Mailmerge Bot --- ]")
print("[*] Initializing API")
# Initializing Google Service
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPE)
# Looping through all the emails
email_list_file = open("variables/email.txt", "r")
count = 0
for email in email_list_file:
email = email.replace("\n", "")
# Loading Email Subject
subject = load_email_layout_subject()
# Formatting Email Subject with Variables
subject_formatted = re.sub(
r'<<([^>]*)>>', lambda m: get_variable(m.group(1), count), subject)
# Loading Email Body
body = load_email_layout_body()
# Formatting Email Body with Variables
body_formatted = re.sub(
r'<<([^>]*)>>', lambda m: get_variable(m.group(1), count), body)
# Sending Email
send_email(email, subject_formatted, body_formatted)
time.sleep(DELAY)
count += 1