PHP script to extract emails

This is a simple php script to extract email address from webpage. This will extract all unique emails and output each in new line. You can modify this script as per your need.

<?
$url="http://example.com/";
$text=file_get_contents($url);
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
$text,
$matches
);
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "<br />";
}
}
else {
echo "No emails found.";
}
?>

Liked It? Get Free updates in your Email

Delivered by feedburner

2 thoughts on “PHP script to extract emails

  1. Anthony
    #

    Want to contribute back as this is a very good way to get email address from the email itself without having to manually type it(Use it for single handed searches). For us, we have an email list and once we pull it up in our custom CRM, it will look for more or stop and tell us it can’t.

    <?php
    //Brought to you by Anthony, Originating code in thanks on our behalf. We changed it up to fit our custom made CRM

    //Lets Extract website from the email and get some hot links
    $stripemail = $_GET['email']; //Get the email from your browsers ?email=NAME form
    $stripdomain = substr(strrchr($stripemail, "@"), 1); //Strip the email so all we have is thewebsite.com
    $domain = "http://www.$stripdomain"; //Now lets add http://www. in front of the stripdomain so we will have a full url

    $url="$domain"; //Added the url varible to the "url" varible
    $text=file_get_contents($url); //Make it text so we can iterate for the emails
    $res = preg_match_all("/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",$text,$matches);
    // ^ Get everything left of @ before a blank space and everything afterwards before a blank space

    //Put it all together and letting it do it's thing
    if ($res) {
    foreach(array_unique($matches[0]) as $email) { $email = str_replace(' addresses','', $email);
    echo "$email";}
    }
    else {
    echo "No emails found from $url.";
    }

    ?>

    Reply
  2. Anthony
    #

    Forgot to mention if you want to get directly from your database then just change the $_GET[’email’] to your corresponding code for reading a column in your database.

    Ex:

    $result = mysql_query(‘SELECT EMAIL from YOURDATABASE’);
    while($row = mysql_fetch_array($result)) {

    $stripemail = $row[‘EMAIL’];
    $stripdomain = substr(strrchr($stripemail, “@”), 1); //Strip the email so all we have is thewebsite.com
    $domain = “http://www.$stripdomain"; //Now lets add http://www. in front of the stripdomain so we will have a full url

    $url=”$domain”; //Added the url varible to the “url” varible
    $text=file_get_contents($url); //Make it text so we can iterate for the emails
    $res = preg_match_all(“/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i”,$text,$matches);
    // ^ Get everything left of @ before a blank space and everything afterwards before a blank space

    //Put it all together and letting it do it’s thing
    if ($res) {
    foreach(array_unique($matches[0]) as $email) { $email = str_replace(‘ addresses’,”, $email);
    echo “$email”;}
    }
    else {
    echo “No emails found from $url.”;
    }

    } or die (‘…’);

    That will iterate your databse of emails and extract and append all emails as it strips, creates a link and echo the results.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *