Multiple emails are sent when only one email should be

What I’m trying to do:
Host a webhook listener in order to capture a webhook’s JSON, wrangle it, and then push it somwhere else. For some reason though, the email trigger is being hit multiple times when it should just happen once.

What I’ve tried and what’s not working:
I have a server function that recieves a webhook and wrangles the data into a JSON to be used as the body for a PUSH request to another API. It sends 1 PUSH request per 1 POST webhook that it receives.

Then based on the PUSH response, I send an email to the target user notifying them of whether it failed or succeeded. However, it seems to be sending 2 to 3 emails per webhook event so I’m not sure why that’s happening.

Code Sample:

cd = {
         "name": substance,                  #int
         "locationid": int(locationID),      #string
         "substanceid": int(substanceID),    #int
         "supplier": vendor,                 #string
         "containersize": containerSize,     #int
         "units": unitCode                   #int
         "dateacquired": ciDate              #string 
      }

 # the endpoint returns array listing IDs of newly created items PUSHed to cheminventory
addsubstance = requests.post(
                          "https://app.cheminventory.net/api/container/add",
                          json = {
                                  "authtoken": "blahblahblah",
                                  "data": [cd] #required by API to be inside a list
                              }
                          ) 
# return object = [1, 2, 3, ...]
if addsubstance:
  anvil.email.send(#email to me saying "Succeeded in adding blah to blah") 
else:
  anvil.email.send(#email to me saying "Failed in adding blah to blah")

So in essence, I’m receiving multiple success emails when addsubstance is both a single-item array and multi-item array. I’m not sure why that’s happening because everything is supposed to just be done once.

how is the to field being populated in the anvil.email.send function?

1 Like

Along with @anthonys suggestion to check the to field, it’s possible your API is actually being triggered more than once. To check that, generate a random token of some sort (a uuid4 would work) and include that in a print statement and the body of the email. If your emails have different tokens, then they’re from different invocations of the API.

2 Likes

That’s a great point. If the webhook is expecting some return code, it could be retrying.

1 Like

I have a substance variable above that’s used as a value in the cd dictionary, so I just f-string it into the text parameter:

anvil.email.send(
      to = "lab@email.com",
      from_address = "no-reply@email.com",
      from_name = "Anvil Inventory App",
      subject = "Confirmed new container",
      text = f"This is an automated message notifying that Quartzy item: {substance} you just marked recieved was successfully added to ChemInventory."
    )

@jshaffstall I’ll give your suggestions a go! Thank you both!

1 Like

Hey @jshaffstall , I finally had the time to check it and you were right–It was sending multiple uuid’s so something was causing the emails to fire off multiple times.

It really should only be 1-in/1-out because I’m using webhook.site to monitor how many are triggered but I’m not entirely sure how to test if it’s the incoming request that’s happening multiple times, or if it’s the outbound request.

That’s difficult if you don’t have control over all of them. Any way to get any of the intervening steps to send their own unique ids you can add to the email?

That’s difficult if you don’t have control over all of them.

Dang yeah, that’s what I had figured. I think I’ll go ahead and contact the owners of each API and go from there. Thank you for all your help so far and always @jshaffstall!