updated maildb state

This commit is contained in:
Paul 2020-09-06 16:30:37 +02:00
parent c58898c516
commit 705fdec133
2 changed files with 20 additions and 12 deletions

View File

@ -3,10 +3,9 @@
maildb-pkg: maildb-pkg:
pkg.latest: pkg.latest:
- pkgs: - pkgs:
- python
- python-mysqldb
- python3 - python3
- python3-mysqldb - python3-mysqldb
- python3-psycopg2
maildb-user-vmail: maildb-user-vmail:
user.present: user.present:

View File

@ -1,12 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#-*- coding: utf-8 -*-
import argparse import argparse
import sys import sys
import MySQLdb
import configparser import configparser
import email
from email.utils import parseaddr from email.utils import parseaddr
import email
#import MySQLdb
import psycopg2
def DBConfig(configfile): def DBConfig(configfile):
@ -24,7 +24,9 @@ def DBConfig(configfile):
def ConnectMysqlDB(dbconfig): def ConnectMysqlDB(dbconfig):
conn = MySQLdb.connect(**dbconfig) connstr = f"host={dbconfig['host']} dbname={dbconfig['db']} user={dbconfig['user']} password={dbconfig['passwd']}"
conn = psycopg2.connect(connstr)
#conn = MySQLdb.connect(**dbconfig)
return conn, conn.cursor() return conn, conn.cursor()
@ -35,7 +37,14 @@ def CloseDB(conn):
def CreateDB(conn, cursor): def CreateDB(conn, cursor):
try: try:
cursor.execute('''CREATE TABLE IF NOT EXISTS mail (id int(10) PRIMARY KEY AUTO_INCREMENT, sender varchar(50), recipient varchar(50), date datetime NOT NULL DEFAULT current_timestamp(), content mediumtext);''') cursor.execute('''CREATE TABLE IF NOT EXISTS mail (
id serial NOT NULL,
sender text NULL,
recipient text NULL,
"date" timestamp NOT NULL DEFAULT CURRENT_DATE,
"content" text NULL,
CONSTRAINT mail_pkey PRIMARY KEY (id)
)''')
conn.commit() conn.commit()
except Exception as e: except Exception as e:
print(e) print(e)
@ -43,7 +52,7 @@ def CreateDB(conn, cursor):
def InsertMail(conn, cursor, sender, recipient, content): def InsertMail(conn, cursor, sender, recipient, content):
try: try:
cursor.execute("INSERT INTO mail (sender, recipient, content) values ('%s','%s','%s')" % (sender, recipient, content)) cursor.execute(f"INSERT INTO mail (sender, recipient, content) values ('{sender}','{recipient}','{content}');")
conn.commit() conn.commit()
except Exception as e: except Exception as e:
print(e) print(e)
@ -56,8 +65,8 @@ def ParseMail(input_data):
content += line content += line
msgobj = email.message_from_string(content) msgobj = email.message_from_string(content)
sender = email.utils.parseaddr(msgobj['From'])[1] sender = parseaddr(msgobj['From'])[1]
recipient = email.utils.parseaddr(msgobj['To'])[1] recipient = parseaddr(msgobj['To'])[1]
return sender, recipient, content return sender, recipient, content