Issue sending emails

Hi, I have a paid account and running into an issue when trying to send bunch of emails (116ish). I’m not sure where is the problem, I’ve tried logging failures from today’s task but don’t see any. Some people however still reported not getting email.

Spam filters do not register any blocked emails that we initially thought are happening, and on last Friday, August 21st, 20:00 UTC where emails go out, no one seem to have got any emails. (Unfortunately I can’t seem to access log of that day since I have limited history - also discovered a leftover task that cluttered logs and since have stopped it).

I wrapped the anvil.email.send in try/except TimoutError since I used to see some timeouts, and I try sending each email up to 10 times.

Here is the code that I’m using, sure I can add more try/excepts but is there anything that sticks out as a culprit?

@anvil.server.background_task
@anvil.server.callable
def send_announcement():
    """
    To be sent Friday at noon.
    """
    announcement = "It's that time of the week to fill out the time tracker.<br>\
                   <br>\
                   Thank you!<br><br>\
                   {}".format(survey_link)
      
    send_emails(email_list=all_emails, html=announcement)


def send_emails(email_list=[], html='', subject=""):
    for email in email_list:
        send_email(email, html, subject)


@anvil.server.callable
def send_email(email, html, subject=""):
    """
    Due to timeout error I try to send email 10 times.
    """
  
    N = 10
    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="BLI Time Tracker",
                         subject=subject if subject else "BLI Time Tracker for {}".format(first_name),
                         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))

The sever call last only 30s, it might or might not be an isue.

Admittedly, one of my user has 2 gmail accounts and only one receive the email sent. Not sure why, I have’t checked yet.

Someone in the forum reported the same issue, some emails are received, some aren’t

I think I read somewhere that on a paid account (which we have) server calls can be hours. These calls are typically 1-2 minutes I found.

Hi @nenad.bozinovic
Server calls always last 30 seconds. Paid users can enjoy background tasks, which technically last forever while free user get only 30 secs

Thanks for clarifying Tony, it helped me find a bug above, I call 'except TimeoutError when I should have called anvil.server.TimeoutError. Indeed, this get caught when called from a client side. But, the issue above was always when using scheduled background task. So it should have worked properly.

1 Like

I am glad that it helps, I didn’t know about anvil.server.TimeoutError, thanks for that

Any new hints are appreciated! To be clear I used background tasks from the the start, so issue is not that the server was timing out at 30seconds but something else.

I also did the test where I sent emails to two accounts on every hour. Logs are fine(!) saying both emails went out, one account got all 20+ emails, another only 6 at irregular times! Both times it took seconds to send emails.What could possibly cause this?? Spam filter does not show any blocked emails.

Here is a snapshot of an inbox showing missing emails (again, emails should have arrived EVERY hour).

Sorry to see that! It appears that our email provider, SendGrid, is currently experiencing a spate of serious account-takeover problems, which is probably causing some SendGrid-originated email to be blocked (often without even hitting your spambox). Here’s Krebs On Security’s writeup:

We’re working on mitigating this.

In the meantime, I would recommend signing up for an email service yourself (not SendGrid! Mailgun or Send in Blue, perhaps?), and configuring it as a Custom SMTP Server in Anvil’s Email Service. This will allow you to route around the damage for the time being.

1 Like

Thanks for alternative solution. FYI, the issue has been going on for 6+ months, typically an a small scale, email here or there would not get through, but recently it started missing in bulk.

In 2020 not even internet is immune to online virus pandemic.

Is there an example for sending email with custom SMTP server? I set one and the test said “Connection successful”. It also works fine when running independently of Anvil. Is anvil.email.send still valid call? I would expect it acts as a wrapper for smtplib calls.

I get an error: “You have enabled Custom SMTP for email sending - did you configure it correctly?”

I would assume so since it works when running via smtplib library.

Hi @nenad.bozinovic, sending email using REST API might be a good idea, I have been using it everyday without any issue:
https://anvil.works/forum/t/the-app-server-send-email-using-rest-api/5663/2

Hi @nenad.bozinovic,

This should work! If you configure a Custom SMTP server in the Email service, all anvil.email.send() calls (plus all Users Service emails) should go via there. Can you tell us more about what’s not working?

