SMTP Email not working

What I’m trying to do:
I have some reports for an in house app I’ve built that are sent via a background task via our SMTP server. Our SMTP connection is successful, but I’m getting the following error when my background tasks run or I run the code as a server function:

anvil.server.InternalError: Internal server error: 8e006d6bf937
at /downlink-sources/latest/anvil/_threaded_server.py, line 518
called from /downlink-sources/latest/anvil/server.py, line 68
called from /downlink-sources/latest/anvil/email.py, line 44
called from Automation, line 105
called from /downlink-sources/latest/anvil/_threaded_server.py, line 191

Any insight here is welcome! If it’s helpful, we use sendgrid for our SMTP server.

Probably should have tested this before posting, but it doesn’t work when I have an attachment. If I just send an email with html or a body it sends successfully.

So, I looked up the internal error, and I think what’s happened here is that you’ve passed something that isn’t a Media object as an attachment. (Honestly, given the error I saw, I am quite curious about what it is that you did pass in there!)

Something like this will work:

blob = anvil.BlobMedia("text/plain", b"Hello, World", name="hello.txt")
anvil.email.send(
    to="recipient@example.com",
    subject="Here's your file",
    text="Please find the attachment.",
    attachments=[blob]
)

We’ll update Anvil to produce a more helpful message in this case in future!