Init
This commit is contained in:
commit
1da01fb98e
74
imap2pdf.py
Normal file
74
imap2pdf.py
Normal file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import string
|
||||
import random
|
||||
import os
|
||||
import imap_tools
|
||||
import time
|
||||
import requests
|
||||
|
||||
imap_server = ""
|
||||
imap_user = ""
|
||||
imap_pass = ""
|
||||
imap_inbox = "INBOX"
|
||||
|
||||
out = "/mnt/thieme_smart_import/"
|
||||
logfile = "/var/log/fax.log"
|
||||
|
||||
def generate_hash():
|
||||
letters = string.ascii_letters
|
||||
letters = letters + string.digits
|
||||
random_string = ""
|
||||
for x in range(0, 4):
|
||||
random_string = random_string + random.choice(letters)
|
||||
return random_string
|
||||
|
||||
def log(command):
|
||||
f = open(logfile, 'a')
|
||||
f.write(time.ctime() + ' ' + command + '\n')
|
||||
f.close()
|
||||
|
||||
def main():
|
||||
try:
|
||||
imap = imap_tools.MailBox(imap_server).login(imap_user, imap_pass)
|
||||
except:
|
||||
sys.exit(0)
|
||||
|
||||
for message in imap.fetch():
|
||||
to = message.to[0].split("@")[0]
|
||||
|
||||
if to == 'fax01' or to == 'fax02' or to == 'fax03':
|
||||
id = '12345'
|
||||
log("Fax for " + id)
|
||||
elif to == 'fax04':
|
||||
id = '12346'
|
||||
log("Fax for " + id)
|
||||
else:
|
||||
imap.move(message.uid, "Unbekannt")
|
||||
log(" > Unknown recipient")
|
||||
continue
|
||||
|
||||
filename = '1^' + id + '^' + generate_hash() + '.pdf'
|
||||
|
||||
for attachment in message.attachments:
|
||||
with open(os.path.join(out, filename), 'wb') as f:
|
||||
f.write(attachment.payload)
|
||||
log(" > Save attachment to " + filename)
|
||||
|
||||
# Mattermost
|
||||
headers = {'Content-Type': 'application/json',}
|
||||
|
||||
if id == '12345':
|
||||
values = '{ "channel": "fax-010203", "text": "Neues Fax in ID 12345 emfangen" }'
|
||||
elif id == '12346':
|
||||
values = '{ "channel": "fax-02", "text": "Neues Fax in ID 12346 emfangen" }'
|
||||
|
||||
response = requests.post('http://chat:8065/hooks/APIKEY', headers=headers, data=values)
|
||||
|
||||
imap.move(message.uid, "Archiv")
|
||||
|
||||
imap.logout()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user