Notifications Script Error - OSError: [Errno 99] Cannot assign requested address

3740
6
04-23-2020 02:26 PM
RonnieRagains
New Contributor

Consistently getting this error when trying to modify the example "Deploy Automatic Notifications" notebook. I am using gmail and have enabled "allow less secure apps" in the gmail settings. I can successfully send notifications from local python (2.7) using the same parameters but keep getting an error when using AGOL Notebook. Any suggestions?

def send_email_smtp(recipients, message,
 subject="Message from your Notebook"):
 """Sends the `message` string to all of the emails in the 
 `recipients` list using the configured SMTP email server. 
 """
 try:
 # Set up server and credential variables
 
 smtp_server_url = "smtp.gmail.com"
 smtp_server_port = 587
 sender = "EmailAddress@gmail.com"
 username = "EmailAddress@gmail.com"
 password = secrets["smtp_email_password"]
 
 server = smtplib.SMTP(smtp_server_url, smtp_server_port)
 server.ehlo()
 server.starttls() # Needed if TLS is required w/ SMTP server
 server.login(username, password)
 except Exception as e:
 log.warning("Error setting up SMTP server, couldn't send " +
 f"message to {recipients}")
 raise e

 # For each recipient, construct the message and attempt to send
 did_succeed = True
 for recipient in recipients:
 try:
 message_body = '\r\n'.join(['To: {}'.format(recipient),
 'From: {}'.format(sender),
 'Subject: {}'.format(subject),
 '',
 '{}'.format(message)])
 message_body = message_body.encode("utf-8")
 server.sendmail(sender, [recipient], message_body)
 print(f"SMTP server returned success for sending email "\
 f"to {recipient}")
 except Exception as e:
 log.warning(f"Failed sending message to {recipient}")
 log.warning(e)
 did_succeed = False
 
 # Cleanup and return
 server.quit()
 return did_succeed

------------------------------------------------------------------------------------------------------

send_email_smtp(recipients = ['DifferentEmail@gmail.com'],
 message = "Hello World!")

------------------------------------------------------------------------------------------------------

Full error:

Error setting up SMTP server, couldn't send message to ['DifferentEmail@gmail.com']
---------------------------------------------------------------------------OSError                                   Traceback (most recent call last)<ipython-input-49-6f063c68c4ea> in <module>      1 send_email_smtp(recipients = ['DifferentEmail@gmail.com'],----> 2                 message = "Hello World!")<ipython-input-48-2a35190fcbcd> in send_email_smtp(recipients, message, subject)     23         log.warning("Error setting up SMTP server, couldn't send " +     24                     f"message to {recipients}")---> 25         raise e     26      27     # For each recipient, construct the message and attempt to send<ipython-input-48-2a35190fcbcd> in send_email_smtp(recipients, message, subject)     16         password = secrets["smtp_email_password"]     17 ---> 18         server = smtplib.SMTP(smtp_server_url, smtp_server_port)     19         server.ehlo()     20         server.starttls() # Needed if TLS is required w/ SMTP server/opt/conda/lib/python3.6/smtplib.py in __init__(self, host, port, local_hostname, timeout, source_address)    249     250         if host:--> 251             (code, msg) = self.connect(host, port)    252             if code != 220:    253                 self.close()/opt/conda/lib/python3.6/smtplib.py in connect(self, host, port, source_address)    334         if self.debuglevel > 0:    335             self._print_debug('connect:', (host, port))--> 336         self.sock = self._get_socket(host, port, self.timeout)    337         self.file = None    338         (code, msg) = self.getreply()/opt/conda/lib/python3.6/smtplib.py in _get_socket(self, host, port, timeout)    305             self._print_debug('connect: to', (host, port), self.source_address)    306         return socket.create_connection((host, port), timeout,--> 307                                         self.source_address)    308     309     def connect(self, host='localhost', port=0, source_address=None):/opt/conda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)    722     723     if err is not None:--> 724         raise err    725     else:    726         raise error("getaddrinfo returns an empty list")/opt/conda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)    711             if source_address:    712                 sock.bind(source_address)--> 713             sock.connect(sa)    714             # Break explicitly a reference cycle    715             err = NoneOSError: [Errno 99] Cannot assign requested address
0 Kudos
6 Replies
by Anonymous User
Not applicable

Hi Ronnie,

Sorry for this - currently we do not allow ArcGIS Online Notebooks to connect to external servers, and so we don't allow connections to email servers. This sample was incorrectly included with Online and will soon be modified to reflect that. To programmatically send out emails we recommend the use of a service such as Email by Zapier, or Twilio SendGrid. The use of Zapier to post to social media is described in the same sample notebook under the Zapier heading, and you may use that as a starting point to send out emails via POST, GET, or PUT requests.

Here's the link to Email by Zapier's documentation. 

Here's the link to using webhooks to trigger Zaps in Zapier.

Let me know if you have anymore questions.

Josh

0 Kudos
PatRiley
New Contributor

I just wasted several hours trying to debug a sample that cannot work.  You should update the description to say that you cannot connect to an external server from AGOL.  If a connection to an external server is not allowed, why would a POST, GET or PUT from a notebook on AGOL to Zapier or Twilio work?

JulieK
by
Occasional Contributor

Hey Josh,

Any update on this? Does it still not allow ArcGIS Online Notebooks to connect to external servers? You are suggesting Zapier, my question is can I use Power automate to send an email? Is that feasible?

Thanks,

Julie

0 Kudos
Jordi-Monk-Developer
New Contributor III

Encountering this same error on July 2022. The funny thing is that it worked since I created the script, 4 or 5 months ago, until last Friday (July 15th, 2022). Fails both using smtp-mail.outlook.com and smtp.gmail.com. The same script works with no issues when executed locally.

pmccord
New Contributor III

I am having the exact same issue as you. I was sending emails with information from feature layers using smtp.gmail.com and it had worked since January 2022. However, it failed on its scheduled run on July 17th and I have not been able to get it to run since. I was also able to successfully run the script locally. 

I hope someone from Esri can weigh in on this. It doesn't make any sense that it was working for the past six months and still works locally, but it suddenly stopped working two weeks ago. This is especially confusing because the above post from @Anonymous User says that connections to external servers (including email servers) is not supported, but I had been able to connect to an smtp instance up until two weeks ago.

0 Kudos
jtmouw_NCDOT
New Contributor III

Any solution on this? Did the ability to connect to external servers just get turned off or is there a workaround?