Sunday, August 26, 2012

Creating a Single Opt-In Mailchimp form

Creating a single opt-in form for your Mail chimp Account is easy and fast. No, no plugins required, no payment. Just a simple understanding of APIs and PHP scripting is enough for creating a single opt-in Mailchimp form for your web page.

So lets get started!!

To clear out the confusion between Double opt-in and Single Opt-in read the following:
1. Double Opt-In : In this type of form, the user will get a confirmation link on his email, which upon his/her clicking adds them to your list. Otherwise Not!

This type of form is necessary for anti spam measures(So that you dont bombard your users with unwanted emails/newsletters).

2. Single Opt-in : In this type of form, the user will automatically get added to your mailing list in Mail Chimp.
Such forms are necessary for internal purposes wherein your users have no intentions of blocking you from your newsletters.

Mail Chimp provides form embedded code for your website which is by default Double Opt-in.
We cannot do anything to that embedded code. So stop banging your head, if you are doing that.

Mail chimp provides API wrappers for all scripting languages, where in you can turn off / disable the double-opt-in option.

But in case you are planning to abuse the stuff!!!!!! Visit this link :
http://mailchimp.com/contact/abuse/

So make good use of this information and go ahead.

Before beginning you need to have following:
1. An authentic API key provided to you for your account
2. The list ID of your list that you want the users emails to get subscribed to

You will get both from the Mailchimp Website.

Now to begin with you first need to get the API wrapper from
http://apidocs.mailchimp.com/api/downloads/

Download the .zip file of the latest API wrapper for PHP provided and extract the contents to a suitable place.

Now in the examples folder get the following files:

1. /inc/config.inc.php
2. /inc/MCAPI.class.php
3. mcapi_listSubscribe.php

Copy these files to another destination.(Most suitably in the folder where you have your HTML form code residing).

Follow these steps and VOILA!!!!! you will be done:

1. Open 'config.inc.php'
Change the $apikey with the your API Key.
Change $listID with the ID of list you want the user to subscribe to.
Delete the line having variable $myemail.

Save the file and exit.

2.  open the file 'mcapi_listSubscribe.php'
Change the require_once addresses by removing '/inc/'. for both the imported files.
Remove $merge_vars variable initialization.
Here you need to get the emailID of user by either $_GET or $_POST method from your form into a variable like 
$subscriberemailID = $_POST["EMAIL"];
In the function (listSubscribe()) calling line replace $myemail(we deleted it!Remember?) by the variable holding the user Email ID($subscriberemailID).
Equalize $merge_vars = null

You can change the echo function if you want.(For coders only!).

Save the file and exit.

3. Open 'MCAPI.class.php' (The crux of magic)
Find function definition of 'listSubscribe'.
In the parameters passed into it you will see a parameter $double_optin being passed as TRUE. Change it to FALSE. (That is all to this head banging stuff!!!!).

Save the file and exit.

So i guess, you have the form ready and raring to go ahead.
Now in your form code put action of your form as action = "mcapi_listSubscribe.php"
or the location of above mentioned file.

Run the form, fill it up, submit the form and the user email ID will be added directly to your Mail Chimp list
without any confirmation email being sent to the user.


21 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. great efforts taken - thanks this saved my day

    ReplyDelete
  3. Unable to load listSubscribe()! Code=250 Msg=FNAME must be provided - Please enter a value

    Tied this but got the error above - any help much appreci8ated

    ReplyDelete
  4. catch
    $FNAME = $_POST["FNAME"];

    and construct this

    $merge_vars = array('FNAME'=>$FNAME);

    and pass $merge_vars into listSubscribe

    and it works for me

    ReplyDelete
  5. is there a way to direct a subscriber to another page after they submit for single optin? Maybe a thank you page of my choosing?

    ReplyDelete
    Replies
    1. oh yes why not?

      use window.location(url) in the javascript code. and that should take you to where you wish to

      Delete
  6. hey i am getting this error

    Unable to load listSubscribe()! Code=-90 Msg=You must specify a email_address value for the listSubscribe method

    what should i do??

    ReplyDelete
  7. Thanks so much for doing this -- it saved me hours of time. I notice that the API version this works with (1.3) has been deprecated. Any chance you could do something similar for the 2.0 version that MC is now supporting?

    ReplyDelete
  8. The above API works if we have only one requried field, what to do if we have multiple requried fields?

    ReplyDelete
  9. Hi guys,
    Just read through the entire thread and sounds good what you said. I actually did the same yesterday but did not manage to get the email adress into my list.
    As I understand, when you call the listsubscribe method it should look like the following:

    listsubscribe($key, $id, $email, $merge_vars, 'default', false, false, false) ???

    Thanks

    ReplyDelete
  10. Thank you for sharing! This just saved me a ton of work :)

    ReplyDelete
  11. hey , can you help me to do this
    because i do what you have say above . but still i receive the confirmation

    help me please

    ReplyDelete
  12. hi
    how can we add firstname with email address

    ReplyDelete
  13. Hi Arshabh,

    I'm following the steps, however I have run into a problem. The files in the api that I downloaded do not correspond with the instructions. Do you have an updated set of instructions for creating single opt in mailchimp form?

    Chung

    ReplyDelete
  14. Filament
    In a single WordPress plugin, Filament contains a group of useful features including Flare, a social share button plugin that makes it easy for others to share your blogpost on Twitter, Facebook, Buffer, and more—even spots like Hacker News and Reddit. Other Filament apps include: MailChimp subscribe form, Google Analytics tracking, all-in-one profiles, code management, and share highlighter.

    ReplyDelete
  15. Filament
    In a single WordPress plugin, Filament contains a group of useful features including Flare, a social share button plugin that makes it easy for others to share your blogpost on Twitter, Facebook, Buffer, and more—even spots like Hacker News and Reddit. Other Filament apps include: MailChimp subscribe form, Google Analytics tracking, all-in-one profiles, code management, and share highlighter.

    ReplyDelete
  16. Filament
    In a single WordPress plugin, Filament contains a group of useful features including Flare, a social share button plugin that makes it easy for others to share your blogpost on Twitter, Facebook, Buffer, and more—even spots like Hacker News and Reddit. Other Filament apps include: MailChimp subscribe form, Google Analytics tracking, all-in-one profiles, code management, and share highlighter.

    ReplyDelete
  17. Filament
    In a single WordPress plugin, Filament contains a group of useful features including Flare, a social share button plugin that makes it easy for others to share your blogpost on Twitter, Facebook, Buffer, and more—even spots like Hacker News and Reddit. Other Filament apps include: MailChimp subscribe form, Google Analytics tracking, all-in-one profiles, code management, and share highlighter.

    ReplyDelete