Skip to content

Instantly share code, notes, and snippets.

@mattsandersuk
Created December 6, 2017 09:23
Show Gist options
  • Save mattsandersuk/56c2866e6bac2fc8b6f0ef6cd60b3fb1 to your computer and use it in GitHub Desktop.
Save mattsandersuk/56c2866e6bac2fc8b6f0ef6cd60b3fb1 to your computer and use it in GitHub Desktop.

Revisions

  1. mattsandersuk created this gist Dec 6, 2017.
    40 changes: 40 additions & 0 deletions sendToMailchimp.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?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();
    }