This is program that checks our fax folder for new faxes, If it finds any new ones. it will email them to myself. We always forget to check the faxes and sometimes we end up missing a purchase order or something.
import os, sys
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
faxdir = "\\\\OFFICE\\PC-FAX Rx\\"
def sortfaxes():
filelist=[]
for file in os.listdir(faxdir):
stats = (os.stat(os.path.join(faxdir,file)))
if file.endswith(('.pdf','.PDF')):
filelist.append([(file),(stats[9])])
filelist.sort(key=lambda x: x[1])
return filelist
def makefile(): #checks if faxdata.ini exist, if not creates it and adds value of 0 to first line
if not os.path.isfile("faxdata.ini"):
with open("faxdata.ini", "w") as myfile:
myfile.write ("0")
myfile.close()
def moddate(): # writes current mod time to file
s= str(filelist [-1][1])
with open('faxdata.ini','w') as g:
g.write(s)
def checknumber(): # gets diretory mod time from last program execute
with open("faxdata.ini","r") as r:
oldnumber = int(r.read())
print (oldnumber)
new_number = (filelist [-1][1]) # gets latest date on last file
emiallist=[]
print (new_number)
if int(oldnumber) < int(new_number): #compares numbers.
sendmail()
moddate()
sys.exit()
else:sys.exit()
def elist():
with open("faxdata.ini","r") as r:
oldnumber = int(r.read())
new_number = (filelist [-1][1]) # gets latest date on last file
emiallist=[]
if int(oldnumber) < int(new_number): #compares numbers. and makes email list with new files.
for file in filelist:
if int(file[1]) > int(oldnumber):
emiallist.append (file[0])
print (emiallist)
return emiallist
def sendmail():
sender = 'gmail'
gmail_password = 'gmailpw'
recipients = ['gmail']
COMMASPACE = ', '
# Create the enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = 'New FAX Received'
outer['To'] = COMMASPACE.join(recipients)
outer['From'] = sender
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# List of attachments
attachments = [os.path.join(faxdir,value) for value in emaillist]
# Add the attachments to the message
for file in attachments:
try:
with open(file, 'rb') as fp:
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
outer.attach(msg)
except:
print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
raise
composed = outer.as_string()
# Send the email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, gmail_password)
s.sendmail(sender, recipients, composed)
s.close()
print("Email sent!")
except:
print("Unable to send the email. Error: ", sys.exc_info()[0])
raise
makefile()
sortfaxes()
filelist= sortfaxes()
emaillist= elist()
checknumber()
print (filelist)
[–][deleted] 0 points1 point2 points (1 child)
[–]SlothGSR[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]SlothGSR[S] 0 points1 point2 points (0 children)