First import
This commit is contained in:
commit
6677a10475
60
app.py
Normal file
60
app.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import imaplib
|
||||||
|
import email
|
||||||
|
import email.header
|
||||||
|
import sqlite3
|
||||||
|
import hashlib
|
||||||
|
import time
|
||||||
|
|
||||||
|
conn = sqlite3.connect("mailboxes.db")
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
sql = conn.cursor()
|
||||||
|
|
||||||
|
def connect(server, username, password, folder):
|
||||||
|
c = imaplib.IMAP4_SSL(server)
|
||||||
|
c.login(username, password)
|
||||||
|
c.select(folder, readonly=True)
|
||||||
|
return c
|
||||||
|
|
||||||
|
def mailbox(c, id):
|
||||||
|
|
||||||
|
r, data = c.search(None, "ALL")
|
||||||
|
for num in data[0].split():
|
||||||
|
r, data = c.fetch(num, '(RFC822)')
|
||||||
|
msg = email.message_from_string(data[0][1])
|
||||||
|
|
||||||
|
subject = str(email.header.decode_header(msg['Subject'])[0][0])
|
||||||
|
mailid = hashlib.sha1(email.header.decode_header(msg['Message-ID'])[0][0]).hexdigest()
|
||||||
|
sender = email.header.decode_header(msg['From'])[0][0]
|
||||||
|
|
||||||
|
print(subject)
|
||||||
|
|
||||||
|
|
||||||
|
sql.execute("SELECT * FROM mails WHERE mailid = ?", (mailid, ))
|
||||||
|
if sql.fetchone() != None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
sql.execute("INSERT INTO mails (mailid, `date`, `from`, `to`, subject, body, account_id) VALUES (?, ?, ?, ?, ?, ?, ?)", (mailid, 0, sender.decode('utf-8', 'ignore'), 'idk', subject.decode('utf-8', 'ignore'), data[0][1].decode('utf-8', 'ignore'), id))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
# Save attachments
|
||||||
|
if msg.get_content_maintype() == 'multipart':
|
||||||
|
for part in msg.walk():
|
||||||
|
if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None and part.get_filename() is not None:
|
||||||
|
filename = str(time.time()) + "_" + part.get_filename()
|
||||||
|
open('attachments/' + filename, 'wb').write(part.get_payload(decode=True))
|
||||||
|
sql.execute("UPDATE mails SET has_attachments = 1 WHERE mailid = ?", (mailid, ))
|
||||||
|
sql.execute("INSERT INTO attachments (mailid, filename) VALUES (?, ?)", (mailid, filename, ))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
sql.execute("SELECT * FROM accounts")
|
||||||
|
accounts = sql.fetchall()
|
||||||
|
for account in accounts:
|
||||||
|
mailbox(connect(account["server"], account["username"], account["password"], account["folder"]), account["id"])
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
conn.commit()
|
1
attachments/.gitkeep
Normal file
1
attachments/.gitkeep
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
3
schema.sql
Normal file
3
schema.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
CREATE TABLE "accounts" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` TEXT, `username` TEXT, `password` TEXT, `folder` TEXT, `last_fetch` INTEGER );
|
||||||
|
CREATE TABLE "attachments" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` INTEGER, `filename` TEXT );
|
||||||
|
CREATE TABLE "mails" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` TEXT, `date` INTEGER, `from` TEXT, `to` TEXT, `subject` TEXT, `body` BLOB, `has_attachments` INTEGER, `account_id` INTEGER );
|
Loading…
Reference in New Issue
Block a user