views:

293

answers:

1

Hi,

I try to validate an email with a shell script. Is there a easy example to validate a mail? Asked google but just found crap and PHP (also crap..).

Thanks and regards.

A: 

You mean something like this?

#!/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

Found at: Fun things in Life - Simple Bash Email Validator

codedevour
This is not a substitute for speaking SMTP properly (waiting after the HELO, and dealing with multi-line responses, etc.). Just because something is on the web somewhere doesn't mean it's useful.
ShiDoiSi
Also, it's written in a style compatible with the Bourne shell and doesn't take advantage of any Bash features.
Dennis Williamson
Found this script also by google. Isn't the perfect solution. Atm i use something like echo $mail | cut -d '@'.... works but isn't also perfect.Thanks for your replies.
fwa