Sending emails form juptyer lite

I’m trying to get jupyter lite widget to send emails but run into a problem with smtp lib. With TLS it say “OSError: [Errno 23] Host is unreachable” with SSL “AttributeError: module ‘smtplib’ has no attribute ‘SMTP_SSL’”

import smtplib
from email.mime.text import MIMEText

def send_email(subject, body, sender, recipients, password):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ', '.join(recipients)
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp_server:
       smtp_server.login(sender, password)
       smtp_server.sendmail(sender, recipients, msg.as_string())
    print("Message sent!")
    
send_email("Test","Body","jmechtel@gmail.com","jmechtel@gmail.com","XXX")

OSError Traceback (most recent call last)
Cell In[9], line 14
11 smtp_server.sendmail(sender, recipients, msg.as_string())
12 print(“Message sent!”)
—> 14 send_email(“Test",“Body”,"jmechtel@gmail.com,"jmechtel@gmail.com",“XXX”)

Cell In[9], line 9, in send_email(subject, body, sender, recipients, password)
7 msg[‘From’] = sender
8 msg[‘To’] = ', '.join(recipients)
----> 9 with smtplib.SMTP(‘smtp.gmail.com’, 587) as smtp_server:
10 smtp_server.login(sender, password)
11 smtp_server.sendmail(sender, recipients, msg.as_string())

File smtplib.py:255, in init(self, host, port, local_hostname, timeout, source_address)

File smtplib.py:341, in connect(self, host, port, source_address)

File smtplib.py:312, in _get_socket(self, host, port, timeout)

File socket.py:851, in create_connection(address, timeout, source_address, all_errors)

File socket.py:836, in create_connection(address, timeout, source_address, all_errors)

OSError: [Errno 23] Host is unreachable

The other route I tried:


import smtplib
from email.mime.text import MIMEText

def send_email(subject, body, sender, recipients, password):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ', '.join(recipients)
    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
       smtp_server.login(sender, password)
       smtp_server.sendmail(sender, recipients, msg.as_string())
    print("Message sent!")
    
send_email("Test","Body","jmechtel@gmail.com","jmechtel@gmail.com","XXX")

AttributeError Traceback (most recent call last)
Cell In[10], line 14
11 smtp_server.sendmail(sender, recipients, msg.as_string())
12 print(“Message sent!”)
—> 14 send_email(“Test",“Body”,"jmechtel@gmail.com,"jmechtel@gmail.com",“XXX”)

Cell In[10], line 9, in send_email(subject, body, sender, recipients, password)
7 msg[‘From’] = sender
8 msg[‘To’] = ', '.join(recipients)
----> 9 with smtplib.SMTP_SSL(‘smtp.gmail.com’, 465) as smtp_server:
10 smtp_server.login(sender, password)
11 smtp_server.sendmail(sender, recipients, msg.as_string())

AttributeError: module ‘smtplib’ has no attribute ‘SMTP_SSL’

Hi @Jan_Mechtel,
As far as I know, calling any external host (including gmail SMTP server in your case) is only allowed in self-hosted Grist.
If you are using getgrist.com, you can build a workaround using an external hosting for your script, such as Google Colab.

Thanks for taking the time to reply.

As far as I know, calling any external host (including gmail SMTP server in your case) is only allowed in self-hosted Grist.
I do self host grist and there it’s the same errors for now.

Do you know where I can find more information on this?

Also the requests seems to work ok even on gristonline:

import requests

def check_internet():
    try:
        requests.get('http://www.google.com', timeout=3)
        return True
    except requests.ConnectionError:
        return False

if check_internet():
    print("Internet access is available.")
else:
    print("Internet access")

The above works fine on gristonline.

Would be great to get more information on this.