Use the Mailchimp API

前端 未结 3 862
一个人的身影
一个人的身影 2021-01-01 04:50

I\'d like to use the Mailchimp Node.js API in my Parse Cloud Hosting app to subscribe a user to a mailing list. Parse doesn\'t support NPM but, given that the Mailchimp API

3条回答
  •  别那么骄傲
    2021-01-01 05:07

    Here's how I got it to work using the MailChimp API v3.0, the method below supports adding/updating a subscriber, as well as adding/removing him to/from a Group!

    Prerequisite: You need to get an MD5 hash method to convert the user's email into a hash.

    • Here's the one I used: http://www.webtoolkit.info/javascript-md5.html#.Vuz-yjZOwXV
    • Copy the code in the link, and paste it into a newly created file, name it "md5js.js" for example.
    • Update the code you copied to start with exports.MD5 = function (string) {
    • You can test the conversion you got from the copy/pasted module by comparing the result with this online tool: http://www.miraclesalad.com/webtools/md5.php

        var jsmd5 = require('cloud/md5js.js');
    
        // here replace that with your own data center (by looking at your API key).
        var datacenter = "us13";
        var MAILCHIMP_URL = "https://:@" + datacenter + ".api.mailchimp.com/3.0/";
        var MAILCHIMP_LIST_NEWSLETTER_ID = ;
    
        Parse.Cloud.define("SubscribeUserToMailingList", function(request, response) {
    
          if (!request.params ||
                !request.params.email){
            response.error("Must supply email address, firstname and lastname to Mailchimp signup");
            return;
          }
    
          var email = request.params.email;
          var firstName = request.params.firstname;
          var lastName = request.params.lastname;
    
          // this converts the email string into an MD5 hash.
          // this is Required if you want to use a "PUT" which allows add/update of an entry, compared to the POST that allows only adding a new subscriber.
          var emailHash = jsmd5.MD5(email);
    
          var mailchimpData = {
            'email_address': email,
            'status': "subscribed",
            'merge_fields': {
              'FNAME': firstName,
              'LNAME': lastName
            },
            'interests': {
              "": true  // optional, if you want to add the user to a "Group".
            }
          };
    
          var url = MAILCHIMP_URL + "lists/" + MAILCHIMP_LIST_NEWSLETTER_ID + "/members/" + emailHash;
    
          // using a "PUT" allows you to add/update an entry.
          Parse.Cloud.httpRequest({
            method: 'PUT',
            url: url,
            body: JSON.stringify(mailchimpData),
            success: function(httpResponse) {
              console.log(httpResponse.text);
    
              response.success("Successfully subscribed");
            },
            error: function(httpResponse) {
              console.error('Request failed with response code ' + httpResponse.status);
              console.error(httpResponse.text);
    
              response.error('Mailchimp subscribe failed with response code ' + httpResponse.status);
            }
          });
        });

提交回复
热议问题