Application which monitors a folder and sends each file via mail
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

file2mail.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import config
  4. import fstools
  5. import json
  6. import logging
  7. import mail
  8. import os
  9. import psutil
  10. import report
  11. import signal
  12. import time
  13. CYCLE_TIME = 60
  14. SLEEP_TIME = .5
  15. logger = logging.getLogger('root')
  16. report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
  17. class GracefulKiller:
  18. kill_now = False
  19. def __init__(self):
  20. signal.signal(signal.SIGINT, self.exit_gracefully)
  21. signal.signal(signal.SIGTERM, self.exit_gracefully)
  22. def exit_gracefully(self, signum, frame):
  23. self.kill_now = True
  24. def file2mail():
  25. # Scan for files to send
  26. for filename in fstools.filelist(config.TARGET_FOLDER):
  27. logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
  28. # Send File as E-Mail
  29. try:
  30. mail.send_mail(
  31. config.SEND_FROM,
  32. config.SEND_TO,
  33. "Gesanntes Dokument - %s" % os.path.basename(filename),
  34. "Hier ist das gescannte Dokument vom Scanner in Buchen:",
  35. files=[filename],
  36. server=config.SMTP_SERVER,
  37. username=config.SMTP_USER,
  38. password=config.SMTP_PASSWORD
  39. )
  40. except Exception:
  41. logger.exception("Exception while sending an E-Mail")
  42. else:
  43. # Remove file
  44. logger.debug("Removing file %s", filename)
  45. os.remove(filename)
  46. if __name__ == '__main__':
  47. killer = GracefulKiller()
  48. i = 0
  49. while not killer.kill_now:
  50. time.sleep(.5)
  51. if i >= CYCLE_TIME / SLEEP_TIME:
  52. file2mail()
  53. i = 0
  54. else:
  55. i += 1