Переглянути джерело

Added a mailer agent, modified the configuration to handle mailer and factory for transport

Steve L. Nyemba 8 роки тому
батько
коміт
5eb6f44cb2
3 змінених файлів з 42 додано та 8 видалено
  1. 4 8
      src/api/index.py
  2. 38 0
      src/utils/mailer.py
  3. BIN
      src/utils/transport.pyc

+ 4 - 8
src/api/index.py

@@ -40,16 +40,12 @@ f = open(PARAMS['path'])
 CONFIG 	= json.loads(f.read())
 HANDLERS= {}
 
-for key in CONFIG :
-	if key == "monitor":
-		continue
-	className = CONFIG[key]['class']
+for key in CONFIG['monitor'] :
+	
+	className = CONFIG['monitor'][key]['class']
 	ref	= "".join(["monitor.",className,"()"])
 	ref 	=  eval(ref)
-	#ref.init(CONFIG[key]['config'])
-
-
-	HANDLERS[key] = {"class":ref,"config":CONFIG[key]["config"]}
+	HANDLERS[key] = {"class":ref,"config":CONFIG['monitor'][key]["config"]}
 
 f.close()
 

+ 38 - 0
src/utils/mailer.py

@@ -0,0 +1,38 @@
+import smtplib
+from email.mime.multipart import MIMEMultipart
+from email.mime.text import MIMEText
+
+class MailAgent :
+	def __init__(self,conf) :
+		self.uid = conf['uid']
+
+		
+		try:
+	
+			self.handler = smtplib.SMTP_SSL(conf['host'],conf['port'])
+			r = self.handler.login(self.uid,conf['password'])
+			#
+			# @TODO: Check the status of the authentication
+			# If not authenticated the preconditions have failed
+			#
+		except Exception,e:
+			print e
+			self.handler = None
+			pass
+
+
+	def send(self,**args) :
+		subject = args['subject']
+		message = args['message']
+		to	= args['to']
+		if '<' in message and '>' in message :
+			message = MIMEText(message,'html')
+		else:
+			message = MIMEText(message,'plain')
+		message['From'] = self.uid
+		message['To']	= to
+		message['Subject'] = subject
+		return self.handler.sendmail(self.uid,to,message.as_string())
+	def close(self):
+		self.handler.quit()
+

BIN
src/utils/transport.pyc