smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')
Actually, when I tried executing the same statements on python console, I came to know that the password was incorrect and it was due to different character encoding.
For all other users, refrain yourself from copy paste
For completeness answering this question, I have encountered exactly the same error multiple times - each time for a different reason. This error message has been haunting me and I kind of mutated into a self-proclaimed expert on this very error message. Here is a list of all the issues I faced with it and how I solved it.
Needed to enable "less secure app" in your Google Admin (https://admin.google.com/ac/security/lsa)
Even if the above is enabled, the service might fail if you have a two-factor authentication setup in your email. The solution is to create an app specific password (https://myaccount.google.com/u/5/apppasswords) that must be used as the password for smtplib in Python instead.
If you do this for the first time with an unknown IP. There may be a secret Captcha priming the authentication. You may see then actually a 534 error code instead of a 535 but I can't guarantee that. You will need to click on a link first to clear the Captcha (https://accounts.google.com/DisplayUnlockCaptcha). You might need to wait 10 min before it actually registers it. So grab a coffee.
Finally, you will get the same error message, if you have not opened the correct port. I had port 587 not opened in firewalld. I had smtp and smtps as services enabled, but these only cover ports 25 and 465 by default. So, if you use 587, make sure it is open, e.g. for firewalld, sudo firewall-cmd --permanent --add-port=587/tcp and restart your firewalld service.
Lastly, this mail still fail if you have not setup your SSL correctly at your server. At some point I had the error message and upon further investigation in some logs I saw it failed to do a handshake.
Update lastly: I had the ehlo() accidently at the wrong place in my code that suddently started to throw the 535 Authentication error, that is:
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls() session.login(sender_address, sender_pass)
session.ehlo("yourdomain.com") # <-- Make sure this comes after starttls() and not before
Google has removed this feature as of May 30th, 2022.
To help keep your account secure, from May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.