Solved

Email Validation Help with command line usage in Ubuntu

Posted on 2010-09-05
8
1,028 Views
Last Modified: 2013-12-06
Hi,

I've been searching for a while now for an optimal way to clean up a list of emails and came across two sources that to me seem to provide the best possible solution.

#1) http://www.linuxjournal.com/article/9585?page=0,3
#2) http://php-email-address-validation.googlecode.com/svn/trunk/EmailAddressValidator.php

I chose to try the second one but I'm not quite sure how to use this script on the command line. I run Ubuntu through virtual box and I need some help with how I would run this scrip.

I saved the script (#2) in a local directory as emailvalidator.php and have another file with emails called emailcheck.txt.

What command would I need to use to call this script in the gnome terminal? Thank you in advance



0
Comment
Question by:faithless1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:ssapkota
ID: 33607666
If you are searching for a tool in gnome-terminal.
Then I better suggest you to use the power of regular expression from tools like grep/sed/awk.
Eg: if  you have a list of email addresses in a file(one in each line), you can run the following commands to filter the valid ones only.
cat file | grep -P '[a-zA-Z][a-zA-Z0-9\._-]+@[a-zA-Z0-9][a-zA-Z0-9\.]+'

[Note: this is just a quick solution and need some more tweak to perform flawlessly.]

Or if its possible can you share a chunk from your source (can be modified as well), and we can give you a full fledged solution.
Those scripts that you found are useful for php based online solution.
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33607669
If you want to read about perl based regular expression:
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33608111
Here is a teaching example showing how to validate an email address in PHP.  There are many such email validator scripts on the WWW and almost all of them are wrong in one way or another.

Good luck with it, ~Ray
<?php // RAY_email_validation.php
error_reporting(E_ALL);

// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE

function check_valid_email($email)
{
    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    // MAN PAGE: http://us3.php.net/manual/en/intro.filter.php
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }
    else
    {
        $regex = '/^[A-Z0-9_-][A-Z0-9._-]*@([A-Z0-9][A-Z0-9-]*\.)+[A-Z\.]{2,6}$/i';
        if (!preg_match($regex, $email)) return FALSE;
    }

    // FILTER or PREG DOES NOT TEST IF THE DOMAIN OF THE EMAIL ADDRESS IS ROUTABLE
    $domain = explode('@', $email);
    if ( checkdnsrr($domain[1],"MX") || checkdnsrr($domain[1],"A") ) return TRUE;

    // EMAIL NOT ROUTABLE
    return FALSE;
}

// DEMONSTRATE THE FUNCTION IN ACTION
$e = '';
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    } else
    {
        echo "<br/>BOGUS: $e \n";
    }
}
// END OF PHP - PUT UP THE FORM
?>
<form method="get">
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="<?php echo $e; ?>" />
<input type="submit" />
</form>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:faithless1
ID: 33608167
Thanks for the responses.

- Ray, how would I run this script on the command line in unix?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33608176
Sorry - I do not know.  I do not use the command line; I use PHP scripts.  Since this was posted in the PHP zone I could only help with a PHP script.  Maybe one of the other experts could show you how to adapt it to command-line execution.  I am sure it is possible.
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33609048
If you want to execute those php scripts, run:
$ php /path/to/file.php
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33609055
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33644777
@faithless1, Its not fair. The answer you accepted doesn't answer your question. Its just a simpler version of what you obtained yourself in #2.
My last 2 posts are directly answering your question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question