I'm trying to send an OTP via email (which I save in a DB as well) and even though I believe my config is right because it logs in, it doesn't send messages (it did the first 2 times and then stopped):
message = """Subject: Your OTP
Hi, your your otp is {otp}"""
from_address = "REDACTED"
password = "REDACTED"
context = ssl.create_default_context()
print('xyz')
msg = message.format(otp=otp)
with smtplib.SMTP_SSL(host="smtppro.zoho.eu", port = 465, context=context) as server:
print('xyz')
server.login(from_address, password)
print('logs')
server.ehlo('127.0.0.1') ## i tried the ehlo() as well and without it all together
print(email) ## prints this and then gets stuck
server.sendmail(
from_address,
email,
msg
)
print('sent')
server.quit() ##i may had forgotten to have that the first 2 times
print('quit')
return "OTP sent to your email"
As you can see in the code block I had forgotten the quit()
thing the first 2 times and I thought maybe there is something wrong with the session. I restarted the server 2 times in hopes to reset the connection.
Mind the comments I have made in the code block.
For the password I used my account password as well as an APP password (just for use in an app), and as I said there wasnt a problem with the log in.
After sometime (2-3 minutes) I get this in my terminal:
File "/home/blank/.local/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/blank/scripts/test/emails.py", line 56, in sendmail
server.sendmail(
File "/usr/lib/python3.11/smtplib.py", line 902, in sendmail
(code, resp) = self.data(msg)
^^^^^^^^^^^^^^
File "/usr/lib/python3.11/smtplib.py", line 580, in data
(code, msg) = self.getreply()
^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/smtplib.py", line 405, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed