Emails have stopped sending and no errors found

What I’m trying to do:
Run a background task, which sends emails to a list of users - was working until Saturday afternoon

My app does have a custom URL, but I do not have a custom email domain setup. This hasn’t been a problem before, and has been working for months.

I use the same email code as found in the other posts, like https://anvil.works/forum/t/issue-sending-emails/6184

What I’ve tried and what’s not working:

  • Adding try/except around the send_email to try and capture any errors.
  • Tried setting a local variable to be the result of the send_email() function, then printing that variable (all seems fine)
  • checking the background task logs just says it has sent the email.
  • edited the scripts to dump more information into the logs, still get messages that emails have sent.
  • Tried republishing the app, get the same “all good” messages when rerunning the background tasks.
  • Checking Junk/Spam on the recipient email address.

I’ve read the forum, and can see that previously there was an issue with the email provider (see here)
Wondering if that has happened again?

I see that the advice is to setup your own custom SMTP after registering with a email_provider, but as this is a live app, I would like to leave that as a last resort.

Has anyone else witnessed odd email behaviour since Saturday?

Cheers

We have found that emails can get by spam filters and not show up in the spam folder. It’s a really hard thing to debug but catching it on the spam folder is the first thing.

You could also have a look at dmarc and dkim records (google it for your domain). I am definitely not an expert but we managed to bumble our way through it.

Note this happened well before Saturday

Thanks very much for this. I’ll do a bit of googling as you suggests.
I’ve just tested sending to another personal email address, that I haven’t used before in the app, and the email came through. So I think you’re right about the emails being identified as spam.
I’ll update this if I get anywhere. I guess I’ll have to setup mx records in my domain or something…that is all on the outer edges of my technical experience :smiley:

We had apps sending emails to addresses that didn’t exist yet or were configured to not receive external emails. Anvil got the bounced reply, tagged these addresses as not existing and stopped sending them emails.

We contacted the support, asked to un-blacklist those addresses and everything restarted working.

2 Likes

Thanks Stefano,

I’m hoping someone in support will take a look to see if there are a load of blocked email addresses.
i’ve checked at the recipient end, and there are no senders being blocked. I also went into old spam folder and marked older emails as NOT SPAM.
I’ve tried checking dmarc settings in the recipient email domain (office365), but there doesn’t seem to be anything obvious.
I also looked at the mx records on the domain that is being used to send the emails, and that seems ok.
Will check dkim records next.

Cheers

My problem was clearly caused by the addresses not being ready the first time (or any time) Anvil tried to send an email to them and received a bounce.

If you have no reason to think that your addresses bounced at any point in time, than this shouldn’t be a problem for you.

If that was the problem, then the emails are not sent at all.

Try to make an app that sends one email to 10 email addresses, including some that you know will work and some that will not, and try to trace that email on all email addresses.

Thanks again.
There was one fake email address in the list of recipients, so I’ve changed this out now.

I’ve sent the email test to two of my personal email addresses.
Email arrives to one (not used before Saturday), and not the other (used before Saturday).

@Anvil - is all good with your email provider etc?
This is probably local to my app.

Cheers

Possible interesting update.
If I setup a button, and call the function that the background_task is running then I receive an email to both personal accounts… I’ll dig a bit deeper and update you guys. It is probably some config issue at my end and not an email_provider issue at all.

Try to send the same email to multiple recipients, so you compare apples to apples.

Apples vs Apples comparison result:

Run as background task to two different personal email addresses. 1 arrives, 1 doesn’t.
Run as an anvil.server.callable function to the same two personal email addresses, both arrive.

If you are inclined, please see if you can spot any errors in my code, which I swear was working on Saturday and I’ve not changed it since…(Note: I’ve obfuscated any personal information in the code below)

I have an send_monthly_reminder() function like this:

# send monthly emails
@anvil.server.background_task
@anvil.server.callable
def send_monthly_reminder():
  """
  Monthly Email reminder: 
  """

  announcement = "Dear Participant/Family,<br>\
                  <br>\
                  Just a quick reminder to blah blah blah"
  
  all_emails=["my.personal_address1@mydomain.co.uk",'my_personal_address2@gmail.com']
  send_emails(email_list=all_emails, html=announcement,subject="my subject goes here")

Then send_emails() looks like this: (pretty much just copied from all the other posts)

# send all the emails in one go
def send_emails(email_list=[], html='', subject=""):
    for email in email_list:
        send_email(email, html, subject)

Then send_email() looks like this:

# how to send an email
@anvil.server.callable
def send_email(email, html, subject=""):
    """
    Overrode the send 10 times due to timeout logic, but kept the structure.
    """
    print(f'LOGGING email is set to {email}')
    N = 1
    not_sent = True  
    count = 1 
    while not_sent and count <= N :
      try:
        #first_name = email.split('@')[0].split('.')[0].capitalize()
        anvil.email.send(to=email,
                  from_name="no-reply@myurl.com",
                  subject=subject if subject else "Reminder",
                  html=html)
        print('Sent email to {}'.format(email))
        time.sleep(0.1)
        not_sent = False
      except TimeoutError:
        print('Timeout: Failed to send an email to: {} ({}/{})'.format(email, count, N))
        count += 1
       
    if not_sent:
      print('Skipped sending email to: {}'.format(email))

So the above works when initiated by a button click, but only sends to one email address (gmail) when running as a background task.
I’ve checked for typos in the email address that isn’t working, it is fine.

Thanks again for any help provided.

I would make apples more apples and get rid of as many variables as possible.

I would start with a quick test on a server console with this one liner, which sends both emails in one shot:

import anvil.email
anvil.email.send(to=['good@email.com','broken@email.com'], subject='hello', html='world')

If one of the email is not received, then I would check with Anvil support and see if there is anything wrong on their side, like an email address being blacklisted.

If both emails reached destination, then we know that there is nothing wrong with the email addresses and it’s time to dig deeper.

Your code is not long, but it does introduce a few possible reasons for failure, and it’s difficult to identify what’s wrong without testing each permutation of the variables in isolation.

At this point I would start introducing more variables: server callables, background tasks, bursts of emails sent quickly one after the other, bursts of emails sent quickly with failures, timeout management, switch the order in the list of addresses, etc.

1 Like

Sorry for slow response. I am still working on this intermittently.
I get a permission error if I try and run it from the server console
I set up a background task in my Test App and it works fine, but the sending address is something like no-reply@test.myapp.com

When I try and send the same email from my live app, with a sending address of somthing like no-reply@myapp.com, then only the gmail address receives it.

that says to me that my O365 email address is blocking the sender, rather than this being an issue with Anvil or my code.
When I look in O365 Admin center I can’t find anything where it is blocked or on a list. I shall continue to investigate and feedback here, in case others have the same issue.
Thanks again for your help.

What error are you getting?

I still don’t see apples to apples in your description.

Have you tried to send one email to two addresses, instead of two emails, one per address?

OK - this is now resolved, and it was nothing to do with Anvil at all.
My perception of the problem was actually wrong. I WAS receiving the emails to my O365 account, but Outlook for Mac was not showing them.
They were coming through as “Unverified” sender and were not showing up in my inbox (focused or Other) nor in my junk/spam folder.

I noticed this when I searched for the sending address in my inbox, and they appeared…stating that they were indeed in my inbox.
I use Outlook for Mac version 16.74.1.

How I fixed it…

  • Updated all software on my Mac
  • Added the senders address to My Contacts.
  • Test by sending another email from my anvil app, and it appeared in my “Other” inbox.
  • Right clicked the email in the “Other” inbox, and selected “Always move to Focused”.

Now works as expected

I would like to express my gratitude to the community for trying to help me. I’ve learned a few things along the way…namely, how to use the uplink and start a server console, I was incorrectly using the App Console tab when I got those permission error.

Regards to All
Boz