-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
95 lines (78 loc) · 3.48 KB
/
main.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
from __future__ import print_function
import pickle
import os.path
import base64
import csv
import argparse
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
def create_message_with_attachment(sender, to, subject, message_text, file):
message = MIMEMultipart()
message['to'] = to
message['from'] = sender
message['subject'] = subject
msg = MIMEText(message_text)
message.attach(msg)
filename = os.path.basename(file)
with open(filename, "rb") as f:
msg = MIMEApplication(f.read(), _subtype="pdf")
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
return {'raw': raw_message.decode("utf-8")}
def send_message(service, user_id, message):
try:
message = service.users().messages().send(userId=user_id, body=message).execute()
print('Message Id: %s' % message['id'])
print('Message Sent!')
return message
except Exception as e:
print('An error occurred: %s' % e)
return None
def main(your_email, subject, resume_path):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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=3000)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
with open('message.txt') as fil:
body = fil.read()
recruiters = list()
with open('emails.csv', newline='') as csvfile:
emails = csv.reader(csvfile, delimiter=',')
line_count = 0
for row in emails:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
# print(f'\t{row[0]} works at {row[1]}, their email is {row[2]}.')
recruiters.extend([row])
line_count += 1
print(f'Processed {line_count} lines.')
for r in recruiters:
temp = f'{body}'
temp = temp.replace('{first}', r[0]).replace('{company}', r[1])
message = create_message_with_attachment(your_email, r[2], subject, temp, resume_path)
send_message(service, your_email, message)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='This program allows you to send multiple emails to different people using the Gmail API')
parser.add_argument("--email", help="Your email address, i.e. 'yourname@gmail.com'", required=True)
parser.add_argument("--subject", help="Subject line of email, i.e. 'Purdue Undergrad - Software Engineering Internship'", required=True)
parser.add_argument("--resume_path", help="Name of resume file in the directory, i.e. 'AdhikaryParas2020.pdf'", required=True)
args = parser.parse_args()
main(your_email=args.email, subject=args.subject, resume_path=args.resume_path)