HypeMailchimp Bundle – How to use mailchimp newsletter API integration with Symfony2

Posted on

Mailchimp bundle

Mailchimp

Introduction

HypeMailchimp bundle is a Symfony 2.x bundle to integrate most of features the new Mailchimp API V2.0 in object oriented fashionable way into your website.

Assumptions:

* You have Symfony2.x running
* You have created account on Mailchimp and have API Key ready, if not please go create on from here

* You are familiar with Mailchimp NewsLetter service

Step 1 : Setup

1.1 Download HypeMailchimp using composer

Add HypeMailchimp in your composer.json

{
    "require": {
        "ahmedsamy/hype-mailchimp-bundle": "dev-master"
    }
}

Update your composer  $ php composer.phar update “ahmedsamy/hype-mailchimp-bundle”

1.2 Register your bundle

Add the following code into your app/AppKernel.php

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Hype\MailchimpBundle\HypeMailchimpBundle(),
    );
}

1.3  Configuration

Add the following to your app/config.yml

# app/config/config.yml
hype_mailchimp:
    api_key: xxxxxxx-us5
    default_list: xxxxxxxx
    ssl: true #optional configuring curl connection

Step 2 : Use the bundle

2.1  Use the service

You can easily use it anywhere using

<?php $mailchimp = $this->get('hype_mailchimp'); ?>

2.2 Examples :

Create new campaign

<?php
    $mc = $this->get('hype_mailchimp');
        $data = $mc->getCampaign()->create('regular', array(
            'list_id' => '93419bbdc0',
            'subject' => 'test created subject',
            'from_email' => 'xxxxx@xxxx.com',
            'from_name' => 'Ahmed Samy',
            'to_name' => 'fans'
                ), array(
            'html' => '<h5>Html content</h5>',
            'sections' => array(),
            'text' => 'test',
            'url' => 'http://www.example.com',
            'archive' => 'test'
        ));
        var_dump($data);
?>

Delete existing campaign

<?php
     $mc = $this->get('hype_mailchimp');
     $data = $mc->getCampaign()
                ->setCi('1088b4ed65')
                ->del();

        var_dump($data);
?>

Send  campaign

<?php
     $mc = $this->get('hype_mailchimp');
     $data = $mc->getCampaign()
                ->setCi('1088b4ed65')
                ->send();

        var_dump($data);
?>

Subscribe new user to list

<?php
     $mc = $this->get('hype_mailchimp');
     $data = $mc->getList()
                ->addMerge_vars(
                        array(
                            'mc_notes' => 'test notes'
                ))
                ->subscribe('moneky@monkiesInSuites.com');
        var_dump($data);
?>

Note that the user will be subscriber to the default list set in config.yml if you want to change the list for this time only, you can use

<?php
     $mc = $this->get('hype_mailchimp');
     $data = $mc->getList()
                ->setListId('xxxxxxx')
                ->addMerge_vars(
                        array(
                            'mc_notes' => 'test notes'
                ))
                ->subscribe('moneky@monkiesInSuites.com');
?>

Tags: , , , , ,


  • yosri

    Hello,
    Can you explain me how to remove or disable the double opt-in ?
    Best regards

    Reply

  • Melody

    Hello,

    Thank you very much for this Bundle and explanations !

    Could you also tell us :
    - how to unsubscribe a user in a list ?
    - and how to check if a user is active in a list ?

    Thank you again for your help :)

    Reply

  • María Pilar

    Many thanks for the information! It works great ;-)

    Reply

  • frian

    Thanks man ! works like a charm :)

    Reply

  • Bruno

    Hi, thanks for this awesome bundle.

    Just a question, I send a new subscribe, I get a good response like :

    {
    “email”: “example email”,
    “euid”: “example euid”,
    “leid”: “example leid”
    }

    But it doesn’t add the new email in my list on mailchimp admin. Why ?

    Reply

  • Mayank Srivastava

    Hi,

    I am trying to install HypeMailchimp with my existing project.
    after doing all the steps given in the above tutorial,

    I get this error when I run my project:

    Twig_Error_Runtime: An exception has been thrown during the rendering of a template (“The parameter “imageonfrontendfrom” must be defined.”) in “/var/www/myproject.com/src/myproject/FrontendBundle/Resources/views/BlockHeader/more.html.twig” at line 37.

    Any leads, where I should check.

    Thanks in advace.

    PS: I am new to Symfony.

    Regards
    Mayank

    Reply

    • Ahmed Samy Post author

      I think you have problem with your form variable in twig as it’s not defined, nothing much to do with Mailchimp bundle

      Reply

  • Carlos Equiz

    Thank you so much!! Great article!

    Reply

  • LAPEYRE

    Thanks a lot !

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *