We often get frustrated, when we see a duplicate email address in our file or sheet. We can manage this duplicate email until if we have a small set of emails.

But the conditions become worst when duplicate emails are stored in bulk. At this point, we can not remove duplicate emails manually.

In this blog post, I am going to explain that  how to clean your email list by removing duplicate emails either it is uploaded via CSV file or manually entered.

Benefits of this code-script:-

  1. You will have two choice either enter email addresses manually or upload a CSV file.
  2. After removing the duplicate emails, you can download the results in a CSV file which doesn’t have any duplicate emails.
  3. If you don’t want to download the results then you can simply see & verify the results.
  4. Takes very less time in filtering the results.

Detailed explanation:-

We will create a form which will allow users to insert different email addresses (with a comma separation ) in a textbox.
OR
they can upload a CSV file containing a list of email addresses directly.

After clicking on submit button, results will display in a table.
OR
A user can also export the filtered results as a CSV file for both cases.

The user can easily download the exported CSV file from the Export CSV link given at the bottom of the result.

Note:-  Format for CSV file to be upload, This CSV file should have one column name say Email(can be any name) and all emails to be filter must be kept  under this coloumn.


Watch the live demo or download code from the link given below

duplicate email remover using php


MailGet is an online Email management sevice which allow you to send Bulk Emails.

You can easily connect and configure it with different SMTP service providers, e.g MailGun, Mandrill, Postmark etc, to send Emails.

Let’s have a look at the coding part.

1. Create a php file name index.php in the root folder of your application and paste the PHP code given below in it.

index.php

This index.php file contains the major functionality of the script. When you will execute this page you will get a form, image is shown above.

<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
<title>Remove Duplicate Email Addresses Using PHP</title>
</head>
<body>
<div class="container">
<div class="row">
<h1 class="main_heading">Remove Duplicate Email Addresses Using PHP</h1>
<div id="main">
<div id="login">
<h2>Duplicate Email Remover</h2>
<hr>
<div id="right">
<div id ="form1" >
<form method="post" action ="" id="ctform">
<label><b>Enter Email :</b></label>
<input type="email" multiple rows="3" id ="text1" name ="email"/>

<input type="submit" id ="dsubmit" value ="submit" name ="dsubmit" />
</form>
<b id="or">OR</b>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<label><b>Upload CSV :</b></label>
<label><input type="file" name="file" id="file" /></label>
<input type="submit" name="csubmit" id="csubmit" value="submit"/>
</form>
</div>
</div>
</div>
</div>
<div id="csvmailcontent">
<?php
try {
if (isset($_POST['dsubmit'])) {
$email = $_POST['email'];
$semail = "$email";
$arr = split(',', $semail);
/**This function remove the duplicte email in this case when input type is
is 'email' if you want to remove duplicate text or field just change the
input type in html form***/
$array = array_unique($arr);
$array2[] = $array;
$lists = json_encode($array2);
if (!empty($email)) {
echo"<table id ='tid' >";
echo"<th>";
echo"<b>";
echo"Result Emails";
echo"</b>";
echo"</th>";
foreach ($array2 as $key1) {
foreach ($key1 as $key2) {
echo"<tr>";
echo"<td>";
echo $key2 . "<br/>";
echo"</td>";
echo"</tr>";
}
} echo"</table>";
/**form having 'Export CSV' button' also takes the value to be downloaded**/
echo"<form action='emaildown.php' method='post' >
<input type='hidden' name='lists' value='$lists' id ='lists' />
<button type='submit' value='Export CSV' id ='mailsubmit'><u>Export CSV</u></button>
</form>";
} else {
echo "<script type='text/javascript'>alert('Please Enter the Emails')</script>";
}
}
} catch (MyException $e) {
echo "Some thing went wrong";
}
?>
<?php
try {
if (isset($_POST['csubmit'])) {
if (isset($_FILES["file"])) {
//if there was an error uploading the file
if ($_FILES["file"] ["error"] > 0) {
echo "<script type='text/javascript'>alert('Please choose File for upload')</script>";
} else {
$storagename = $_FILES["file"]["name"];
$_SESSION['file_name'] = $storagename;
$id = $_SESSION['file_name'];
$row = 1;
/***saving file to upload folder you can check whether your file has been upoaded or not. Clean
this folder if you find necessary after certain time when a bulk of file stored in this folder****/
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename);
$file = new SplFileObject("upload/" . $storagename);
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(',', '"', '\\');
foreach ($file as $value) {
/**extracting value from file and printing on page**/
foreach ($value as $row) {
$arcsv[] = $row;
}
}
$arcsv = array_unique($arcsv);
$_SESSION['csv_value'] = $arcsv;
if (empty($arcsv)) {
$arcsv = $_SESSION['csv_value'];
}
echo"<table id ='tid'>";
echo"<th>";
echo"<b>";
echo"Result Emails";
echo"</b>";
echo"</th>";
foreach ($arcsv as $key) {
echo"<tr>";
echo"<td>";
echo $key . "<br/>";
echo"</td>";
echo"</tr>";
}
echo"</table>";
$arcsv1[] = $arcsv;
$lists1 = json_encode($arcsv1);
}
}
if (!empty($file)) {
/**form having 'Export CSV' button' also takes the value to be downloaded**/
echo"<form action='csvdown.php' method='post' >
<input type='hidden' name='lists1' value='$lists1' />
<button type='submit' value='Export CSV' id ='csvsubmit'><u>Export CSV<u></button>
</form>";
}
}
} catch (MyException $e) {
echo "Some thing went wrong";
}
?>
</div>
</div>
</div>
</body>
</html>

