Created
September 13, 2015 19:12
-
-
Save AndyNovo/54112d2ab5cbc46dd35e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import email | |
import dateutil.parser | |
import sqlite3 | |
mail = sys.stdin.read() | |
msg = email.message_from_string(mail) | |
datetime = dateutil.parser.parse(msg["DATE"]) | |
thedata = { | |
"subject" : msg["SUBJECT"], | |
"from" : msg["FROM"], | |
"to" : msg["TO"], | |
"date" : datetime.strftime("%m%d"), | |
"time" : datetime.strftime("%H%M%S") | |
} | |
for part in msg.walk(): | |
if part.get_content_type() == "text/plain": | |
thedata["content"] = part.get_payload() | |
query = "INSERT INTO emails (from_email, to_email, content, subject, date, time) values (:from, :to, :content, :subject, :date, :time)" | |
conn = sqlite3.connect("/path/to/the/database/emails.sqlite") | |
cur = conn.cursor() | |
cur.execute(query, thedata) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment