I’m having trouble with my server’s email sending feature. It’s been using Gmail SMTP through Python’s smtplib for years, but now it’s giving me an error about username and password not being accepted.
After some digging, I found out it might be related to Gmail’s Less Secure Apps policy. Apparently, I need to set up an App Password. I’ve already enabled 2-step Authentication, but I’m stuck on generating the right App Password.
When I try to create one on my personal computer, it says it’s for that device only. But I need it for my server. Is there a way to generate an App Password that will work for my server? Can I do this from the Linux command line or somehow specify it’s for a different device?
Here’s a simplified version of my current Python code:
import smtplib
sender = '[email protected]'
receiver = '[email protected]'
password = 'my_old_password'
message = 'Hello, this is a test email.'
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, message)
Any help would be appreciated!
I’ve dealt with this exact headache before, Ryan. Here’s what worked for me:
Go to your Google Account settings and find the ‘Security’ tab. Look for ‘App passwords’ - it should be there if you’ve got 2FA set up. Choose ‘Select app’ and pick ‘Other (Custom name)’. Name it something like ‘ServerEmailScript’.
Google will generate a 16-character password. Copy that and replace your old password in your Python script. Don’t forget to update any environment variables or config files where you might store this.
One gotcha: make sure your server’s IP isn’t blocked by Google. I once spent hours debugging only to realize my server’s IP was flagged. A quick whitelist fixed it.
Also, consider using environment variables for sensitive info like passwords. It’s a good practice to keep them out of your code.
hey ryan, ive been there. i fixed mine by genrating an app pass as ‘other’ and naming it server.
just swap your old pass in the code and try again!
I encountered a similar issue when updating my server’s email configuration. In my case, I resolved it by opening my Google Account settings and navigating to the Security section. I then located the App Passwords feature and selected the option to create a custom app password. I named it to indicate its server use and replaced my old password with the newly generated 16-character string.
After implementing this change, my application resumed sending emails normally. I suggest double-checking your network configuration if you continue to experience issues, and always ensure that your credentials are stored securely.