PHP CLI script check for valid Email address

en: Example of a php cli script to check a (large) list of email addresses for validity
de: Beispiel eines PHP CLI Skriptes zur Überprüfung einer (grossen) Liste von Email Adressen auf Gültigkeit


You have a list of email addresses you want to validate in a text file

1@example.com
john.doe@me
john.doe.me.com
john.doe@gmail.com

create a small php cli script: check.email.php

#!/usr/bin/php
<?
define ( 'CR', chr(10).chr(13) ) ;
if ( count ( $argv ) <= 1 ) die ( 'no arguments' . BR ) ;
$srcfile = $argv[1] ;
if ( !file_exists ( $srcfile ) ) die ( 'file ' . $srcfile . ' does not exist' . BR ) ;
$file = file ( $srcfile ) ;
foreach ( $file as $email ) {
    echo trim ( $email ) ;
    if ( !filter_var ( trim ( $email ), FILTER_VALIDATE_EMAIL ) ) {
        echo " \033[1;31mInvalid email format\033[0m" ;
    }   
    echo CR ;
}
?>

make it executable with

chmod +x check.email.php

run with the textfile name argument

./check.email.php file_to_validate.csv
1@example.com
john.doe@me Invalid email format
john.doe.me.com Invalid email format
john.doe@gmail.com

Notice: it does NOT check if the email address exists, it only checks for proper syntax.


TITLE: Example of a php cli script to check a (large) list of email addresses for validity
DATE: 2017-12-21
UPDATE:
AUTHOR: jRu
SOURCE:
TAGS:
cli email php validation