Toastie 10 months ago
commit
479ee82c7e
8 changed files with 105 additions and 0 deletions
  1. 6 0
      .env.template
  2. 2 0
      .gitignore
  3. 22 0
      README.md
  4. 5 0
      data/.ash_history
  5. 12 0
      data/Dockerfile
  6. 1 0
      data/requirements.txt
  7. 43 0
      data/txt2mail.py
  8. 14 0
      docker-compose.yml

+ 6 - 0
.env.template

@@ -0,0 +1,6 @@
+MAILTO=user+text2mail@example.com
+MAIL_RELAY_HOST=mail.example.com
+MAIL_PORT=587
+MAIL_FROM=user+text2mail@example.com
+MAIL_USER=user@example.com
+MAIL_PASSWORD=PutYouMailPasswordHere

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.env
+data/mail/

+ 22 - 0
README.md

@@ -0,0 +1,22 @@
+# txt2mail
+
+Sends the content of each text file moved from the folder `1-prep` to `2-ready` via e-mail.
+First line is set as subject and all following lines as message body.
+When done, the mail is moved to `done`
+
+```
+mail/
+├── 1-prep
+├── 2-ready
+└── 3-done
+```
+
+## Configuration
+1. `cp .env.template .env`
+2. Set your mail config in .env
+
+## Usage
+`docker-compose up -d`
+
+## Remarks
+- This container runs as uid/gid 1000

+ 5 - 0
data/.ash_history

@@ -0,0 +1,5 @@
+whoami 
+:q!
+ps
+quit
+exit

+ 12 - 0
data/Dockerfile

@@ -0,0 +1,12 @@
+FROM python:alpine
+
+WORKDIR  /opt/txt2mail
+ENV HOME /opt/txt2mail
+
+COPY . .
+RUN chown -R 1000:1000 . && pip install --no-cache-dir -r requirements.txt
+
+VOLUME   /opt/txt2mail
+
+USER 1000:1000
+CMD [ "python", "./txt2mail.py" ]

+ 1 - 0
data/requirements.txt

@@ -0,0 +1 @@
+inotify-simple

+ 43 - 0
data/txt2mail.py

@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+import os, shutil, smtplib, sys
+from inotify_simple import INotify, flags
+from email.mime.text import MIMEText
+
+# Define and create folders if they don't exist
+prep  = 'mail/1-prep/'
+ready = 'mail/2-ready/'
+done  = 'mail/3-done/'
+os.makedirs(prep,  exist_ok=True)
+os.makedirs(ready, exist_ok=True)
+os.makedirs(done,  exist_ok=True)
+
+# Register inotify event for files moved to the ready folder
+inotify = INotify()
+watch_flags = flags.MOVED_TO
+wd = inotify.add_watch(ready, watch_flags)
+print('Watching for files moved to :' + os.path.dirname(os.path.realpath(__file__)) + '/' + ready)
+
+# Get events and take action
+while True:
+  events = inotify.read()
+  for event in events:
+    # Report file found and read lines
+    print(ready + event.name)
+    lines = open(ready + event.name).readlines()
+
+    # Compose e-mail
+    msg = MIMEText(''.join(lines[1:]))
+    msg['Subject'] = ''.join(lines[0]).strip()
+    msg['From'] = os.environ['MAIL_FROM']
+    msg['To'] = os.environ['MAILTO']
+
+    # Send e-mail
+    s = smtplib.SMTP(host=os.environ['MAIL_RELAY_HOST'], port=os.environ['MAIL_PORT'])
+    # s.set_debuglevel(1)
+    s.starttls()
+    s.login(os.environ['MAIL_USER'], os.environ['MAIL_PASSWORD'])
+    s.sendmail(os.environ['MAIL_FROM'], os.environ['MAILTO'], msg.as_string())
+    s.quit()
+
+    # Move file to the "done" folder
+    shutil.move(ready + event.name,done)

+ 14 - 0
docker-compose.yml

@@ -0,0 +1,14 @@
+version: '2' 
+services:
+  txt2mail:
+    build: 
+      context: ./data/
+      dockerfile: Dockerfile
+    image: toastie89/txt2mail
+    container_name: txt2mail 
+    hostname: txt2mail
+    restart: on-failure:3
+    volumes:
+      - './data/:/opt/txt2mail'
+    env_file:
+      - .env