Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- # Wikemail.py - import your emails into dokuwiki to create
- # a searchable, wiki-able archive of you mail - Duncan Gough 23/10/04
- # Ronald van Raaij 2019-03-20:
- # Updated libraries and code to python3
- # Added commandline arguments for setting username, password, servername, outputdirectory
- # Used BeatifulSoup to extract html from mail. Todo: add threading/waiting
- # dependencies:
- # pip3 install imap_tools
- # pip3 install python-dateutil
- # pip3 install beautifulsoup4
- # pip3 install lxml
- # for chown to work, the script needs to be run with sudo
- import sys, re, email, os, logging, getopt, mmap
- import email.utils
- from imap_tools import MailBox
- from dateutil import parser
- from bs4 import BeautifulSoup, Doctype
- #from TaskThread import TaskThread
- #import threading
- def main(argv):
- user=''
- password=''
- server=''
- outputdir=''
- logFile = 'import_mail.log'
- loglevel = 2
- index_file='berichten.txt'
- filepermissions = 0o775
- owner_group = (1,4)
- logging.basicConfig( filename = logFile,filemode = 'w',level = logging.DEBUG,format = '%(asctime)s - %(levelname)s: %(message)s',\
- datefmt = '%m/%d/%Y %I:%M:%S %p' )
- logging.debug("Starting of the import")
- try:
- opts, args = getopt.getopt(argv,"hu:p:s:o:l",["user=","pass=","server=","outputdir=",'loglevel='])
- except getopt.GetoptError:
- print( 'test.py -u <user> -p <password> -s <server> -o <outputdir>')
- sys.exit(2)
- for opt, arg in opts:
- if opt == '-h':
- print ('test.py -i <user> -p <password> -s <server> -o <outputdir>')
- sys.exit()
- elif opt in ("-u", "--user"):
- user = arg
- elif opt in ("-p", "--pass"):
- password = arg
- elif opt in ("-s", "--server"):
- server = arg
- elif opt in ("-o", "--outputdir"):
- outputdir = arg
- if loglevel==4:
- logging.debug('server name is "' + server)
- logging.debug('user name is "' + user)
- logging.debug('password is "' + password)
- logging.debug('Output dir is "' + outputdir)
- #assume wiki starts in ../docuwiki/data/pages to determine namespace start
- if outputdir[-1:]=='/':
- namespace=outputdir.rpartition('pages/')[2]
- else:
- outputdir=outputdir+"/"
- namespace=outputdir.rpartition('pages/')[2]
- namespace=namespace[:-1].lower()
- namespace=namespace.replace('/', ":")
- logging.debug('namespace='+ namespace)
- try:
- mailbox = MailBox(server, ssl=False)
- mailbox.login(user, password )
- logging.debug('Logged in')
- except Exception as e:
- logging.debug(repr(e))
- print('unable to log in or other imap access error: ' + (repr(e)))
- sys.exit(2)
- for msg in mailbox.fetch('(UNDELETED)'):
- logging.debug(msg.date)
- dateasdate=parser.parse(msg.date)
- wiki_date = dateasdate.strftime('%Y%m%d')
- wiki_date = str(wiki_date)
- wiki_from = msg.from_.replace( '@', '_at_' )
- wiki_to = ', '.join(msg.to)
- wiki_filename = msg.uid + msg.subject.lower()
- wiki_filename = wiki_filename.replace( 're: ', '' )
- wiki_filename = wiki_filename.replace( 'fwd: ', '' )
- wiki_filename = wiki_filename.replace( ' ', '_' )
- wiki_filename = wiki_filename.replace( '/', '_' )
- wiki_filename = wiki_filename.replace( ',', '_' )
- soup=BeautifulSoup(msg.html, "lxml")
- for e in soup.contents:
- if isinstance(e, Doctype):
- e.extract()
- break
- email_body = str(soup)
- email_body = email_body.replace("<?xml version=?1.0? encoding=?UTF-8??>", "" )
- message = ''
- message += '===== ' + msg.subject.upper() + '=====\n\n'
- message += 'From: ' + msg.from_ + ')\n\n'
- message += 'To: ' + wiki_to + '\n\n'
- message += 'Date: ' + wiki_date + '\n\n'
- message += email_body + "\n\n"
- link_to_message='[['+ namespace + ':' + wiki_date.replace( ' ', '_' ) + ':' + wiki_filename + '|' + wiki_date + '-'+ msg.subject + ']]\n\n'
- outputdir_with_date=outputdir + wiki_date.replace( ' ', '_' )
- try:
- os.stat(outputdir_with_date )
- except:
- os.makedirs(outputdir_with_date )
- f = open(outputdir_with_date + '/' + wiki_filename + '.txt', 'a' )
- f.write(message)
- f.close
- os.chmod(outputdir_with_date, filepermissions)
- os.chown(outputdir_with_date, owner_group[0], owner_group[1])
- # make a link in the main page/textfile
- date_heading='=== ' + wiki_date + ' ===' +'\n\n'
- with open(outputdir + index_file, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
- if s.find(date_heading.encode()) == -1:
- f = open(outputdir + index_file, 'a+' )
- f.write(date_heading )
- f = open(outputdir + index_file, 'a+' )
- f.write(link_to_message)
- f.close
- mailbox.logout()
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement