diff --git a/SAS/TriggerEmailService/Server/lib/TriggerEmailService.py b/SAS/TriggerEmailService/Server/lib/TriggerEmailService.py index 9f3e122b7dd1b857f05e4b4c5914d5c9fc305504..905493db5778bb32110bc493aca2b6a1f2d97d9d 100644 --- a/SAS/TriggerEmailService/Server/lib/TriggerEmailService.py +++ b/SAS/TriggerEmailService/Server/lib/TriggerEmailService.py @@ -17,9 +17,10 @@ # # You should have received a copy of the GNU General Public License along # with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. -import dateutil +import smtplib import logging +from email.mime.text import MIMEText from datetime import timedelta, datetime from lofar.sas.otdb.OTDBBusListener import OTDBBusListener from lofar.common.util import waitForInterrupt @@ -29,9 +30,9 @@ from lofar.sas.TriggerEmailService.common.config import DEFAULT_TRIGGER_NOTIFICA from lofar.mom.momqueryservice.momqueryrpc import MoMQueryRPC from lxml import etree from StringIO import StringIO -from lofar.common.datetimeutils import parseDatetime from re import findall + logger = logging.getLogger(__name__) received_template_subject = """%(PROJECTNAME)s trigger %(TRIGGERID)s received""" @@ -101,8 +102,25 @@ kind regards, LOFAR Science Operations & Support [ sos@astron.nl ]""" -def email(mom_id, subject, body, attachment): - pass +def email(recipients, subject, body, attachment): + recipients.append("sos@astron.nl") + recipients.append("operator@astron.nl") + + sender = "Trigger Email Service <no-reply@astron.nl>" + COMMASPACE = ', ' + + msg = MIMEText(body) + msg["Subject"] = subject + msg["From"] = sender + msg["To"] = COMMASPACE.join(recipients) + + if attachment: + txt = MIMEText(attachment) + msg.attach(txt) + + s = smtplib.SMTP('localhost') + s.sendmail(sender, recipients, msg.as_string()) + s.quit() class OTDBTriggerListener(OTDBBusListener): @@ -137,8 +155,13 @@ class OTDBTriggerListener(OTDBBusListener): trigger_id = self.mom_rpc_client.get_trigger_id(mom_id) if trigger_id: - subject, body = self._fill_template(otdb_id, mom_id, trigger_id, template_subject, template_body) - email(mom_id, subject, body, None) + self._send_email(otdb_id, mom_id, trigger_id, template_subject, template_body) + + def _send_email(self, otdb_id, mom_id, trigger_id, template_subject, template_body): + subject, body = self._fill_template(otdb_id, mom_id, trigger_id, template_subject, template_body) + recipients = self._get_recipients(mom_id) + + email(recipients, subject, body, None) def _fill_template(self, otdb_id, mom_id, trigger_id, template_subject, template_body): project = self.mom_rpc_client.getProject(mom_id) @@ -153,6 +176,16 @@ class OTDBTriggerListener(OTDBBusListener): return subject, body + def _get_recipients(self, mom_id): + recipients = [] + + emails = self.mom_rpc_client.get_project_details(mom_id) + for k, v in emails.items: + recipients.append(v) + + return recipients + + class TriggerNotificationListener(AbstractBusListener): def __init__(self, momquery_rpc = MoMQueryRPC(), busname=DEFAULT_TRIGGER_NOTIFICATION_BUSNAME, subject=DEFAULT_TRIGGER_NOTIFICATION_SUBJECT, broker=None, **kwargs):