Skip to content

Instantly share code, notes, and snippets.

@chadxz
Created September 23, 2013 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadxz/6671437 to your computer and use it in GitHub Desktop.
Save chadxz/6671437 to your computer and use it in GitHub Desktop.
Using MailChimp.NET MergeVars in ASP.NET WebForms. This is primarily to illustrate the use of the MergeVar class for sending custom merge variables via the subscribe method.
using MailChimp.Lists;
using System.Runtime.Serialization;
namespace MyNamespace {
[DataContract]
public class CustomMergeVars: MergeVar
{
[DataMember(Name="FNAME")]
public string FirstName { get; set; }
[DataMember(Name="LNAME")]
public string LastName { get; set; }
}
}
using System;
using MailChimp;
using MailChimp.Helper;
using MailChimp.Lists;
namespace MyNamespace {
public partial class _default : System.Web.UI.Page
{
protected void SubmitFormLink_Click(object sender, EventArgs e)
{
CommsValidator.IsValid = true;
Validate();
if (IsValid)
{
var mc = new MailChimpManager(AppGlobals.MailChimpAPIKey);
try
{
var email = new EmailParameter()
{
Email = txtEmailAddress.Text
};
var mergeVars = new CustomMergeVars
{
FirstName = txtFirstName.Text,
LastName = txtLastName.Text
};
// see here for full documentation on the lists/subscribe api call
// http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php
mc.Subscribe(listId: AppGlobals.MailChimpMailingListID,
emailParam: email,
mergeVars: mergeVars,
updateExisting: true,
sendWelcome: true);
Response.Clear();
Response.Redirect("follow_success.aspx");
Response.End();
}
catch
{
CommsValidator.IsValid = false;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment