I have a form that its action is a PHP file. However, I need the form data to be inserted into a MailChimp list and also send an email to the subscriber via PHPMailer.
PhpMailer is in full operation, that is, when I click the download button it sends the content to your e-mail, however it is not entering the mailChimp list.
Please help me, please, I have this bug a few days ago ... I already researched the Drew library (mailchimp API) but I did not get very satisfactory results ...
My code should send user data to the list:
<?php
require_once 'MCAPI.class.php';
require 'vendor/phpmailer/class.phpmailer.php';
require 'vendor/phpmailer/PHPMailerAutoload.php';
date_default_timezone_set('America/Sao_Paulo');
function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => '') ){
$data = array(
'apikey' => $api_key,
'email_address' => $email,
'status' => $status,
'merge_fields' => $merge_fields
);
$mch_api = curl_init(); // initialize cURL connection
curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json
$result = curl_exec($mch_api);
return $result;
}
$nome = isset($_POST['MERGE1']) ? $_POST['MERGE1']: '';
$lastname = isset($_POST['MERGE2'])? $_POST['MERGE2']: '';
$email = isset($_POST['MERGE0']) ? $_POST['MERGE0']: '';
$empresa = isset($_POST['MERGE3'])? $_POST['MERGE3']: '';
$cargo= isset($_POST['MERGE4'])? $_POST['MERGE4']: '';
$funcao = isset($_POST['MERGE5'])? $_POST['MERGE5']: '';
$check = $_POST['MMERGE6'];
$status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
$list_id = 'minhalista'; // where to get it read above
$api_key = 'minhaApi'; // where to get it read above
$merge_fields = array(
'FNAME' => $nome,
'LNAME' => $lastname,
'MMERGE3' => $empresa,
'MMERGE4' => $cargo,
'MMERGE5' => $funcao,
'MMERGE6' => $check,
);
rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );
if ($email == true){
phpMailerEnviando o email aqui...
}