Created
February 7, 2015 18:04
-
-
Save frafra/7a605c48efb2d0b05be9 to your computer and use it in GitHub Desktop.
Save your emails to file (both content and attachments)
This file contains 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
#!/usr/bin/env python | |
#! coding: utf-8 -*- | |
# | |
# email-archiver.py | |
# | |
# Copyright (C) 2015 - Francesco Frassinelli | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
import os | |
import imaplib | |
import email | |
import email.utils | |
import datetime | |
import locale | |
storage = "/home/frafra/mail/" | |
conf = [ | |
[ | |
"utente@gmail.com", | |
"password", | |
"imap.gmail.com", | |
"Archivio", | |
None, | |
], | |
] | |
locale.setlocale(locale.LC_ALL, "it_IT.UTF8") | |
def getMsgs(usernm, passwd, servername): | |
conn = imaplib.IMAP4_SSL(servername) | |
conn.login(usernm,passwd) | |
conn.select("Inbox") | |
typ, data = conn.search(None, "ALL") | |
for num in data[0].split(): | |
typ, data = conn.fetch(num, "(RFC822)") | |
msg = email.message_from_string(data[0][1]) | |
typ, data = conn.store(num, "-FLAGS", "\\Seen") | |
yield msg | |
typ, data = conn.store(num, "+FLAGS", "\\Deleted") | |
conn.expunge() | |
def date_from_email(text): | |
date_tuple = email.utils.parsedate_tz(text) | |
date_str = email.utils.mktime_tz(date_tuple) | |
return datetime.datetime.fromtimestamp(date_str) | |
if __name__ == "__main__": | |
for usern, passwd, servername, path, rule in conf: | |
os.chdir(os.path.join(storage, path)) | |
for msg in getMsgs(usern, passwd, servername): | |
subject = msg["Subject"].strip() | |
if rule: | |
if subject.upper() != rule.upper(): | |
continue | |
date = date_from_email(msg["Date"]).strftime("%Y%m%d") | |
base = "[%s - %s]" % (date, subject) | |
for part in msg.walk(): | |
payload = part.get_payload(decode=1) | |
filename = part.get_filename() or "Messaggio.txt" | |
if payload: | |
filename = base + " " + filename | |
filename = filename.replace("/", "-") | |
if not os.path.exists(filename): | |
open(filename, "w").write(payload) | |
else: | |
print "%s already exists" % filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment