tingel
Goto Top

Python IMAP Idle Problem

Hallo zusammen,

ich habe in Python ein kleines Scirpt, welches Mail abruft und diese auswertet.
Das läuft und läuft.
Nun wollte ich das ganze mit einem Config-File versehen und jetzt klappt es nicht mehr.
Das Script lässt sich starten, aber es passiert nichts...
Kann mir jemand helfen, sehe den Fehler nicht, auch wenn es an sich nichts Großes sein dürfte.

Das Script:
import imaplib2
from threading import *
import email
import re
import sys
import ConfigParser
import io
import time
import os

# wait for an event of imaplib2
class Idler(object):
	def __init__(self, conn):
		self.thread = Thread(target=self.idle)
		self.M = conn
		self.event = Event()

	def mail_handle():
		for part in msg.walk():
			print ("2");  
			if part.get_content_maintype() == 'multipart':  
				continue
			if part.get('Content-Disposition') is None:  
				continue
			if (attachment_save == '1'):  
			# save attachment in program-folder
				filename = attachment_file;
				fp = open(filename, 'wb');  
				fp.write(part.get_payload(decode=True));
				fp.close();
				print ('%s saved!' % filename);  
			# informations about email-body
			data = part.get_payload(decode=True);
			if (html_mail == '1'):  
				# delete HTML-tags
				plaintext = re.sub("<.*?>", "", data);  
			if not data:
				continue;
			if (mail_save == '1'):  
				# save mail-content in text-file
				f = open(mail_file, 'w');  
				f.write(plaintext);
				f.close();
			# mark mails as seen
			con.store(num, '+FLAGS', '\Seen');  
 
	def start(self):
		self.thread.start()
 
	def stop(self):
		self.event.set()
 
	def join(self):
		self.thread.join()
 
	def idle(self):
        # endless loop
		while True:
			# stop loop, when stop() is called
			if self.event.isSet():
				return;
			self.needsync = False;
			# a callback-methode, when a new mail arrives
			def callback(args):
				print("6");  
				if not self.event.isSet():
					self.needsync = True;
					self.event.set();
				# execute Idle-Call
				self.M.idle(callback=callback)
				self.event.wait();
				if self.needsync:
					self.event.clear();
					self.dosync();
 
	# will be called, when a new mail arrives
	def dosync(self):
		typ, data = con.search(None, 'UNSEEN');  
		c = 0;
		print ("3");  
		for num in data.split():
			typ, data = con.fetch(num, '(RFC822)')  
			c +=1;
			text = data[1];
			msg = email.message_from_string(text);
			# filter mail, Case sensitive
			if (subject_check == "1" and from_check == "1"):  
				if (subject_contains in msg['subject'] and subject_contains in msg['from']):  
					mail_handle();
		
			elif (subject_check == "1" and from_check != "1"):  
				if (subject_contains in msg['subject']):  
					mail_handle();

			elif (subject_check != '1' and from_check == '1'):  
				if (subject_contains in msg['from']):  
					mail_handle();
 
try:
	try:
		from configparser import RawConfigParser
	except ImportError:
		from ConfigParser import RawConfigParser  # ver. < 3.0

	# initialice
	config = RawConfigParser();

	# config-file
	configFilePath = '/home/daniel/Downloads/config.cfg';  
	config.read(configFilePath);

	# getting variable
	server = config.get('E_Mail', 'server');  
	port = config.getint('E_Mail', 'port');  
	name = config.get('E_Mail', 'name');  
	password = config.get('E_Mail', 'password');  
	folder = config.get('E_Mail', 'folder');  
	subject_check = config.getint('E_Mail', 'subject_check');  
	subject_contains = config.get('E_Mail', 'subject_contains');  
	from_check = config.getint('E_Mail', 'from_check');  
	from_contains = config.get('E_Mail', 'from_contains');  
	html_mail = config.getint('E_Mail', 'html_mail');  
	attachment_save = config.getint('E_Mail', 'attachment_save');  
	attachment_file = config.get('E_Mail', 'attachment_file');  
	mail_save = config.getint('E_Mail', 'mail_save');  
	mail_file = config.get('E_Mail', 'mail_file');  
	minutes = config.getint('E_Mail', 'minutes');  
	run_file = config.get('E_Mail', 'run_file');  

	# account
	con = imaplib2.IMAP4_SSL(server, port);
	con.login(name, password);
	# choose folder
	con.select(folder);
	# start "Idler" 
	idler = Idler(con);
	idler.start();
	# quit and new connection
	time.sleep(1*60);
	
finally:
	#cleaning
	idler.stop();
	idler.join();
	con.close();
	con.logout();

Die Config-Datei:
[E_Mail]
server = imap.server.de
port = 993
name = mail@domain.de
password = password
folder = INBOX
subject_check = 1
subject_contains = Test
from_check = 1
from_contains = .de
html_mail = 1
attachment_save = 1
attachment_file = /home/user/attachment.pdf
mail_save = 1
mail_file = /home/user/latest-mail.txt
minutes = 5
run_file = /home/user/start.sh

Content-ID: 311851

Url: https://administrator.de/contentid/311851

Ausgedruckt am: 26.11.2024 um 09:11 Uhr