I’m having trouble with the Mailgun API. When I try to delete or update a mailing list member, I get a “Mailing list member not found” error. But when I attempt to add the same email address, it says “Address already exists”. This is confusing.
Here’s a simplified version of the code I’m using:
Function RemoveSubscriber(listAddress, userEmail)
Dim apiClient = New APIClient("https://api.mailgun.net/v2")
apiClient.SetAuth("api", API_KEY)
Dim reqParams = New Dictionary(Of String, String)
reqParams.Add("list", listAddress)
reqParams.Add("member", userEmail)
Return apiClient.SendRequest("DELETE", "lists/{list}/members/{member}", reqParams)
End Function
Has anyone else run into this issue? Any ideas on how to resolve these conflicting messages?
I’ve encountered a similar issue with the Mailgun API before, and it can be quite frustrating. In my experience, this problem often occurs due to caching or synchronization delays on Mailgun’s end.
One workaround I found effective was to implement a retry mechanism with a short delay between attempts. Something like this:
Function RemoveSubscriber(listAddress, userEmail)
Dim apiClient = New APIClient("https://api.mailgun.net/v2")
apiClient.SetAuth("api", API_KEY)
Dim reqParams = New Dictionary(Of String, String)
reqParams.Add("list", listAddress)
reqParams.Add("member", userEmail)
Dim maxAttempts = 3
Dim attempt = 1
While attempt <= maxAttempts
Dim response = apiClient.SendRequest("DELETE", "lists/{list}/members/{member}", reqParams)
If response.IsSuccessful Then
Return response
End If
System.Threading.Thread.Sleep(2000) ' Wait 2 seconds before retrying
attempt += 1
End While
Return "Failed after " & maxAttempts & " attempts"
End Function
This approach has helped me overcome the inconsistency in member status messages. If the issue persists, I’d recommend reaching out to Mailgun support for further assistance.
I’ve dealt with this Mailgun API inconsistency before. It’s likely due to eventual consistency in their distributed system. One approach that worked for me was to implement a ‘get’ request before any ‘delete’ or ‘add’ operations. This way, you can verify the current state of the member before taking action.
Here’s a rough idea:
Function ManageSubscriber(listAddress, userEmail, action)
Dim apiClient = New APIClient("https://api.mailgun.net/v2")
apiClient.SetAuth("api", API_KEY)
Dim reqParams = New Dictionary(Of String, String)
reqParams.Add("list", listAddress)
reqParams.Add("member", userEmail)
Dim memberStatus = apiClient.SendRequest("GET", "lists/{list}/members/{member}", reqParams)
If action = "delete" And memberStatus.IsSuccessful Then
Return apiClient.SendRequest("DELETE", "lists/{list}/members/{member}", reqParams)
ElseIf action = "add" And Not memberStatus.IsSuccessful Then
Return apiClient.SendRequest("POST", "lists/{list}/members", reqParams)
End If
Return "No action taken"
End Function
This approach should help mitigate the conflicting messages you’re experiencing.