Welcome Guest, Not a member yet? Register   Sign In
Mailchimp newsletter subscriptions
#1

[eluser]doubleplusgood[/eluser]
Hey there,

I was wondering if anyone has had experience producing a newsletter signup form that connects to a Mailchimp list?

I'm trying to find a sample that will set some flashdata success and failure messages also. Attempted this myself by placing the mailchimp mcapi class as a library and then calling this in my newsletter controller;

Code:
class Newsletter extends Controller {

    function Newsletter()
    {
        parent::Controller();    
    }

    function storeAddress(){
        $data['email'] = $this->input->post('email');
        $data['api'] = 'API';
        $data['list_id'] = 'LISTID';
        $this->load->library('Mcapi',$data);

        // Validation
        if(!$data['email']){
            $this->session->set_flashdata('invalid_email', 'Email address is invalid');
        }

        if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $data['email'])) {
            $this->session->set_flashdata('invalid_email', 'Email address is invalid');
        }

        if($this->mcapi->listSubscribe($data['list_id'], $data['email'], '') === true) {
            // It worked!    
            $this->session->set_flashdata('success', 'Please check your email to confirm signup');
        }else{
            // An error ocurred, return error message    
            // return 'Error: ' . $api->errorMessage;
            $this->session->set_flashdata('error', 'Something went wrong');
        }

    }
}

Simple signup form in my view;

Code:
<?php echo form_open('newsletter/storeAddress'); ?>
<?=form_input('email', set_value('email'));?>
<?=form_submit('submit', 'Add')?>
<?php echo form_close(); ?>

If anyone has any advice/pointers, I would be very grateful. Smile

Thank you.
#2

[eluser]doubleplusgood[/eluser]
Just bumping this to see if anyone can help me integrate a signup form with mailchimp? Smile

Thank you.
#3

[eluser]The Wizard[/eluser]
Hello,

this is a quick & dirty integration of mailchimp.

It loops trough the lists in your api key and adds the
email to it.

Hope it is of good use to you.

BTW, i renamed the file MCAPI.class.php to MCAPI.php and put
it into the libraries folder.
Code:
$this->load->library( 'Mcapi', array() );
        



        $apikey = 'HERE GOES YOUR API KEY, DONT FORGET THIS, YES I KNOW THIS WILL CAUSE AN ERROR'
        $this->load->library( 'Mcapi' );

        $api = new MCAPI($apikey);

        $data_lists = $api->lists();

        if ( $api->errorCode )
        {
            echo "Unable to load lists()!";
            echo "\n\tCode=".$api->errorCode;
            echo "\n\tMsg=".$api->errorMessage.'<br />';
        }
        else
        {


            foreach ( $data_lists['data'] as $list )
            {

                // echo "Id = ".$list['id']." - ".$list['name'].'<br />';
                // echo "Web_id = ".$list['web_id'].'<br />';
                // echo "\tSub = ".$list['stats']['member_count'];
                // echo "\tUnsub=".$list['stats']['unsubscribe_count'];
                // echo "\tCleaned=".$list['stats']['cleaned_count'].'<br />';


                $merge_vars = array(
                                    'FNAME'=>'Test',
                                    'LNAME'=>'Account'
                                    );

                // By default this sends a confirmation email - you will not see new members
                // until the link contained in it is clicked!
                $retval = $api->listSubscribe( $list['id'], '[email protected]', $merge_vars );

                if ($api->errorCode){
                    echo "Unable to load listSubscribe()!\n";
                    echo "\tCode=".$api->errorCode."\n";
                    echo "\tMsg=".$api->errorMessage."\n";
                } else {
                    echo "Subscribed - look for the confirmation email!\n";
                }
                


            }


        }




Theme © iAndrew 2016 - Forum software by © MyBB