Created
December 6, 2017 09:23
-
-
Save mattsandersuk/56c2866e6bac2fc8b6f0ef6cd60b3fb1 to your computer and use it in GitHub Desktop.
Send form data to mailchimp Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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