Use the Mailchimp API

前端 未结 3 888
一个人的身影
一个人的身影 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:14

    1. Install mailchimp in your project

      npm install mailchimp-api
      
    2. From client controller call the server-controller with required data
      Don't forget to add $http to the top of controller

      $http({
          method : 'POST',
          url : '/mailchimp-users/subscribe',
          data : {user:this.name}}).
              success(function(response) {
          console.log("hai this is basic test" + response);
          $scope.send = response.message;
      }).error(function(response) {
          $scope.error = response.message;
      });
      
    3. In server controller Add this to the beginning of page

      var MailchimpUser = mongoose.model('MailchimpUser'),
      _ = require('lodash'),
      mcapi = require('mailchimp-api');
      var apiKey = '4bf6fb8820c333da4179216c3c2ef8fb-us10';
      // Change this to your Key
      var listID = 'ebbf193760';
      var mc = new mcapi.Mailchimp(apiKey, {version: '2.0'});
      

      Add this function

      exports.subscribe = function(req, res) {
          var entry = req.body.user;
          var mcReq = {
              apikey: '4bf6fb8820c333da4179216c3c2ef8fb-us10',
              id: 'ebbf193760',
              email: {email: entry + '@gmail.com'},
              merge_vars: {
                  FNAME: 'subscriber-first-name',
                  LNAME: 'subscriber-last-name'
              },
              'double_optin': false,
              'send_welcome': true
          }
          // submit subscription request to mail chimp
          mc.lists.subscribe(mcReq, function(data) {
              console.log(data);
          }, function(error) {
              console.log(error);
          });
      };
      
    4. Add this route your route file

      app.route('/mailchimp-users/subscribe')
         .post(mailchimpUsers.subscribe);
      

提交回复
热议问题