Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
35 posts Page 1 of 4
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
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.
Thanks cheesypoof! I'll definitely give this a try!
Great work, cheesypoof :)
I'm sure this will come in useful for great many sites (with MailChimp's free plan of 2000 subscribers and 12000 mails/month, there is no reason now for even basic sites not to have the newsletter feature).
Many thanks for sharing cheesyP ... I'm looking forward to trying it out :)
Fantastic solution cheesypoof! Thank you! I have just finished seting up a newsletter signup form using your tip. I have to style it up, but the important thing is that is still working. I was afraid that with MCAPI v2 the earlier MCAPI's will not work anymore, but fortunately they are still ok.
I'm glad it worked. ;)

Since v2 of the API has been released as you said, I will look into updating the thread.

Edit: Updated code and instructions to use v2 of the API.
Hi,

We will use this on our website, thanks loads for the easy to use Mailchimp :)
Hello,

I am trying to use it on my website.
I have downloaded MailChimp.class.php from your link, copy/past "newsletter.php" and use the code <cms:form action....></cms:form>
When i test it on local this fatal error comes :

Fatal error: Class 'MailChimp' not found in /Users/coeur2louve/Sites/try/couch/tags.php(2320) : eval()'d code on line 2


I think I understand there is no MailChimp class in couch/tags.php.
How to set it , Do I have to subscribe on MailChimp ?

Is it better to embed the html code from a free provider here in France ?

Thanks
I think the problem is the actual code changed on that GitHub repository. They added namespacing (PHP 5.3+ feature).

Instead of:
Code: Select all
$MailChimp = new MailChimp('<cms:show mc_api_key/>');
Please try:
Code: Select all
$MailChimp = new \Drewm\MailChimp('<cms:show mc_api_key/>');

Let me know if this works. Thanks.
Hello Cheesyproof,

Thanks a lot for your help. I did what you suggest and now a page with this message comes :

An unknown error was encountered. Please try again later or contact us.


Is it because I use it on local ?
35 posts Page 1 of 4