Unable to submit to mailchimp via Uniform Plugin

I am attempting to use a Uniform form to send an email & submit an email address to Mailchimp.

The form sends out the 2x emails perfectly (1 to the client & 1 to the customer). However nothing is being submitted to Mailchimp. I am assuming the issue lies in the ‘data’ or ‘headers’ params. Any help would be super appreciated.

<?php

use Uniform\Form;

return function ($kirby)
{
    $form = new Form([
        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'title' => [],
        'organisation' => [],
        'tel' => [],
        'message' => [],
    ]);

    if ($kirby->request()->is('POST')) {
        $form->emailAction([
                'to' => 'testemail@test.com',
                'from' => 'testemail@test.com',
                'subject' => 'Newly submitted website form',
            ])
            ->webhookAction([
                'url' => 'https://us5.api.mailchimp.com/3.0/lists/XXXXXXXXXX/members/',
                'json' => true,
                'params' => [
                    'method' => 'POST',
                    'data' => ['status' => 'pending'],
                    'headers' => ['Authorization: apikey XXXXXXXXXXXXXXXX-XXX'],
                ],
            ])
            ->emailAction([
                // Send the success email to the email address of the submitter.
                'to' => $form->data('email'),
                'from' => 'hello@test.com',
                'replyTo' => 'hello@test.com',
                'subject' => 'Thank you for your message.',
                'template' => 'success',
            ]);
            
    }

    return compact('form');
};

Can you do a remote request with these credentials, either using Kirby’s Remote::post() method, or via a REST client like Postman, Insomnia …?

This is an older topic but I want to share my solution based on @jimbobrjames’ kind help here (Uniform by @mzur + Mailchimp API - #4 by jimbobrjames) and in the discord channel. Maybe it helps someone in the future—especially since I can’t find any documentation on this from Mailchimp’s side.

Keeping Uniform’s validations in place but replacing the webhook action with a Remote call wrapped in a try-catch-statement:

try {
    $url = 'https://usXX.api.mailchimp.com/3.0/lists/XXXXXX/members/';
    $data = [
        'email_address' => $data['email'],
        "status" => "pending",
        'merge_fields'  => [
            'FNAME' => $data['firstname'],
            'LNAME' => $data['lastname'],
        ],
    ];
    $options = [
        'headers' => [     
            'Content-Type: application/json',
            'Authorization: Basic XXXXXX',
        ],
        'method' => 'POST',
        'data'   => json_encode($data),
    ];

    $response = Remote::request($url, $options);

    // do whatever you need to with the response

} catch (Exception $error) {
    return Response::json(['message' => 'The submission could not be sent: ' . $error->getMessage()], 400);
}
2 Likes