Publishing System Settings Logout Login Register
Create an Email Stripper
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on March 10th, 2007
4031 views
PHP Coding
In this tutorial we are going to create an email collecter. I get spam mail from other design sites where the CC has been used instead off BCC. And I like to grab these email addresses so i can advertise my site to these people. So i developed this script to do it. I do this using the explode function built into php.

To begin with you need to create the database where we will store the information


CREATE TABLE `emailer` (
`email` varchar(255) NOT NULL default '',
PRIMARY KEY (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


And then create a new file. We will start with the database connection information


<?
$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
$dba = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");



Thats just the basic connection info, you can stick it in another file and include it if you want.

Now for the fun parts. Here is the rest of the code.


if ($_POST['Submit']){
$string = $_POST['textarea'];
$abc = explode(',',$string);
$max = count($abc);
$i = 0;
while($i < $max){
$email = trim($abc[$i]);
$query = mysql_query("INSERT INTO `emailer` ( `email` )
VALUES (
'$email'
)");
$i = $i + 1;
}
echo "All done";
}else{
?>
<form name="email" method="post" action="">
<label>Enter Emails:
< textarea name="textarea">< /textarea>
</label>

<label>
< input type="submit" name="Submit" value="Submit">
</label>
</form>
<?
}
?>


As you can see there is the form has been put in the else part of the script. So when the button is pressed we got to this part

if ($_POST['Submit']){
$string = $_POST['textarea'];
$abc = explode(',',$string);
$max = count($abc);
$i = 0;
while($i < $max){
$email = trim($abc[$i]);
$query = mysql_query("INSERT INTO `emailer` ( `email` )
VALUES (
'$email'
)");
$i = $i + 1;
}
echo "All done";


This is the part where we store the individual email addresses from the text area.


$abc = explode(',',$string);
$max = count($abc);


the explode function is quite simple.


explode('seperator', 'string, '**Max items** ')
** if you leave max items empty it will collect them all

The count function counts how many array values are inside the array. Using this allows us to determine how many times we need to loop.
$i = 0;
while($i < $max){
$email = trim($abc[$i]);
$query = mysql_query("INSERT INTO `emailer` ( `email` )
VALUES (
'$email'
)");
$i = $i + 1;
}
echo "All done";


I'm using the while function to do my loop, many people would use the for function however i prefer the while function for this. So when $i, our counting varible, is larger than $max the script finishes. $i also gets the record from the array

$email = trim($abc[$i]);


Now then you may be wondering how the script stops email duplication seems as i did say that it wouldn't. Well you may have noticed that there is one field in the table we created at the start and its a primary key. This means that its individual and no it isn't case senstive. If the letters are in the same order it won't go in.

Arutha
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Arutha

As much as i love the default message i want to just say hello and to tell you to visit my blog :)
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top