Skip to content
Permalink
Browse files
minor update
  • Loading branch information
johala9 committed Mar 30, 2021
1 parent 02ba05c commit a139f001b7af32d7360c55524dc868a8a7f89ed7
Showing 1 changed file with 79 additions and 71 deletions.
@@ -9,86 +9,94 @@ from email.message import EmailMessage
#allows searching of directory (os is for Miscellaneous operating system interfaces)
import os

# Pull out everything in excel file
#------------------------EDIT BELOW FILE DIR------------------------------
e = pd.read_excel(r"C:\Users\arunj\Documents\UniWork\Python\RESIT\subscribers.xlsx")
# Set arrays as column values
emails = e['SUBSCRIBERS'].values
contentPref = e['ARTICLE'].values
announcementPref = e['ANNOUNCEMENT'].values
#email_sender function
def email_sender(subsFile, articlesFile, announceFile, adminEmail, adminPass):
# Pull out everything in excel file
e = pd.read_excel(subsFile)
# Set arrays as column values
emails = e['SUBSCRIBERS'].values
contentPref = e['ARTICLE'].values
announcementPref = e['ANNOUNCEMENT'].values

# TYPE OF EMAIL
# Question will repeat until 1 OR 2 is inputted
while True:
emailTypeIn = int(input("\nHello Admin:\nType of email - Enter 1 for article. Enter 2 for announcement\n"))
# TYPE OF EMAIL
# Question will repeat until 1 OR 2 is inputted
while True:
emailTypeIn = int(input("\nHello Admin:\nType of email - Enter 1 for article. Enter 2 for announcement\n"))
if emailTypeIn == 1:
print("\n")
subject = "DogsHome - Digest"
break
elif emailTypeIn == 2:
print("\n")
subject = "DogsHome - Announcement"
break
else:
# Repeat if other statments arent met
continue
# Subject is set

# TEMPLATE FILE TO BE USED DEPENDING ON SUBJECT/TYPE
if emailTypeIn == 1:
print("\n")
subject = "DogsHome - Digest"
break
templateDir = articlesFile
elif emailTypeIn == 2:
print("\n")
subject = "DogsHome - Announcement"
break
else:
# Repeat if other statments arent met
continue
# Subject is set
templateDir = announceFile

# Repeat this until template file is found in the directory selected above
while True:
# Pull up all files in directory
all_files = os.listdir(templateDir)
# Visual aid
print(all_files)
templateFile = input("Please enter an above template file FULL name:\n")
# Append \\ into file directory to fix issue
fullTemp = templateDir + "\\" + templateFile
if templateFile in all_files:
# read template file
with open(fullTemp, 'r') as f:
rawHTML = f.read()
break
else:
# Repeat if file not found
continue

# TEMPLATE FILE TO BE USED DEPENDING ON SUBJECT/TYPE
if emailTypeIn == 1:
#------------------------EDIT BELOW FILE DIR------------------------------
templateDir = r"C:\Users\arunj\Documents\UniWork\Python\RESIT\templates\ARTICLES"
elif emailTypeIn == 2:
#------------------------EDIT BELOW FILE DIR------------------------------
templateDir = r"C:\Users\arunj\Documents\UniWork\Python\RESIT\templates\ANNOUNCEMENTS"
# Set msg type
msg = EmailMessage()
msg.set_content(rawHTML, subtype='html')

# Repeat this until template file is found in the directory selected above
while True:
# Pull up all files in directory
all_files = os.listdir(templateDir)
# Visual aid
print(all_files)
templateFile = input("Please enter an above template file FULL name:\n")
# Append \\ into file directory to fix issue
fullTemp = templateDir + "\\" + templateFile
if templateFile in all_files:
# read template file
with open(fullTemp, 'r') as f:
rawHTML = f.read()
break
else:
# Repeat if file not found
continue
# msg.add_header(subject,'text/html')
msg['Subject'] = subject

# Set msg type
msg = EmailMessage()
msg.set_content(rawHTML, subtype='html')
# Log into GMail server with admin/dev email (created bespoke for this proj)
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(adminEmail, adminPass)

# msg.add_header(subject,'text/html')
msg['Subject'] = subject
# Allows correct indexing of arrays pulled from excel
counter = 0
# Loop through all the addresses listed by admin in spreadsheet and send msg (as string)
for email in emails:
# Check prefs for both content and announcement, if Y send.
if emailTypeIn == 1 and contentPref[counter] == 'Y':
server.sendmail(adminEmail, email, msg.as_string())
elif emailTypeIn == 2 and announcementPref[counter] == 'Y':
server.sendmail(adminEmail, email, msg.as_string())
# Sending in quick succession was sometimes causing issue with gmail server
time.sleep(1)
# ensure loop works correctly
counter = counter + 1

# Log into GMail server with admin/dev email (created bespoke for this proj)
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
# Good practive to close server
server.quit()

#--------------------------------------EDIT BELOW LINE------------------------------
server.login(<ADD YOUR OWN EMAIL HERE>, <ADD YOUR OWN PASSWORD HERE>)
#msg to admin to show that process is complete
print("DONE")

# Allows correct indexing of arrays pulled from excel
counter = 0
# Loop through all the addresses listed by admin in spreadsheet and send msg (as string)
for email in emails:
# Check prefs for both content and announcement, if Y send.
if emailTypeIn == 1 and contentPref[counter] == 'Y':
server.sendmail(<ADD YOUR OWN EMAIL HERE>, email, msg.as_string())
elif emailTypeIn == 2 and announcementPref[counter] == 'Y':
server.sendmail(<ADD YOUR OWN EMAIL HERE>, email, msg.as_string())
# Sending in quick succession was sometimes causing issue with gmail server
time.sleep(1)
# ensure loop works correctly
counter = counter + 1
# Change to whatever directory you are using
subsFile = r"C:\Users\arunj\Documents\UniWork\Python\RESIT\subscribers.xlsx"
articlesFile = r"C:\Users\arunj\Documents\UniWork\Python\RESIT\templates\ARTICLES"
announceFile = r"C:\Users\arunj\Documents\UniWork\Python\RESIT\templates\ANNOUNCEMENTS"

# Good practive to close server
server.quit()
# REPLACE WITH EMAIL/PASS
adminEmail = "@gmail.com"
adminPass = ""

#msg to admin to show that process is complete
print("DONE")
email_sender(subsFile, articlesFile, announceFile, adminEmail, adminPass)

0 comments on commit a139f00

Please sign in to comment.