Это сильно отличается от вашего исходного кода, но этот метод, который я нашел, работает для меня. Используйте это или нет, но этот метод довольно надежен.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import ssl
def send_email():
subject = 'You have to see this, it is about your university'
body = 'look, these are yours pendenting homeworks: ' + name_teacher2 + ' and these are yours expired homeworks: '+ name_teacher
sender = 'bot_piloto.test@hotmail.com'
receiver = 'bot_piloto.test@hotmail.com'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
body = MIMEText(body, "plain")
msg.attach(body)
#Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.hotmail.com", 587, context=context) as server:
server.login(sender, "*******")
server.sendmail(
sender,
receiver,
msg.as_string()
)