forked from dray89/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailMailMerge.py
76 lines (59 loc) · 2.2 KB
/
EmailMailMerge.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
#https://docs.python-guide.org/scenarios/xml/
from __future__ import print_function
import smtplib, openpyxl, sys
from mailmerge import MailMerge
from datetime import date
template = input("Input Document Name and Extension: " )
username = input("Input Sender Email: ")
password = input("Input Password: ")
excel_doc = input("Input Excel Document: ")
from openpyxl import load_workbook
workbook = load_workbook(excel_doc)
sheet = workbook['Sheet1']
FinalDispositionColumn = sheet.get_highest_column()
FinalDispositionStatus = sheet.cell(row=1, column=lastCol).value
okay = ["Email Sent", "voicemail left"] #All FinalDispositions Okay to Contact
contacts = {}
for r in range(2, sheet.get_highest_row() + 1): # Check each contact's best disposition. Check to ensure all Final Dispositions are correct.
FinalDisposition = sheet.cell(row=r, column=lastCol).value
if FinalDisposition in okay:
name = sheet.cell(row=r, column=1).value
email = sheet.cell(row=r, column=2).value
contacts[name] = email
else:
pass
smtpObj = smtplib.SMTP('smtp.domain', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(username, sys.argv[1])
for name, email in contacts.items():
from mailmerge import MailMerge
document = MailMerge(template)
mergefields = []
mergefields.append(document.get_merge_fields())
document.merge(mergefields)
sendmailStatus = smtpObj.sendmail(username, email, body)
if sendmailStatus != {}:
document('There was a problem sending email to %s: %s' % (email, sendmailStatus))
smtpObj.quit()
range = input("How many contacts?: ")
contacts = []
contacts.append(input("Input Contact List: "))
address = []
address.append()
sent_from = "[email protected]"
to =
subject = input("Enter Email Subject:" )
body = document(mailmerge)
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" %(sent_from, to, subject, body) # Log in to email account. Send out emails.
server = smtplib.SMTP_SSL('smtp.domain', 465)
server.ehlo_or_helo_if_needed()
server.login(domain_user, domain_password)
server.sendmail(sent_from, to, email_text)
server.close()
print("Email Sent!")