[AS3] A look into the MailChimp’s API
For a current project, i had to build a simple solution to suscribe to a newsletter without having to be redirected anywhere else.
MailChimp is an extremely powerful social tool, and probably the best email marketing platform available! Anyway, I will not cover MailChimp with praises, but if you are interested, just take a look.
Before using the piece of code below, you need to create 3 things :
• A texfield named emailAddress,
• A submit button, let’s call it submitBtn (what an amazing name)!
• Finally, a respond textfield where in case of success or error a message wil be displayed. We will call it responseText
Now, the end of the code.
//api and list id keys var _api:String = "YOUR_MAILCHIMP_API_KEY"; var _listID:String = "LIST_ID_HERE"; //text box handler emailAddress.text = "email address"; emailAddress.tabIndex = 1; emailAddress.addEventListener(FocusEvent.FOCUS_IN, txtFocusIn); emailAddress.addEventListener(FocusEvent.FOCUS_OUT, txtFocusOut); function txtFocusIn(e:FocusEvent) { emailAddress.text = ""; } function txtFocusOut(e:FocusEvent) { if (emailAddress.text == "") { emailAddress.text = "email address"; } } //button setup submitBtn.buttonMode = true; submitBtn.addEventListener(MouseEvent.CLICK, submitForm); function submitForm(e:Event) { var email:String = emailAddress.text; //check if email is valid if (isValidEmail(email)) { //set response text responseText.text = "sending..."; //disable the submit button submitBtn.removeEventListener(MouseEvent.CLICK, submitForm); //setup POST var variables:URLVariables = new URLVariables("method=listSubscribe&output=xml&apikey=" + _api + "&id=" + _listID + "&email_address=" + email + "&merge_vars="); var request:URLRequest = new URLRequest(); request.url = "http://api.mailchimp.com/1.2/?method=listSubscribe"; request.method = URLRequestMethod.POST; request.data = variables; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.VARIABLES; loader.addEventListener(Event.COMPLETE, completeHandler); try { trace("loading..."); responseText.text = "loading..."; loader.load(request); } catch(error:Error) { trace("unable to load URL"); responseText.text = "Oh, something went wrong, please try again. Thanks!"; trace(e.target.data); } function completeHandler(e:Event) { var _t:String = unescape(e.target.data); //decode the url var _xml:XMLList = new XMLList(_t); //parse the xml trace(_xml.@type); if (_xml.@type == "array") { //check to see if there is an error trace(_xml.error); responseText.text = _xml.error; resetForm(); } else if (_xml.@type == "boolean") { //check to see if successfully added trace("successfully added to list"); responseText.text = "You have beeen successfully added to our list. Thank you!"; resetForm(); } } } else { trace ("email invalid"); resetForm (); responseText.text = "invalid email!"; } } //validate given email function isValidEmail(_e:String):Boolean { var exp:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i; return exp.test(_e); } //reset the form elements function resetForm(){ submitBtn.addEventListener(MouseEvent.CLICK, submitForm); emailAddress.text = "email address"; }
It was quite easy, right?