2. In the same root folder, create emaildown.php file and paste the following code in it.

emaildown.php 

This file has the code for downloading the emails after removing the duplicate and store in a CSV file in the case when a user enters the emails manually.

<?php
try {
$array2 = json_decode($_POST['lists']);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$data = $array2;
$file = fopen('php://output', 'w');
fputcsv($file, array('Emails '));
foreach ($data as $row) {
fputcsv($file, $row, ',');
}
exit();
} catch (MyException $e) {

echo "Some thing went wrong";
}
?>

3. Similarly, create a csvdown.php file in the root same root folder with following code.

csvdown.php

This file contains code for downloading the emails after removing the duplicate and store in a csv file in the case when user uploads a CSV file.

<?php
try {
$array2 = json_decode($_POST['lists']);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$data = $array2;
$file = fopen('php://output', 'w');
fputcsv($file, array('Emails '));
foreach ($data as $row) {
fputcsv($file, $row, ',');
}
exit();
} catch (MyException $e) {

echo "Some thing went wrong";
}
?>

4. Finally, create a css folder in the root directory. Create style.css file in it and paste the css code given below in it.

style.css

This file is taking care of all designing work in the form. You can alter it and can make form of your choice.

@import url(http://fonts.googleapis.com/css?family=Raleway);
#main{
min-width: 350px;
margin: 44px auto;
font-family:raleway;
}
span{
color:red;
}
h1.main_heading {
text-align: center;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#login{
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 33px;
margin-top: 23px;
}
input[type=text],input[type=email]{
width:99.5%;
padding: 10px;

border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 37%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 4px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
}
#right{
overflow: hidden;
overflow-wrap: break-word;
width: 104%;
border: 1px dashed rgb(215, 215, 215);
box-shadow: 0px 5px 17px 1px #99A3AD, 0px 0px 40px #EEEEEE;
margin-left: -2%;
padding: 20px;
}
#right p{
padding: 20px;
}
form1 {
padding-top: 27px;
width: 248px;
margin: auto;
}
#right h3 {
margin-top: -8px;
text-align: center;
}
#csvmailcontent{
width:60%px;
font-family:raleway;
margin-top: -4%;
}
#mailsubmit{
background:none!important;
border:none;
padding:0!important;
font: inherit;
border-bottom:1px solid #444;
cursor: pointer;
font-size: 150%;
margin-left: 45%;
}
#csvsubmit{
background:none!important;
border:none;
padding:0!important;
font: inherit;
border-bottom:1px solid #444;
cursor: pointer;
font-size: 150%;
margin-left: 45%;
}
#uploadcsv
{
margin-bottom: 5%;
}
#text1{
margin-top: 1%;
margin-bottom: 11px;
}
input#text1 {}
#or{
margin-left: 45%;
font-size: 20px;
font-family:raleway;
margin-top: -50px;
}
#or:hover{
color: #204d74;
z-index: 8;
}
#file{
color:lightblue;
margin-top: 3px;
margin-bottom: 4px;
}
table {
font-family: 'Raleway', sans-serif;
color:#666;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
margin:20px;
border:#ccc 1px solid;
font-size: 16px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
margin: 0 auto;
}
table th {
text-align: center;
padding:21px 25px 22px 25px;
border-top:1px solid #fafafa;
border-bottom:1px solid #e0e0e0;

background: #ededed;
background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -moz-linear-gradient(top, #ededed, #ebebeb);
}
table th:first-child {
text-indent: center;
padding-left:20px;
}
table tr:first-child th:first-child {
-moz-border-radius-topleft:3px;
-webkit-border-top-left-radius:3px;
border-top-left-radius:3px;
}
table tr:first-child th:last-child {
-moz-border-radius-topright:3px;
-webkit-border-top-right-radius:3px;
border-top-right-radius:3px;
}
table tr {
text-indent: center;
padding-left:20px;
}
table td:first-child {
text-align: center;
padding-left:20px;
border-left: 0;
}
table td {
padding:18px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}
table tr.even td {
background: #f6f6f6;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6));
background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}
table tr:last-child td:first-child {
-moz-border-radius-bottomleft:3px;
-webkit-border-bottom-left-radius:3px;
border-bottom-left-radius:3px;
}
table tr:last-child td:last-child {
-moz-border-radius-bottomright:3px;
-webkit-border-bottom-right-radius:3px;
border-bottom-right-radius:3px;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
@media only screen and (min-width:768px){
#main {
max-width: 370px;
}
}

You Should have a look over incresing your Email Delivery Service rate for better inbox delivery using MailGet.

Conclusion:

Hope you would have learned & enjoyed from this blog post and would have got a clear picture about duplicate email remover. I am sure you will give a try to the script provided and implement it in your own projects as well. Feel free to visit our website again in the future to get in touch with new coding tricks. You can let us know about your feedback in the space provided below.