I have a python 2.7 script running on windows. It logs in gmail, checks for new e-mails and attachments:
#!/usr/bin/env python
# -*- coding: utf-8 -*-file_types = ["pdf", "doc", "docx"] # download attachments with these extentionslogin = "login"
passw = "password"imap_server = "imap.gmail.com"
smtp_server = "smtp.gmail.com"
smtp_port = 587from smtplib import SMTP
from email.parser import HeaderParser
from email.MIMEText import MIMEText
import sys
import imaplib
import getpass
import email
import datetime
import os
import timeif __name__ == "__main__":try:while True:session = imaplib.IMAP4_SSL(imap_server)try:rv, data = session.login(login, passw)print "Logged in: ", rvexcept imaplib.IMAP4.error:print "Login failed!"sys.exit(1)rv, mailboxes = session.list()rv, data = session.select(foldr)rv, data = session.search(None, "(UNSEEN)")for num in data[ 0 ].split():rv, data = session.fetch(num, "(RFC822)")for rpart in data:if isinstance(rpart, tuple):msg = email.message_from_string(rpart[ 1 ])to = email.utils.parseaddr(msg[ "From" ])[ 1 ]text = data[ 0 ][ 1 ]msg = email.message_from_string(text)got = []for part in msg.walk():if part.get_content_maintype() == 'multipart':continueif part.get('Content-Disposition') is None:continuefilename = part.get_filename()print "file: ", filenameprint "Extention: ", filename.split(".")[ -1 ]if filename.split(".")[ -1 ] not in file_types:continuedata = part.get_payload(decode = True)if not data:continuedate = datetime.datetime.now().strftime("%Y-%m-%d")if not os.path.isdir("CONTENT"):os.mkdir("CONTENT")if not os.path.isdir("CONTENT/" + date):os.mkdir("CONTENT/" + date)ftime = datetime.datetime.now().strftime("%H-%M-%S")new_file = "CONTENT/" + date + "/" + ftime + "_" + filenamef = open(new_file, 'wb')print "Got new file %s from %s" % (new_file, to)got.append(filename.encode("utf-8"))f.write(data)f.close()session.close()session.logout()time.sleep(60)except:print "TARFUN!"
And the problem is that the last print reads garbage:
=?UTF-8?B?0YfQsNGB0YLRjCAxINGC0LXQutGB0YIg0LzQtdGC0L7QtNC40YfQutC4LmRv?=
for example
so later checks don't work. On linux it works just fine.
For now I tryed to d/e[n]code filename to utf-8. But it did nothing. Thanks in advance.