(Remember that if you have published a previous version of your app, you’ll need to publish it again if you want those changes to take effect when people visit your public URL.)

This is my custom SMTP:

and I run simple call that works fine without custom SMTP (except again when I send many emails etc. all the issues above):

@anvil.server.background_task
@anvil.server.callable
def send_test_email():
    """
    Test email sent daily.
    """
    anvil.email.send(to=admin, subject="asdf", html="asdf") 

I get this error:

anvil.email.SendFailure: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 0.35250:02017368, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:111E0000, 1.36674:0E000000, 1.61250:00000000, 1.45378:161E0000, 1.44866:34020000, 16.55847:810F0000, 17.43559:0000000024020000000000000000000000000000, 20.52176:140F148D0F00103100000000, 20.50032:140F148D7F17000000000000, 0.35180:140F148D, 255.23226:00000000, 255.27962:0A000000, 255.27962:0E000000, 255.31418:1F000130, 0.35250:00000000, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:18000000, 1.36674:32000000, 1.61250:00000000, 1.45378:1D000000, 1.44866:01000000, 16.55847:84000000, 17.43559:0000000070030000000000000500000000000000, 20.52176:140F148D0F00101053000000, 20.50032:140F148D7F17000065000000, 0.35180:58000000, 255.23226:4800D13D, 255.27962:0A000000, 255.27962:32000000, 255.17082:DC040000, 0.27745:7B000000, 4.21921:DC040000, 255.27962:FA000000, 255.1494:7D000000, 0.38698:05000780, 0.37692:01000000, 0.37948:0F010480, 5.33852:00000000534D545000000780, 7.36354:010000000000010905000780, 4.56248:DC040000, 7.40748:010000000000010B05000780, 7.57132:000000000000000000000000, 1.63016:32000000, 4.39640:DC040000, 8.45434:C3FA480F44007E419485F14E2DEC75FD05000780, 5.10786:0000000031352E32302E333334382E3031353A4D4E32505231314D42333539383A39653530623638662D626535392D343661662D383839352D66356437633735636362393000254000000000, 7.51330:0EAE32BB554FD80805000780, 0.39570:00000000, 1.55954:0A000000, 0.49266:02000000, 1.33010:0A000000, 2.54258:00000000, 0.40002:7A000000, 1.56562:00000000, 1.64146:32000000, 1.33010:32000000, 2.54258:DC040000, 255.1750:AF000000, 255.31418:0A005D36, 0.22753:BE000000, 255.21817:DC040000, 0.64418:0A00F565, 4.39842:DC040000, 0.41586:B9000000, 4.60547:DC040000, 0.21966:C8000000, 4.30158:DC040000 [Hostname=MN2PR11MB3598.namprd11.prod.outlook.com] . You have enabled Custom SMTP for email sending - did you configure it correctly? called from /downlink/anvil/email.py, line 21 called from [ServerModule, line 85](javascript:void(0)) called from [ServerModule, line 96](javascript:void(0)) called from [ServerModule, line 120](javascript:void(0)) called from [Form, line 98](javascript:void(0))

Hi @nenad.bozinovic,

This error is coming from the remote mail server. From a quick google, it looks like the mail server has denied the request to send emails from a particular email address (possibly you’re using an email address that the server is not set up to send emails from).

Could you try adding a from_address parameter to anvil.email.send and passing in the email address you have configured for use with your Custom mail server?

1 Like

Thanks Bridget, adding from_address resolved it. I think this should be made default in case of the custom SMTP server use. Also can I recommend updating the exception message since it was misleading.

1 Like

I’m glad to hear this resolved your issue.

Thanks for the suggestion - we have now improved that error message!

1 Like

Hi daviesian,

While I moved on from this, FYI, I’m still seeing the same error which is wrong:

“You have enabled Custom SMTP for email sending - did you configure it correctly?”

also I’ll repeat that I think it makes sense to set a default “from_address” to be the same as in the custom SMTP server username.

Of course, ideally, I hope you resolve the issue with Anvil SMTP server as it has been causing me (and many others) lots of issues with emails never being delivered.

1 Like

I agree.

Please make the “from_address” a user configurable item (also on the app_server). This problem has been around for a while now and should be a relatively simple fix!