Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created September 13, 2015 19:12
Show Gist options
  • Save AndyNovo/54112d2ab5cbc46dd35e to your computer and use it in GitHub Desktop.
Save AndyNovo/54112d2ab5cbc46dd35e to your computer and use it in GitHub Desktop.
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