I recently added a MailChimp newsletter signup form to a client's website. If you are interested in doing something similar, the process is quite simple.
First we need to create an account with MailChimp. Create and copy your API key: http://kb.mailchimp.com/accounts/management/about-api-keys. Create a list and copy its ID: http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
We also need to download a MailChimp API (v3) wrapper; I chose to use https://github.com/drewm/mailchimp-api/ (PHP 5.3+). We only require MailChimp.php from the above link.
In this example we only use the /lists/{list_id}/members method: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members. We also display appropriate success and error messages.
newsletter.php
To place a signup form in any other template we can use the following stripped down code:
Lastly, it is highly recommended that you add some spam protection: viewtopic.php?f=8&t=7047. We could add <cms:stop_spam frm_email frm_email/> directly after <cms:if k_success> in newsletter.php.
Let me know if you found this helpful.
First we need to create an account with MailChimp. Create and copy your API key: http://kb.mailchimp.com/accounts/management/about-api-keys. Create a list and copy its ID: http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
We also need to download a MailChimp API (v3) wrapper; I chose to use https://github.com/drewm/mailchimp-api/ (PHP 5.3+). We only require MailChimp.php from the above link.
In this example we only use the /lists/{list_id}/members method: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members. We also display appropriate success and error messages.
newsletter.php
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<?php require_once( 'MailChimp.php' ); ?>
<cms:template title='Newsletter'>
<cms:editable label='MailChimp API Key' name='mc_api_key' required='1' type='text'/>
<cms:editable label='MailChimp List ID' name='mc_list_id' required='1' type='text'/>
</cms:template>
<cms:form method='post' name='subscribe'>
<cms:if k_success>
<cms:php>
global $CTX;
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($CTX->get('mc_api_key'));
$result = $MailChimp->post('lists/' . $CTX->get('mc_list_id') . '/members', array(
'email_address' => $CTX->get('frm_email'),
'status' => 'pending'
));
if (isset($result['id'])) {
echo "<p class=\"success\">Please check your inbox for a confirmation email.</p>";
} else if (isset($result['type'])) {
echo "<p class=\"error\"><strong>" . $result['title'] . " (" . $result['status'] . "):</strong> " . $result['detail'] . "<br><br>" . $result['type'] . "</p>";
} else {
echo "<p class=\"error\">An unknown error was encountered. Please try again later or contact us.</p>";
}
</cms:php>
<cms:else/>
<cms:if k_error>
<div class="notice">Please enter a valid email address.</div>
</cms:if>
<cms:input name='email' placeholder='Email Address' required='1' type='text' validator='email'/>
<input type="submit" value="Subscribe to Newsletter">
</cms:if>
</cms:form>
<?php COUCH::invoke(); ?>
To place a signup form in any other template we can use the following stripped down code:
- Code: Select all
<cms:form action="<cms:link 'newsletter.php'/>" method='post' name='subscribe'>
<cms:input name='email' placeholder='Email Address' required='1' type='text' validator='email'/>
<input type="submit" value="Subscribe to Newsletter">
</cms:form>
Lastly, it is highly recommended that you add some spam protection: viewtopic.php?f=8&t=7047. We could add <cms:stop_spam frm_email frm_email/> directly after <cms:if k_success> in newsletter.php.
Let me know if you found this helpful.