Adding a “Subscribe to Newsletter” checkbox or radio button to an existing contact form is incredibly painless using the MailChimp API. Include one library file, and you’ll have access to all functions the API has to offer. In this example, I added a Yes/No radio selection. You could easily change this to be a checkbox. When “Yes” is selected, and the form is submitted, the contact form does its thing, and then the user is added to a MailChimp List.
A radio button with the name “emailUpdates” is added to the form. The name fields are used when adding this person to your MailChimp subscriber list.
Include the MailChimp API PHP Wrapper
Download the PHP “wrapper” class for the MailChimp API. This is also available for several other languages. In the below script, this is the required file “MCAPI.class.php”.
Add this to the bottom of the PHP file specified in the form action. In my case, this was my email script “sendmail.php”. Note that you should add this before any redirects at the end of the script. They would look like “header(“Location: blahblah”)”. The PHP below is a barely-modified version of the official example code for “listSubscribe”. If checks if “emailUpdates” is set to “Yes”. If it is, the submitted e-mail address and name are inserted into the list in MailChimp.
You will need to log into your MailChimp account to copy and paste your API Key and your List Unique ID.