diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index f6292d25..0c796e9e 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -32,6 +32,25 @@ #TODO #Document input/output values +#Instructions for usage: +# 1) In the registry, create the directory /Servers/Telecomm +# 2) Create a key named 'smsUsers' with value +# [("USERNAME1","TENDIGITPHONENUMBER@txt.att.net"),... +# ,("USERNAMEM","TENDIGITPHONENUMBER@vtext.com")] +# Note that the domain name depends on the user's +# mobile phone provider. "USERNAME" must be ALL caps. +# 3) Create a key named 'domain' with a value that +# matches the domain name of the email address +# from which you wish to send messages. +# e.g. domain = "physics.someschool.edu" +# 4) Create a key named 'smtpServer' whose value is +# the smtp server you wish to use. +# 5) (Optional) Create a key named 'password' +# corresponding to the login credentials of +# the account you plan to send messages from. +# Note that some smtp servers require you to +# authenticate before you can send messages +# to email addresses outside of its domain. from labrad import types as T, util from labrad.server import LabradServer, setting, Signal @@ -47,8 +66,9 @@ SMS_KEY = 'smsUsers' DOMAIN_KEY = 'domain' SERVER_KEY = 'smtpServer' +PASSWORD_KEY = 'password' -def textMessage(recipients, subject, msg, domain, server, username='LabRAD',attempts=2): +def textMessage(recipients, subject, msg, domain, server, username='LabRAD', password=None, attempts=2): """Send a text message to one or more recipients INPUTS: @@ -61,9 +81,9 @@ def textMessage(recipients, subject, msg, domain, server, username='LabRAD',atte """ if not isinstance(recipients,list): recipients = [recipients] - return email(recipients, subject, msg, domain, server, username, attempts=attempts) + return email(recipients, subject, msg, domain, server, username, password, attempts=attempts) -def email(toAddrs, subject, msg, domain, server, username='LabRAD', attempts=2, noisy=False): +def email(toAddrs, subject, msg, domain, server, username='LabRAD', password=None, attempts=2, noisy=False): """Send an email to one or more recipients INPUTS: @@ -88,6 +108,9 @@ def email(toAddrs, subject, msg, domain, server, username='LabRAD', attempts=2, message = header+msg #Get connection to smtp server and send message server = smtplib.SMTP(server) + if password is not None: + server.starttls() + server.login(fromAddr, password) result = server.sendmail(fromAddr, toAddrs, message) #Update the toAddrs list to include only recipients for which mail sending failed toAddrs = result.keys() @@ -127,6 +150,7 @@ def _refreshConnectionData(self): reg = cxn.registry p = reg.packet() p.cd(REGISTRY_PATH) + p.dir(key='regcontent') p.get(SMS_KEY, key='userlist') p.get(DOMAIN_KEY, key='domain') p.get(SERVER_KEY, key='server') @@ -134,20 +158,28 @@ def _refreshConnectionData(self): self.smsUsers=dict(resp['userlist']) self.domain = resp['domain'] self.smtpServer = resp['server'] + keys = resp['regcontent'][1] + if 'password' in keys: + p.get(PASSWORD_KEY, key='password') + resp = yield p.send() + self.password = resp['password'] + else: + self.password = None print 'Refresh complete.' @setting(10, toAddrs=['s{email address of recipient}','*s{array of recipient email addresses}'], subject='s', msg='s', username='s', returns='b{success}*s{failures}') def send_mail(self, c, toAddrs, subject, msg, username='LabRAD'): - success, failures = email(toAddrs, subject, msg, self.domain, self.smtpServer, username) + success, failures = email(toAddrs, subject, msg, self.domain, self.smtpServer, username, self.password) return (success, failures) - @setting(11, subject='s', msg='s', recipients=['*s','s'], returns='b{success}*s{failures}') - def send_sms(self, c, subject, msg, recipients): + @setting(11, subject='s', msg='s', recipients=['*s','s'], username='s', returns='b{success}*s{failures}') + def send_sms(self, c, subject, msg, recipients, username='LabRAD'): if not isinstance(recipients,list): recipients = [recipients] recipients = [self.smsUsers[name.upper()] for name in recipients if name.upper() in self.smsUsers] - success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer) + success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer, + username, self.password) return (success, failures)