def send_mail(self,to,subject, _body):              
        body = _body.replace('\n', '\r\n')
        guid=str(uuid.uuid4())
        body = config.get('ckan.site_url') + "/user/register?guid=" + guid + "&activate"
        # Prepare actual message
        message = """From: %s
        To: %s
        Subject: %s

        %s 
        """ % (config.get('dk.aarhuskommune.odaa_from'),str(to),subject,body)

        try:
            result=self.connection()       
            connectString = """dbname='%s' user='******' host='%s' password='******'""" % (result["database"],result["user"],result["host"],result["password"])
            conn = psycopg2.connect(connectString)
            c = conn.cursor()            
            p=request.params["password1"]            
            password=self._encode(result["password"],p)
            now = datetime.datetime.now()                                    
            sql="INSERT INTO mailnotifikation VALUES ('" + guid + "','" + str(now) + "','" + request.params["name"] + "','" +request.params["fullname"] +   "','" + request.params["email"] + "','" + password + "','False')"
            c.execute(sql)
	    conn.commit()

            nowlm = now - datetime.timedelta(days=int(config.get('dk.aarhuskommune.odaa_days')))
            sNowlm=nowlm.strftime('%Y-%m-%d')            
            sql="delete from mailnotifikation where _date<'%s'" % (sNowlm);           
            c.execute(sql)
	    conn.commit()
            conn.close()
            smtpObj = smtplib.SMTP('localhost')        
            to=to.split(',')
            smtpObj.sendmail(config.get('dk.aarhuskommune.odaa_from'), to, message)
        except Exception as e:
            logging.error('Error: unable to send email. %s ',e)
 def emailNotifikation(self,context):   
     result=self.connection()           
     connectString = """dbname='%s' user='******' host='%s' password='******'""" % (result["database"],result["user"],result["host"],result["password"])
     conn = psycopg2.connect(connectString)
     c = conn.cursor()                     
     now = datetime.datetime.now()        
     date=now.strftime('%Y/%m/%d %H:%M:%S')
     sql="SELECT * FROM mailnotifikation where _guid='%s' and registered='False';" % request.params.get('guid', 'Not present').encode('utf-8')
     c.execute(sql)
     rows=c.fetchall()   
     if len(rows)>0:
         password=self._decode(result["password"], rows[0][5])
         data_dict={'name': rows[0][2], 'email': rows[0][4], 'fullname': rows[0][3],'password1':password,'password2':password}
         sql="UPDATE mailnotifikation SET registered='True' WHERE _guid='%s';" % request.params.get('guid', 'Not present').encode('utf-8')
 	    c.execute(sql)
         conn.commit()
         conn.close()
         self._save_new(context,data_dict)