subscription system using MailChimp
Hello peoples,
I am back with a new essential tutorial article on how to make a newsletter system for your own website using MailChimp API. This is yet simple and very much secure system. For that, I am gonna show you how to integrate this system. I am using the programming language is PHP. All of you developers might know that the subscription system used MailChimp or SMS. Well, I am not showing you by SMS this is the mail system. Ok, so lets. start.
Step 1 : –
First of all what you need is just make a new client account with your username, email, and password in mail chimp by clicking here. And complete your confirmation and details. You need to fill up some forms here. Complete all of them and you can skip the social networking link option.
Click Here: To Get Code of Add facebook & twitter share link on your website
Step 2 : –
In the next step what you need to do is just click on your username at the right side and choose “Account” and click. And then click on “Extras” and then “Api Keys”.
Also, you will see there something describes the plan that is you your basic account you can take only 2000 subscribers and 12000 mail you can send via mail chimp. If you need more you will need to upgrade the plan.
Step 3 : –
Now click on “create A Key” button and your key will be created. For get the key just scroll down the page and you will see the key in a table. Keep it private. Don’t give it to anyone.
Step 4 : –
Now click see the menu bar, there has an option “Lists” click on it. And you will redirect to another page. There was one list that you made in the signup time in mail chimp. click on your list name. Now click on “Settings” and then click on “List name and defaults” option and you will get a list Id. keep it also private and don’t give anyone.
Step 5 : –
Now make 2 pages in your project folder index.php, action.php . and Integrate the below codes.
index.php
<?php session_start(); // place it on the top of the script ?> <?php $statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:''; unset($_SESSION['msg']); echo $statusMsg; ?> <h1>Subscribe to get Newsletter</h1> <form action="action.php" method="post"> First Name : <input type="text" name="fname"><br> Last Name : <input type="text" name="lname"><br> Email : <input type="email" name="email"><br> <button type="Subscribe" name="submit">Subscribe</button> </form>
action.php
<?php session_start(); if(isset($_POST['submit'])){ $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){ // MailChimp API credentials $apiKey = 'YOUR_API_KEY'; //Give Your API Key Here $listID = 'YOUR_LIST_ID'; //Give Your List ID Here $memberID = md5(strtolower($email)); $dataCenter = substr($apiKey,strpos($apiKey,'-')+1); $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID; // member information $json = json_encode([ 'email_address' => $email, 'status' => 'pending', 'merge_fields' => [ 'FNAME' => $fname, 'LNAME' => $lname ] ]); // send a HTTP POST request with curl $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // store the status message based on response code if ($httpCode == 200) { $_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed.</p>'; } else { switch ($httpCode) { case 214: $msg = 'You are already subscribed.'; break; default: $msg = 'Some problem occurred, please try again.'; break; } $_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>'; } }else{ $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>'; } } // redirect to homepage header('location:index.php');
Now Test it And your system will be ready. If have any problem do comment in below comment section. You will get the reply shortly.
Click Here: To Get Code of embedding facebook page widget
Thanks for visiting our blog. Follow our website because we have more tutorial DataInFlow is a platform, where you can get the text tutorial and video tutorial of a different kind of programming language like PHP, HTML, Javascript, Jquery, CSS3 and get some technology and latest news.
You much like DataInFlow.com because it provides you to ask any kind of programming questions and get the answer in quick time. Also you can answer any others question if you know. If you have any programming question, so Click for ASK Here.