Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mattsandersuk
Created December 6, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsandersuk/56c2866e6bac2fc8b6f0ef6cd60b3fb1 to your computer and use it in GitHub Desktop.
Save mattsandersuk/56c2866e6bac2fc8b6f0ef6cd60b3fb1 to your computer and use it in GitHub Desktop.
Send form data to mailchimp Raw
<?php
// ==========================================================================
// Mailchimp
// ==========================================================================
//
// Send user to mailchimp
// --------------------------------------------------------------------------
public function sendToMailchimp(Request $request)
{
// customer info
$first_name = $request->first_name;
$last_name = $request->last_name;
$email = $request->email;
// setup
$api_key = "apikey [APIKEY]";
$list_id = "[LISTID]";
$api_endpoint = "https://[urlprefix].api.mailchimp.com/3.0/lists/{$list_id}/members";
// client
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $api_endpoint, [
'headers' => [
'Authorization' => $api_key,
'content-type' => 'application/json',
],
'json' => [
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => [
'FNAME' => $first_name,
'LNAME' => $last_name,
]
]
]);
if($res->getStatusCode() == 200){
$message = "Successfully Subscribed";
} else {
$message = "There was an error adding you to our newsletter list";
}
return redirect()->back();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment