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