Skip to content
Permalink
d95f426004
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
108 lines (93 sloc) 3.72 KB
#enable sending of emails
import smtplib
#used to prevent issue with gmail server
import time
#allow programme to pull out data from spreadsheet provided to admin
import pandas as pd
#allow creation of email type
from email.message import EmailMessage
#allows searching of directory (os is for Miscellaneous operating system interfaces)
import os
def loginEmail(adminEmail, adminPass):
# 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)
return server
#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"))
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 sets
# TEMPLATE FILE TO BE USED DEPENDING ON SUBJECT/TYPE
if emailTypeIn == 1:
templateDir = articlesFile
elif emailTypeIn == 2:
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
# Set msg type
msg = EmailMessage()
msg.set_content(rawHTML, subtype='html')
# msg.add_header(subject,'text/html')
msg['Subject'] = subject
server = loginEmail(adminEmail, adminPass)
# 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
# Good practive to close server
server.quit()
#msg to admin to show that process is complete
print("DONE")
# USER: CHANGE THESE VARIABLES BEFORE FUNCTION CALL
# 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"
# REPLACE WITH EMAIL/PASS
adminEmail = "arunjohal.email.dev@gmail.com"
adminPass = ""
email_sender(subsFile, articlesFile, announceFile, adminEmail, adminPass)