I'm trying to push the $POST_[values] from my form to my Mailchimp list of contacts, but the only two values I can get to be pushed are "email" and "status" (which is "subscribed").
I read all the documentation and I can't make it work with the provided merge fields.
<div class="input-box">
<span>First Name</span>
<input type="text" name="FNAME" value="FNAME" tabindex="1">
</div>
<div class="input-box">
<span>Last Name</span>
<input type="text" name="LNAME" value="LNAME" tabindex="2">
</div>
<div class="input-box">
<span>Email*</span>
<input type="email" name="email2" tabindex="3" required>
</div>
<div class="input-box">
<span>Company*</span>
<input type="text" tabindex="4" name="MMERGE9" value="MMERGE9" required>
</div>
<input id="submit-download" type="submit" name="submit2" tabindex="5" disabled></input>
include('./mailchimp/MailChimp.php');
$MailChimp = new MailChimp('XXXX');
$list_id = 'XXX';
$email = $_POST['email2'];
$MERGE9 = $_POST['MMERGE9'];
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
'MERGE9' => $MERGE9,
I tried all variations provided by Mailchimp: MERGE9, MMERGE9, |MERGE9|
I use this library: https://github.com/drewm/mailchimp-api
I also tried to list the merge fields using the curl command but I can't get it to work:
Note: This question focuses on using a custom HTML form to post new members to a MailChimp list using the MC API; code relating to validation, security, spam/reCaptcha and other important considerations has been omitted. I don't recommend using this code on its own in production.
Original issue: When posting a new 'member' to a MC 'list_id' with their API [using post("lists/$list_id/members")], only two fields, 'status' and 'email_address', are stored, ignoring all the merge_fields I wish to also store.
Solution: 'merge_fields' have to be submitted as an array using the MC API.
In order to solve my issue, I found it useful to experiment a bit directly with the API using curl using GET, as the mailchimp-api library by drewm is great but it hides a bit of the inner workings.
PHP
<?php
include './../mailchimp/MailChimp.php'; // update this \path\to MailChimp.php from drewm mailchimp library (https://github.com/drewm/mailchimp-api);
use \DrewM\MailChimp\MailChimp; // do not change/edit this line;
if(isset($_POST['submit'])) { //edit as needed the name of the submit field in your form;
$MailChimp = new MailChimp($key); //update $key with your MailChimp API key; should look like 3856ojd298g5q82c2213453a98172346-xx22; can be found under >>Top Right Corner dropdown (displaying your name)>>Account>>Extras>>API Keys>>Create a key [keep this key private as gives partial API access to your account]
$email = $_POST['email']; // update field name as needed;
$fname = $_POST['FNAME']; // update field name as needed;
$lname = $_POST['LNAME']; // update field name as needed;
$company = $_POST['COMPANY']; // update field name as needed;
$result = $MailChimp->post("lists/$list_id/members", [ // update $list_id with your list_id; should look like this 'js83js93jm'; can be found under MailChimp.com>>Audience>>Audience Dashboard>>Manage Audience dropdown>>Settings>>Audience name and defaults>>Audience ID
'email_address' => $email, // this field is a MC API default, don't edit;
'merge_fields' => [
'FNAME' => $fname,
'LNAME' => $lname,
'MMERGE9' => $company,
],
'status' => 'subscribed', // this field is a MC API default, don't edit, although you could possibly remove it if you don't want to add the subscribed status at this point (verify in the docs);
]);
?>