Adding mailchimp
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 7 Nov 2018 17:39:43 +0000 (12:39 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 7 Nov 2018 17:39:43 +0000 (12:39 -0500)
Using php curl and the mailchimp 3.0 API.

Toolkit/Contacts/ContactUs.php
Toolkit/LeadManager/MailChimp.php [new file with mode: 0644]
config/application.ini
setup.phtml

index 2579b6a..ec99548 100755 (executable)
@@ -1105,6 +1105,10 @@ class Toolkit_Contacts_ContactUs
                 && CONSTANT_CONTACT) {
                 $contact->attach(new Toolkit_LeadManager_ConstantContact());
             }
+            if (    defined('MAILCHIMP_APP')
+                && MAILCHIMP_APP) {
+                $contact->attach(new Toolkit_LeadManager_MailChimp());
+            }
         }
         $contact->save($this->dbh, false);
         return true;
diff --git a/Toolkit/LeadManager/MailChimp.php b/Toolkit/LeadManager/MailChimp.php
new file mode 100644 (file)
index 0000000..adb991f
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * MailChimp.php
+ *
+ * PHP version 5.3
+ *
+ * @category  Toolkit
+ * @package   LeadManager
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2013 Gaslight Media
+ * @license   Gaslight Media
+ * @version   SVN: (0.1)
+ * @link      <>
+ */
+
+// require_once GLM_APP_BASE . 'Common/LeadManager/Affiliates/MCAPI.class.php';
+/**
+ * Require the MCAPI Class
+ */
+/**
+ * Toolkit_LeadManager_MailChimp
+ *
+ * This class is an Observer class for setting up the contact forms
+ * with the ablity to send contact data to the MailChimp list.
+ *
+ * @category  Toolkit
+ * @package   LeadManager
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2013 Gaslight Media
+ * @license   Gaslight Media
+ * @release   Release: (0.1)
+ * @link      <>
+ */
+class Toolkit_LeadManager_MailChimp
+    extends Toolkit_LeadManager_Observer
+{
+    public function send(Toolkit_LeadManager_Lead $subject)
+    {
+        // have to convert the $subject Object to an array to be used
+        // in the mailchimp call to add to list
+        $values = array(
+            'email_address' => $subject->getEmail(),
+            'first_name'    => $subject->getFname(),
+            'last_name'     => $subject->getLname(),
+            'city_name'     => $subject->getCity(),
+            'state_code'    => $subject->getState(),
+            'state_name'    => $GLOBALS['states'][$subject->getState()],
+            'zip_code'      => $subject->getZip(),
+            'home_number'   => $subject->getPhone()
+        );
+        if (is_a($subject, 'Toolkit_LeadManager_Contact')) {
+            $values['address_line_1'] = $subject->getAddress();
+            $values['address_line_2'] = $subject->getAddress2();
+        } else if (is_a($subject, 'Toolkit_LeadManager_Customer')) {
+            $values['address_line_1'] = $subject->getAdd1();
+            $values['address_line_2'] = $subject->getAdd2();
+        }
+
+        // Test if this email is in the list
+        $ch = curl_init();
+        $email = md5( strtolower( $subject->getEmail() ) );
+        $url = 'https://us19.api.mailchimp.com/3.0/lists/'.MAILCHIMP_LISTID.'/members/'. $email;
+        $data = array(
+            'email_address' => $subject->getEmail(),
+            'status'        => 'pending',
+            'merge_fields'  => array(
+                'FNAME' => ($subject->getFname())?$subject->getFname():'',
+                'LNAME' => ($subject->getLname())?$subject->getLname():'',
+                'PHONE' => ($subject->getPhone())?$subject->getPhone():'',
+            )
+        );
+        // echo '<pre>$data: ' . print_r( $data, true ) . '</pre>';
+        $jsonData = json_encode( $data );
+        curl_setopt_array(
+            $ch,
+            array(
+                CURLOPT_URL            => $url,
+                CURLOPT_USERPWD        => MAILCHIMP_USER . ':' . MAILCHIMP_APIKEY,
+                CURLOPT_HTTPHEADER     => array( 'Content-Type: application/json' ),
+                CURLOPT_RETURNTRANSFER => true,
+                CURLOPT_HEADER         => false,
+                CURLOPT_POST           => true,
+                CURLOPT_SSL_VERIFYPEER => false,
+                CURLOPT_CUSTOMREQUEST  => 'PUT',
+                CURLOPT_POSTFIELDS     => $jsonData,
+            )
+        );
+        $output = curl_exec( $ch );
+        // echo '<pre>$output: ' . print_r( json_decode( $output ), true ) . '</pre>';
+        // $info = curl_getinfo( $ch );
+        // echo '<pre>$info: ' . print_r( $info, true ) . '</pre>';
+
+    }
+
+
+}
index 06dd760..e8dde2d 100644 (file)
@@ -18,7 +18,7 @@ contactdb.html_email = "ON"
 
 ; STREAM SEND API
 ; Turn on the streamsend module to use with our contact application
-contactdb.streamsend.application = On
+contactdb.streamsend.application = Off
 ; Login id for the streamsend account
 contactdb.streamsend.login = "7KDfOTJvGNOH"
 ; Transaction key for the streamsend account
@@ -43,6 +43,15 @@ constantcontact.apipath = Off
 ; example BeaverIslandBoatCo/lists/1
 constantcontact.list.0 = Off
 
+; MAILCHIMP API
+contactdb.mcapi.application = On
+; Mailchimp API key
+contactdb.mcapi.apikey = "f78346e39590822f683c7572d25259f2-us19"
+; Mailchimp List Id (List)
+contactdb.mcapi.listId = "8b27bab6a8"
+; Mailchimp username
+contactdb.mcapi.user = "GolfMecca101";
+
 ; Turn the coupon application On or Off
 coupons.application = Off
 
@@ -133,6 +142,9 @@ blocks.application = On
 ; development server configuration data inherits from production and
 ; overrides values as necessary
 [development : production]
+contactdb.mcapi.apikey = "bc09e2b095b07e87e65169df37deacfd-us19"
+contactdb.mcapi.listId = "af73c15416"
+contactdb.mcapi.user = "steve@gaslightmedia.com";
 
 ; chuck's server configuration data inherits from development
 ; and overrides values as necessary
index 8c9e7be..40677f1 100644 (file)
@@ -359,6 +359,10 @@ define(
     'CONSTANT_CONTACT_LIST',
     serialize($applicationConfig->constantcontact->list->toArray())
 );
+define('MAILCHIMP_APP', $applicationConfig->contactdb->mcapi->application);
+define('MAILCHIMP_APIKEY', $applicationConfig->contactdb->mcapi->apikey);
+define('MAILCHIMP_USER', $applicationConfig->contactdb->mcapi->user);
+define('MAILCHIMP_LISTID', $applicationConfig->contactdb->mcapi->listId);
 /**
  * Coupons Database Installed?
  */