IT:AD:Mandrill:HowTo:Develop your API endpoints
Summary
Your startup's service is on servers in the cloud, your SMTP service is sitting over on the Mandrill servers.
You send out emails, they bounce, people click things, they want to unsubscribe, etc. – they have to call back some API endpoints.
Process
Unsubscribe
We've already covered what's need to process unsubscribe events:
Bounce, etc.
You use webhooks:
The webhook request:
* always uses the same data format, regardless of the event type.
* is a standard POST request
* with a single parameter - mandrill_events.
* containing JSON.
Starting point.
Don't have a working example to show, but use the following snippets as starting points.
Get the Parameter
string mandrillEventsAsJson = System.Web.HttpContext.Current.Request.Params["mandrill_events"];
Whether you have to Decode it first is to be seen.
Parse the Json
Then process it:
void Main()
{
string jsonData =
@"[
{""ts"":123,
""event"":""send"",
""url"":""http://foo.com"",
""user_agent"":""bar"",
""environment"":""bar"",
""location"":[
{
""country_short"": ""en"",
""country_long"": ""en"",
""region"": ""en"",
""city"": ""en""
}
],
}
]
";
var r = JsonConvert.DeserializeObject<List<RootObject>>(jsonData);
r[0].url.Dump();
}
// Define other methods and classes here
public class Location
{
public string country_short { get; set; }
public string country_long { get; set; }
public string region { get; set; }
public string city { get; set; }
}
public class RootObject
{
public int ts { get; set; }
public string @event { get; set; }
public string url { get; set; }
public string user_agent { get; set; }
public string environment { get; set; }
public List<Location> location { get; set; }
}
Process, according to event type
Depending on the event value, update a database.
senddeferralhard_bouncesoft_bounceopenclickspamunsubreject
Customer.EmailAddressIsValid=false; etc.
Send the OK Response.
Make sure to respond with an OK.