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()

