使用Bash验证电子邮件

时间:2009-10-06 08:28:54

标签: bash email validation

我尝试使用shell脚本验证电子邮件。有一个简单的例子来验证邮件吗?问谷歌,但刚刚发现垃圾和PHP(也废话..)。

谢谢和问候。

6 个答案:

答案 0 :(得分:2)

如果你在#上明确说明bash!你可以使用正则表达式:

#!/bin/bash

if [[ $email =~ '(.+)@(.+)' ]] ; then
   user=${BASH_REMATCH[1]}
   host=${BASH_REMATCH[2]}
fi

如果您认真考虑使用bash编程,请一直阅读手册页。两次。另请阅读Bash FAQ

答案 1 :(得分:1)

验证脚本。

此脚本的含义是在发送电子邮件之前确保所有有效的电子邮件。

此脚本会检查电子邮件列表。

 #!/bin/bash
#:
#: e-mail.verify.sh
#:
#: Date: 2011/14/12 13:14 PM IST
#: Author: Louay _at_ louie.msh@gmail.com
#: Discription: Verify (or) Validate the Hotmail Adresses.
#:
#:


#: First we create a Expect script to be sourced for us.

if [ ! $# == 1 ]
then
        echo "Invalid Args $0 Filename"
        exit 0
fi


#: Verifying the Hotmail adressess.
#: First verify the network Connections

C_R="\e[01;31m"                         ## Colors
C_B="\e[01;30m"
C_G="\e[01;32m"
C_END="\e[00m"


SMTPSERV=`host -t mx hotmail.com |grep 5 | grep mx2.hotmail.com |cut -d " " -f 7| sed 's/\.$//'`
ping -c2 $SMTPSERV >/dev/null
               if [ "$?" -eq 0 ]
               then
                       echo -e "Internet Connection" "\t\t\t\t\t\t$C_G[ OK ]$C_END"
                       echo -e "$SMTPSERV is AVAILABLE."
                       echo -n "Verifing"
                       for (( i=0; i<5; i++ ))
                       do
                               echo -n ".."
                               sleep 1
                                               done
                       echo
               else
                       echo -e "Internet Connection:" "\t\t\t\t\t\t$C_R[ FAIL ]$C_END" ""
                       echo -e "$SMTPSERV is Unavialable."
                       echo -e "Check your Network settings."
                       exit 0
               fi





COUNT=0
RM_FILE="validemails.txt"

rm -f $RM_FILE

cat $1 | while read LINE; do
{
MAFR="MAIL FROM: <louie.msh@gmail.COM>"
MATO="RCPT TO: <$LINE>"

#: ^variablies declared for not get escaped in the next cat command, where
#: we set the $MAFR in the expect script.

cat << __EOF > e-veri
#!/bin/expect
#:
#: Date: 2011/14/12 01:14 PM
#: Author: Louay Mshelim_at_ louie.msh@gmail.com
#: Discription: Expect Script to Verify/Validate the Hotmail Adresses.
#:


set VMAFR "$MAFR"
set VMATO "$MATO"
         spawn nc -C mx4.hotmail.com 25
                        expect "Sending"
                        send "HELO mx4.hotmail.com\r"
                        expect "OK"
                        send "\$VMAFR\r"
                        expect "OK"
                        send "\$VMATO\r"
                        expect "250"
                        send "quit\r"

expect eof
__EOF



#: Running the expect script and extracting the Results.txt

expect e-veri > Results.txt
grep 550 Results.txt >/dev/null

                if [ "$?" -eq 0 ]
                then
                        echo -e $LINE >> invalid.txt        #invalid E-mails
                else
                        echo -e "$LINE" >> validemails.txt
                fi
}
done
echo -e "Valid E-mail have been saved to $C_R[ validemails.txt ]$C_END"

#: END

答案 2 :(得分:1)

这是codevour对脚本的改进和有效的版本:

#!/bin/bash
# check for valid usage
if [ x$1 = 'x' ]
then
echo "Usage: $0 <email address>"
exit 1
fi
mailcmd=`mktemp`
# grabbing fields
user=`echo $1 | perl -p -e 's/^([^@]+)@([^\@]+)$/$1/g'`
host=`echo $1 | perl -p -e 's/^([^@]+)@([^\@]+)$/$2/g'`
mxhost=`host -t mx $host|perl -p -e 's/.* ([^ ]+)\.$/$1/g'|sort -R|tail -1`

# compose email commands
echo -ne "helo example.com\r\n" > $mailcmd
echo -ne "mail from: <tester@example.com>\r\n" >> $mailcmd
echo -ne "rcpt to: <$1>\r\n" >> $mailcmd
echo -ne "quit\r\n" >> $mailcmd

# check for mail results
mailresult=`cat $mailcmd | nc $mxhost 25| grep ^550 | wc -c`

if [ $mailresult -eq 0 ]
then
echo $1 "is valid"
exit 0
else
echo $1 "is not valid"
exit 1
fi

# clean up
rm $mailcmd

答案 3 :(得分:0)

你的意思是这样的?

#!/bin/bash
# check for valid usage
if [ x$1 = 'x' ]
then
echo "Usage: $0 <email address>"
exit 1
fi

# grabbing fields
user=`echo $1 | cut -f1 -d\@`
host=`echo $1 | cut -f2 -d\@`
mxhost=`host -t mx $host | cut -f7 -d\ `
len=`echo $mxhost | wc -c`
len=`expr $len - 2`
mxhost=`echo $mxhost | cut -b1 -$len`

# compose email commands
echo -ne "helo test.com\r\n" > mailcmd
echo -ne "mail from: test\@test.com\r\n" >> mailcmd
echo -ne "rcpt to: $1\r\n" >> mailcmd
echo -ne "quit\r\n" >> mailcmd

# check for mail results
mailresult=`cat mailcmd | nc $mxhost 25| grep ^550 | wc -c`

if [ $mailresult -eq 0 ]
then
echo $1 "is valid"
exit 0
else
echo $1 "is not valid"
exit 1
fi

# clean up
rm mailcmd

发现于: Fun things in Life - Simple Bash Email Validator

答案 4 :(得分:0)

我对后一个脚本的任何建议,检查使用接受检查但没有身份验证的多个服务器,使用知道的nslookup:

例如代码: http://www.vivaolinux.com.br/script/Simples-Verificador-de-Email-Gmail

请参阅网站中的以下代码。

我希望我做出了贡献,我也希望能够合作。

谢谢。

答案 5 :(得分:0)

你可以这样使用,

nextBoolean()