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’