Several of my clients use Constant Contact to manage their email newsletter subscription lists. It’s a cost-effective solution to handle user signups, send and track emails, and manage unsubscribe requests. The default web form that Constant Contact gives you however, leaves a lot to be desired in the design department. For direct integration into your website you need to leverage their API which, at the time of this writing, is somewhat poorly documented and doesn’t include any code samples for ASP.NET.
Below is a simple code example of how you can use the API to embed a simple form on your site that will submit a request to Constant Contact and add a user to your list. I’m sure there is much more you can do with the API but this is the basic requirement for most implementations.
Step one is to request an API key from Constant Contact. Go to http://developer.constantcontact.com/apikey/login and input your username and password to generate the key that you’ll need for API access.
We can now use this authentication to make an HTTP POST to the Constant Contact server and submit our user’s form data.
Step two will be to setup a form with validation controls that will allow a user to enter their first name, last name, email address, and other fields that you may want to capture (like their company name). If you’re reading this post I’m assuming you know how to setup an ASP.NET web form. If not, buy Stephen Walther’s book.
Step three is the actual code you need to transmit the data that the user entered into the form.
UPDATE: 12/4/09 – As ‘Urbal the Great’ points out in the comments below, Constant Contact has updated their authentication method so we need to use HTTPS instead of just HTTP for the main URI. The code has been updated below.
Imports System.Net Imports System.IO 'Setup your variables Dim sUsername As String = "CONSTANT_CONTACT_USERNAME" Dim sPassword As String = "CONSTANT_CONTACT_PASSWORD" Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/activities" Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1" 'If you have more than one list, change this number to whichever list you're targeting Dim sAPIKey As String = "CONSTANT_CONTACT_API_KEY" 'Setup an HttpWebRequest to send the data Dim address As New Uri(sUri) Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest) request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword) request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" 'Build an encoded string of the data to pass to Constant Contact 'More info on this can be found at http://developer.constantcontact.com/doc/activities Dim data As New StringBuilder() data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8)) data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" & Chr(10)), Encoding.UTF8)) data.Append(HttpUtility.UrlEncode((txtEmail.Text & ",HTML," & txtFirstName.Text & "," & txtLastName.Text & "," & txtOrganization.Text), Encoding.UTF8)) data.Append("&lists=" + HttpUtility.UrlEncode(sListUri) 'The "guts" of the code to execute the request and return a response 'The response (returned as 'strResponse') will be XML. You can parse this for status messages if you like, or just ignore it. Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString()) Dim st As String = String.Empty request.ContentLength = byteData.Length Using postStream As Stream = request.GetRequestStream() postStream.Write(byteData, 0, byteData.Length) End Using Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse) Dim reader As New StreamReader(response.GetResponseStream()) st = reader.ReadToEnd() End Using
Step four would be to provide the user with some sort of confirmation. Perhaps redirect them to a “thank you page” or turn some panels on and off to signal completion of the process.
That’s it! I’m sure there are other ways to do this, probably more elegant ones, and I’d love to hear from anyone who’s done more Constant Contact integration work with .NET. If you’re just looking to quickly and easily integrate a sign-up form on your site though, this should get you started.
Special thanks to “cbaugh” on the Constant Contact forums who provided a C# example which was the baseline for this VB code example that I’ve documented here.