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:
pkg.latest:
- pkgs:
- python
- python-mysqldb
- python3
- python3-mysqldb
- python3-psycopg2
maildb-user-vmail:
user.present:

View File

@ -1,12 +1,12 @@
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import argparse
import sys
import MySQLdb
import configparser
import email
from email.utils import parseaddr
import email
#import MySQLdb
import psycopg2
def DBConfig(configfile):
@ -24,7 +24,9 @@ def DBConfig(configfile):
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()
@ -35,7 +37,14 @@ def CloseDB(conn):
def CreateDB(conn, cursor):
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()
except Exception as e:
print(e)
@ -43,7 +52,7 @@ def CreateDB(conn, cursor):
def InsertMail(conn, cursor, sender, recipient, content):
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()
except Exception as e:
print(e)
@ -56,8 +65,8 @@ def ParseMail(input_data):
content += line
msgobj = email.message_from_string(content)
sender = email.utils.parseaddr(msgobj['From'])[1]
recipient = email.utils.parseaddr(msgobj['To'])[1]
sender = parseaddr(msgobj['From'])[1]
recipient = parseaddr(msgobj['To'])[1]
return sender, recipient, content