file2mail/file2mail.py

65 lines
1.6 KiB
Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import config
import fstools
import json
import logging
import mail
import os
import psutil
import report
import signal
import time
CYCLE_TIME = 60
SLEEP_TIME = .5
logger = logging.getLogger('root')
report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
self.kill_now = True
def file2mail():
# Scan for files to send
for filename in fstools.filelist(config.TARGET_FOLDER):
logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
# Send File as E-Mail
try:
mail.send_mail(
config.SEND_FROM,
config.SEND_TO,
"Gesanntes Dokument - %s" % os.path.basename(filename),
"Hier ist das gescannte Dokument vom Scanner in Buchen:",
files=[filename],
server=config.SMTP_SERVER,
username=config.SMTP_USER,
password=config.SMTP_PASSWORD
)
except Exception:
logger.exception("Exception while sending an E-Mail")
else:
# Remove file
logger.debug("Removing file %s", filename)
os.remove(filename)
if __name__ == '__main__':
killer = GracefulKiller()
i = 0
while not killer.kill_now:
time.sleep(.5)
if i >= CYCLE_TIME / SLEEP_TIME:
cyclic_job()
i = 0
else:
i += 1