Solved

Email Validation Help with command line usage in Ubuntu

Posted on 2010-09-05
8
1,011 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
  • 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 109

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 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