My Ubuntu based webserver needs to occasionally send emails. My python code is:
withsmtplib.SMTP('smtp-relay.gmail.com', 587, 'mydomain.com') as s:
s.sendmail(fromaddr, toaddr, msg.as_string())
s.quit()
I have
- a Google workspace account
- am using IP authentication (not SMTP auth)
- my staging and production servers added as trusted IPs (staging is local, production is cloud)
This setup had been working fine for 6+ months.
Two days ago I upgraded Ubuntu from 20LTS to 22LTS and python 3.8 to 3.10. Now the email is working fine on the staging server, but production keeps throwing:
Invalid credentials for relay [...]. The IP\n5.7.1 address you've registered in your G Suite SMTP Relay
service doesn't\n5.7.7 match domain of the account this email is being sent from. If you are\n5.7.1 trying to
relay mail from a domain that isn't registered under your G\n5.7.1 Suite account or has empty envelope-from,
you must configure your\n5.7.1 mail server either to use SMTP AUTH to identify the sending domain or\n5.7.1 to
present one of your domain names in the HELO or EHLO command. For\n5.7.1 more information, please visit
https://support.google.com/a/answer/6140680#invalidcred ...
Any suggestions?
Edit 1: I fired up my old ubuntu server in the cloud. I added its new IP as trusted on Google. The email worked fine. I can think of only three possibilities
- Google somehow recognizes and trusts requests coming from the old device (even though it now has a different IP)
- Linode is somehow not sending the correct IP address from my new server
- Something broke during the Ubuntu upgrade
I find each of the 3 possibilities quite bizarre and unbelievable at this point, but I'll keep researching.
PS: Three factoids that may/may not be relevant:
- I upgraded the staging server in place. For production I spun a new instance, made sure everything else was working fine (except email) and then transferred IP from the existing instance to new
- When I log in to my google admin account to edit trusted IP list, my IP is the same as staging server. I don't think I have the same option for production, since it's an Ubuntu server I manage through SSH
- I found some comments online (none in official documentation), that the reverse DNS needs to be setup before Google would relay anything. I set up the entry about 20 hours ago for production, but still getting the same error. And for my staging server, I don't have rDNS and it still sends emails (it's accessible from the internet, but I don't have a static IP)
PPS:
- The sender email is someuser@mydomain.com (not @gmail.com)
- The production server is hosted on linode.com
- This post comes close to discussing a similar situation, but that is focused more on signing in. My setup uses IP authentication, not SMTP auth. Plus it was working fine until Friday (8/12)