PDA

View Full Version : Verify mail Address.



Hackuin
07-25-2011, 03:00 PM
Recently, we had a seminar for computational criminology. And, we had collected various people e-mail, addresses and we are about have a seminar again next month. We had to send invitation to selected members, but the list go too high numbered, and to my astonishment, I had sent them email, but, mail box dumped with "Mailer Demon-Failure", to the stupidity of humans haunt us at most of the times, plenty of people have either misspelled there E-mail Addresses or supplied wrong E-mail address.

So, to avoid, these situations again, I taught of wring a script to verify e-mail address, before sending E-mails/Just to send e-mails to valid address.


#!/bin/bash
#:
#: e-mail.verify.sh
#:
#: Date: 2011/23/07 08:20:58 PM IST
#: Author: Hackuin _at_ Ymail com
#: Discription: Verify (or) Validate the G-Mail Adresses.
#:
#: Copyright (c) 2011 Under GPL License.
#:


if [ ! $# == 1 ]
then
echo "Usage: $0 Filename"
exit 0
fi

cd /tmp

cat $1 | while read LINE; do
{
FILENAME="i*"
NULL="/dev/null"
rm -rf i*


#: At first I tryied this URI, but, it limited to 5 mail verification
#: per hour it seems :/
#: wget "http://www.email-unlimited.com/tools/verify-email.aspx?Email=$LINE&B1=Verify"

wget "http://my-addr.com/free-email-verification-tool/verify-email-address/reverse-email-lookup/verify_email.php/?mail=$LINE&x=15&y=13"
grep "e-mail exist" $FILENAME >$NULL
if [ "$?" -eq "0" ]
then
echo -e "$LINE" > VALIDEMAIL
else
echo -e "$LINE" > $NULL
fi
}
done

#: END


However, instead of using/calling, other peoples tool, I mean the existing "my-addr.com" script, I again taught of writing our own script, verify.


#!/bin/bash
#:
#: e-mail.verify.sh
#:
#: Date: 2011/22/07 09:40:28 PM IST
#: Author: Hackuin _at_ Ymail com
#: Discription: Verify (or) Validate the G-Mail Adresses.
#:
#: Copyright (c) 2011 Under GPL License.
#:


#: First we create a Expect script to be sourced for us.
#:
if [ ! $# == 1 ]
then
echo "Usage: $0 Filename"
exit 0
fi

#: Verifying the G-mail 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 gmail.com |grep 5 |cut -d " " -f 7| sed 's/\.$//'`
ping -c1 $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

# Checking Expect is available or not.

expect -v >/dev/null
if [ "$?" -eq 0 ]
then
echo -e "Expect:" "\t\t\t\t\t\t\t$C_G[ OK ]$C_END" ""
else
echo -e echo -e "Expect:" "\t\t\t\t\t\t\t$C_R[ FAIL ]$C_END" ""
echo -e "sudo apt-get install -y expect"
fi
COUNT=0
RM_FILE="validemails.txt"

rm -rf $RM_FILE

cat $1 | while read LINE; do
{
MAFR="MAIL FROM: <EXPECT@SCRIPT.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-veri1
#!/bin/expect
#:
#: Date: 2011/22/07 09:23:44 PM IST
#: Author: Hackuin _at_ Ymail com
#: Discription: Expect Script to Verify/Validate the G-Mail Adresses.
#:


set VMAFR "$MAFR"
set VMATO "$MATO"

spawn nc gmail-smtp-in.l.google.com 25
expect "ESMTP"
send "HELO\r"
expect "service"
send "\$VMAFR\r"
expect "OK"
send "\$VMATO\r"
expect "OK"
send "quit\r"

expect eof
__EOF

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

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

if [ "$?" -eq 0 ]
then
echo -e $LINE >/dev/null #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


Below are screen shots:

http://img1.imagehousing.com/91/c8d8deca52c62622684c6219a133a111.png

-Hackuin

the_empty
07-25-2011, 06:11 PM
wow its really interesting. Shows the power of Bash in good hands....

TFS

Anant Shrivastava
07-25-2011, 06:13 PM
If i understand the script correctly this one is for gmail only however similar checks could be implemented for other services also.

that will be i think 2-3 more steps.

1) extract the domain name (all i can think of is cut -f2 -d"@")
2) extract the mx record server (nslookup or dig)
3) connect and verify.

Am i right.

Hackuin
07-25-2011, 08:48 PM
@Anant:
Yep.
As most of e-mail were of G-mail, I extracted them to a file. And, sourced it to the script.
With the first script, you could directly supply, well know Mail Service providers, like, GMAIL-YAHOO-HOTMAIL etc.,

For the second script.
Just need to extract , MX records, like,


host -t mx google.com/hotmail.com/yahoo.com

which gives the smtp servers. And, source the email-list file to the server and verify.
-Hackuin

prashant_uniyal
07-25-2011, 09:11 PM
Great share..!! Really, this shows the power of bash :)

abhaythehero
07-26-2011, 03:59 PM
trivial suggestion .. won't it be better if we exit 1 instead of 0 here ...

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

Hackuin
07-26-2011, 04:58 PM
The whole idea of exit( ) is status of the script at the point.
As, we are using "echo" in other words, we are displaying an error message to STDOUT. So, it doesn't matter how exit ( ) behaves.
If we were not using "ehco", it might be helpful, even tough again, it depends on weather, we are using the EXIT STATUS again in the script referencing to it.

louay
12-01-2011, 09:15 PM
Its fabulous script ^_^

Could you please advice me if i want to check list of emails at Hotmail server.

I have been modified this part on script but it still not work for me



#!/bin/expect
#:
#: Date: 2011/22/07 09:23:44 PM IST
#: Author: Hackuin _at_ Ymail com
#: Discription: Expect Script to Verify/Validate the G-Mail Adresses.
#:


set VMAFR "$MAFR"
set VMATO "$MATO"
spawn nc mx1.hotmail.com 25
expect "Sending"
send "EHELO mx1.hotmail.com\r"
expect "OK"
send "\$VMAFR\r"
expect "OK"
send "\$VMATO\r"
expect "250"
send "quit\r"

expect eof

please advice ^_^

Hackuin
12-03-2011, 04:02 PM
@louay:
1. You need to understand the logic of the script at first place.
2. domain doesn't accept "EHELO", try "HELO"
3. use nc -C mx1.hotmail.com 25, as host doesn't recognize the line feed as the other domains.




.....

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

expect eof


-Hackuin

neo
12-05-2011, 10:32 AM
The whole idea of exit( ) is status of the script at the point.
As, we are using "echo" in other words, we are displaying an error message to STDOUT. So, it doesn't matter how exit ( ) behaves.
If we were not using "ehco", it might be helpful, even tough again, it depends on weather, we are using the EXIT STATUS again in the script referencing to it.

Just suggestion:

Well the point raised by abhay is regarding general good program practice. Normally it is good programing practice to exit using code 1 if due to some reason your pogram is not able to do intented work :) Because we dont know in advance if some one want to reffer our script in other script or not :)

Overall Nice Script dude :)

neo
12-05-2011, 10:42 PM
Written a python script for same purpose...
if someone wants to check it out.
HERE (http://www.garage4hackers.com/f33/verify-email-address-python-1790.html) it is