-
Notifications
You must be signed in to change notification settings - Fork 3
/
emailRecruit.py
executable file
·109 lines (97 loc) · 3.67 KB
/
emailRecruit.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
#!/usr/bin/python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
import xlrd # For working with Excel
class Recruiter(object):
"""
Class to hold Recruiter Object
Recruiter Object has following attributes:-
* First Name of Recruiter (String)
* Last Name of Recruiter (String)
* Company Name of Recruiter (String)
* Mail ID of Recruiter (String)
* Job Role of Opening (String)
* Job ID of Opening (String)
Recruiter Object has following functions:-
__init__() - to initialize all values
__str__() - to return all attributes when object is used as string
"""
def __init__(self, firstName, lastName, company, mailID, position, jobID):
self.firstName = firstName
self.lastName = lastName
self.company = company
self.mailID = mailID
self.position = position
self.jobID = jobID
def __str__(self):
return("Recruiter object:\n"
" firstName = {0}\n"
" lastName = {1}\n"
" Company = {2}\n"
" mailID = {3}\n"
" position = {4} \n"
" jobID = {5}"
.format(self.firstName, self.lastName, self.company,
self.mailID, self.position, self.jobID))
def getBody(name, company, position):
"""
Function to read template mail from mailTemplate and replace generic strings with recruiter specific values
Takes Name(string), Company(string), Position(string) as arguments
Returns Mail Body (string)
"""
try:
file = open("mailTemplate", "r")
body = file.read()
file.close()
return body.replace("$insertName$", name).replace("$insertCompany$", company).replace("$insertPosition$", position)
except IOError:
print "Error: File does not seem to exist."
return "Hello"
fromaddr = "[email protected]" # Replace with your mail ID
msg = MIMEMultipart()
msg['From'] = fromaddr
# Login to Gmail
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD") # Replace with your password here
# Read from and Attach Resume to Mail
filename = "DanisFermiResume.pdf"
attachment = open(filename, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
# Opening and Parsing Excel data into list of Recruiter Objects
wb = xlrd.open_workbook("recruiters.xlsx")
for sheet in wb.sheets():
number_of_rows = sheet.nrows
number_of_columns = sheet.ncols
recruiters = []
rows = []
for row in range(1, number_of_rows):
values = []
for col in range(number_of_columns):
value = (sheet.cell(row,col).value)
try:
value = str(int(value))
except ValueError:
pass
finally:
values.append(value)
recruiter = Recruiter(*values)
recruiters.append(recruiter)
# Iterate over recruiter list and send email
for recruiter in recruiters:
msg['To'] = recruiter.mailID
msg['Subject'] = "Danis Fermi-MS Student-Job Application for $insertPosition$ position".replace("$insertPosition$", recruiter.position)
if recruiter.jobID != "":
msg['Subject'].append("(Job Id: "+recruiter.jobID+")")
body = getBody(recruiter.firstName, recruiter.company, recruiter.position)
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, recruiter.mailID, text)
server.quit()