'Why is Python smtplib not working for mail?

I have tested this for a game that I am making. For some reason it worked a lot but I took a break from it and it has stopped working. I looked around to see if it was a problem with my code or if my code was "too old" or something like that but even after finding some other snippets it still doesn't work. Right now this is the code I pulled off the internet (that someone else said worked):

import smtplib
# SMTP_SSL Example
gmail_user = "[email protected]"
gmail_pwd = "APassword"
server_ssl = smtplib.SMTP_SSL("m.google.com", 587)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(gmail_user, gmail_pwd)  
# ssl server doesn't support or need tls, so don't call server_ssl.starttls() 
server_ssl.sendmail(FROM, TO, message)
#server_ssl.quit()
server_ssl.close()
print('successfully sent the mail')

When I run it it dies at line 5, server_ssl = smtplib.SMTP_SSL("m.google.com", 587). I have also tested it with smtplib.SMTP_SSL("smtp.gmail.com", 465) instead, and it dies in the same place. The errors are:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    server_ssl = smtplib.SMTP_SSL("m.google.com", 587)
  File "/usr/lib/python3.8/smtplib.py", line 1043, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/usr/lib/python3.8/smtplib.py", line 255, 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 1049, in _get_socket
    new_socket = socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

I've searched around for others with the same problem but all their errors are -2 instead. I really don't know if it is a problem with my computer, internet, code or what. It has been a couple weeks since it has happened so I don't think the temporary part is actually true or if the google servers have moved. If someone knows where the servers have moved that would also help. One of the most important things I want to know is if it is working for anyone else. Thanks for the help.



Solution 1:[1]

Gmail on port 587 does not permit connecting over SSL. You should generally use SMTP, not SMTP_SSL, when connecting to port 587. The SSL-enabled port is usually 465 (but as you note, I don't think Gmail supports that).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tripleee