Forums

Flask smpt Server Disconnected

Hi! I was getting an AUTH error, but I took y'alls advice and used "smtp.gmail.com", 465 for my mail server. It is now giving me this error:

File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Here is my flask code:

user = "myemail@mydomain.com"
password = "mypassword"
mailserver=smtplib.SMTP("smtp.gmail.com", 465)
mailserver.ehlo()
mailserver.starttls()
mailserver.login(user,password)
message = "Name:{}\nEmail: {}\nAddress: {}\nSize: {}\nNote: {}\nOrder: {}".format(name, email, address, size, note, order)
mailserver.sendmail(user,"email@mydomain.com",message)

I have a developer account, so I'm not sure why this isn't working. Any help would be greatly appreciated. Thanks!

There is some discussion in this SO question that covers how you can debug it and possibly what is happening.

Ok, I read the post, and tried using port 587. That didn't work. I also tried using smtplib.SMTP_SSL() instead of just smtplib.SMTP. That got rid of my previous error, but then I get:

File "/usr/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:1108)

Also, when I keep the same port but change the server to SSL, I get:

File "/home/ConnorCollins/mysite2/flask_app.py", line 37, in sendOrder
mailserver.starttls()
File "/usr/lib/python3.8/smtplib.py", line 755, in starttls
raise SMTPNotSupportedError(
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.

Do these errors have something to do with the fact that I am using myemail@mydomain.com rather than myemail@gmail.com or do I have to add some extra changes or install something in order to get SSL to work? Thank you!

I don't think it's the underlying cause of the exception you're seeing, but just to make sure: is yourdomain.com set up to use Gmail for its email hosting? smtp.gmail.com will only work for domains for which Google are running the email infrastructure (eg. using G Suite).

Yes, I am using G Suite, so it should work.

Could you run your original code again with port 587 and show us the full traceback?

Sure!

Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ConnorCollins/mysite2/flask_app.py", line 35, in sendOrder
mailserver=smtplib.SMTP_SSL("smtp.gmail.com", 587)
File "/usr/lib/python3.8/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 1042, in _get_socket
new_socket = self.context.wrap_socket(new_socket,
File "/usr/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/usr/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:1108)

Sorry, when @fjl said "your original code" he meant the code with smtplib.SMTP, not with smtplib.SMTP_SSL.

No Problemo. So I changed it back and realized that it didn't accept the password. I checked that the password and email matched by signing into the account again and I'm positive that its the correct password and correct email. Does this have something to do with Google not allowing me to login?

Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ConnorCollins/mysite2/flask_app.py", line 38, in sendOrder
mailserver.login(user,password)
File "/usr/lib/python3.8/smtplib.py", line 734, in login
raise last_exception
File "/usr/lib/python3.8/smtplib.py", line 723, in login
(code, resp) = self.auth(
File "/usr/lib/python3.8/smtplib.py", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  
https://support.google.com/mail/?p=BadCredentials z68sm22647758qke.113 - gsmtp')

Also, the email I'm using is listed as a user account on the google domains website. I can change it to admin if that would make it work. Would that change anything?

edit: Nevermind, I changed it to admin and it didn't work. Is there something I need to change in the email settings?

Is it that the username is supposed to be different from the actual email? Should the username be myemail rather than myemail@mydomain.com?

Hi! So sorry about posting a couple times in a row, but I'm on a deadline. So I've tried changing my password, using http://www.google.com/accounts/DisplayUnlockCaptcha, and changing my account to admin. I've done all these things a couple time without any luck. How and what do I need to change in my gmail settings to fix the error? Thank you and sorry again about the multiple posts!!

edit: Never mind, I got it to work. I turned on accessibility for less secure apps, which was hard to figure out how to do because I was using my own domain. Thanks for your help!

Glad to see that you made it work. So, it was just the configuration thing on the google side, right?

Yes. It took me a while to figure out how to allow less secure apps, because the settings are structured differently for domain emails. All I had to do was 1. Open gmail. 2. Click settings 3. Click Manage This Organization. 4. Click Security. 5. Click Less Secure Apps. 6. Turn on accessibility for less secure apps.