STARTTLS error prevents sending emails using custom SMTP in Anvil

What I’m trying to do: Use a custom subdomain (consult.animl.health, hosted by Namecheap) SMTP in Anvil to send emails via Google Workspace account

What I’ve tried and what’s not working: Extensively followed documentation from Anvil, Namecheap, Google on configuration settings and code snippets. Currently stuck at the error message “anvil.email.SendFailure 530-5.7.0 Must issue a STARTTLS command first” as shown in attached screenshot. Pointers in the right direction and ideas to pursue will be much appreciated, thank you!

Code Sample:

 anvil.email.send(
        to="patient@gmail.com",
        subject="Consultation initiated",
        html="Files Successfully Uploaded for patient",
        from_name="ANI.ML Health",
        from_address="we.care@consult.animl.health")

Clone link:
share a copy of your app

Hi @christopher.pinard

My understanding is that after previous discussions about fixing the SMTP hostname, this was working. Did something change since then, or have you always received this error?

1 Like

Hi Ian @daviesian, after we switched from “smtp dot google dot com” (oops) to “smtp dot gmail dot com” (yay) we got the “Test settings” button to confirm “Connection succeeded” as shown in the attached screenshot but we’ve been stuck on the “must issue STARTTLS command” error message since then and not gotten the email to be sent via Google SMTP. Thank you for looking into this with us, and we hope that the Anvil user community will learn and benefit from our scenario!

Hi @christopher.pinard,

Thanks for confirming - that helps! What happens if you use smtp-relay.gmail.com instead of smtp.gmail.com?

Hi @daviesian, seems like we’re getting warmer, this error (screenshot) comes up, we haven’t configured Gmail relay yet but would be glad to if you say it’s the right direction, thank you!

Yes, I think this is the way to go. See these Google Docs. It’s definitely how we do it on some of our internal apps (which are, of course, all built in Anvil) :slightly_smiling_face:

Getting this kind of thing to work with Google’s ever-changing rules is always a nightmare, but hopefully this will work!

1 Like

Giving it a try now, will update with results here, thank you superstar @daviesian!

Hello! I made the smtp-relay changes in Google Workspace Admin (screenshot) following Google’s instructions (https: //support dot google dot com/a/answer/6140680#invalidcred and the Google link you shared above), our Namecheap MX records refer to “smtp dot google dot com” exactly on Google’s instructions (screenshot, did they change it to “smtp.gmail.com” and forgot to update their documentation?), and we get the following error message (screenshot):
“anvil.email.SendFailure: 550-5.7.1 Invalid credentials for relay [18.135.76.16]. The IP address you’ve
550-5.7.1 registered in your Workspace SMTP Relay service doesn’t match the
550-5.7.1 domain of the account this email is being sent from. If you are
550-5.7.1 trying to relay mail from a domain that isn’t registered under your
550-5.7.1 Workspace account or has empty envelope-from, you must configure your
550-5.7.1 mail server either to use SMTP AUTH to identify the sending domain or
550-5.7.1 to present one of your domain names in the HELO or EHLO command.”
Hopefully our scenario and your insights benefit the Anvil community. Thank you!



I’m afraid at this point we’re well outside the expertise of anyone in the office at this end. I think the next thing to try will be to get it working with a standard SMTP client (something like Outlook, Thunderbird, etc) in order to rule out anything to do with Anvil. If you can get that working, then we can be more confident that it’s possible to do with Anvil!

I suppose there’s one obvious question: Do you actually need to send through your Google Workspace account? Emails sent directly through Anvil can be fully signed by your custom domain, so should be indistinguishable.

1 Like

Has this got anything to do with the post I made back in April (but didn’t get any response to)?

Perhaps it involves Google Workspace’s requirement for OAuth. Less Secure Apps was hard for us to set up so we put that on pause. For now, we’re using Anvil’s SMTP to send emails which is working fine. Thank you Paul, Ian, and the broader Anvil Community for sharing your insights and expertise!

Hello Ian, we’re following your suggestion of using Anvil’s SMTP to send emails which is working fine. Thank you and the broader Anvil Community for sharing your insights and expertise!