Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribing email address to MailChimp from iOS app

I have added a contact form to my app that allows users to send feedback to me directly via email. I'm using Mandrill and Parse, and it works well!

On the contact form is an "Add me to mailing list…" option, and I'm looking for a way to add the user's email to MailChimp automatically if this option is checked.

I understand that there's a MailChimp API that's accessible by Objective C through a wrapper, though I'm wondering if there's not a more straightforward way to simply add an email to a MailChimp mailing list in iOS/Objective C?

Thanks for reading.


EDIT #1: Progress, but not yet success.

1) I've added cloud code from this answer to Parse (substituting in the two keys, where KEY2 is last three characters of MailChimp key):

var mailchimpApiKey = "MY_MAILCHIMP_KEY";

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 mailchimpData = {
        apikey  : mailchimpApiKey,
        id      : request.params.listid,
        email   : {
          email : request.params.email
        },
        merge_vars : request.params.mergevars
      }

      var url = "https://KEY2.api.mailchimp.com/2.0/lists/subscribe.json";

      Parse.Cloud.httpRequest({
        method: 'POST',
        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);
        }
      });

});

2) And I've added this Objective-C code to my iOS project (adding in my MailChimp list ID):

[PFCloud callFunctionInBackground:@"subscribeUserToMailingList" withParameters:@{@"listid":@"MY_LIST_ID",@"email":userEmail,@"mergevars":@{@"FNAME":firstName,@"LNAME":lastName}}
                                    block:^(NSString *result, NSError *error){
                                        if (error) {
                                            //error
                                        } else {

                                        }
                                    }];

The result? This error:

Error Domain=Parse Code=141 "The operation couldn’t be completed. (Parse error 141.)" … {error=Mailchimp subscribe failed with response code 500, code=141}

EDIT #2: More progress, but not yet success.

The previous error was being caused by an attempt to add an email address to the mailing list that was already there. I am now getting no errors and a "Successfully subscribed" result in the block above. However, logging in to MailChimp, the new address is still not there.

like image 382
Rogare Avatar asked Dec 01 '14 21:12

Rogare


People also ask

Can you use Mailchimp from your phone?

Mailchimp 's mobile app makes it easy to manage your account on the go. Create and edit campaigns, track account activity, send and reply to Inbox messages, view reports, and add new contacts directly from your mobile device.

Can you edit the mobile version of Mailchimp?

To edit mobile styles in a drag-and-drop template, follow these steps. In the Content section of the campaign builder, click Edit Design. Click the Style tab and choose Mobile Styles. Use the drop-down menus and other fields to edit the mobile styles as needed, and click Save.


1 Answers

OK, turns out the code is fine! Please use, share, and enjoy.

The issue was that MailChimp (smartly) requires double opt-in for mailing lists.

  • The first opt-in is running this code with a specific userEmail, and it results in an email being sent to your to-be-added user.
  • The email asks them to confirm their subscription, and if they do so (it's a link in the email), that's the second opt-in. Then, their email is added to your list.

So, bottom line is that the code doesn't automatically add a user to your mailing list—their confirmation is still required. It's a nice way to make sure people on your mailing list actually want to be there (i.e., have a chance of reading your emails)!

like image 131
Rogare Avatar answered Oct 30 '22 18:10

Rogare