bash and telnet to test an email
Asked Answered
K

3

8

I'm trying to find out whether an email address is valid.

I've accomplished this by usign telnet, see below

$ telnet mail.example.com 25
Trying 0.0.0.0...
Connected to mail.example.com.
Escape character is '^]'.
220 mail.example.com Mon, 14 Jan 2013 19:01:44 +0000
helo email.com
250 mail.example.com Hello email.com [0.0.0.0]
mail from:[email protected]
250 OK
rcpt to:[email protected]
550 Unknown user

with this 550 request i know that the address is not valid on the mail server... if it was valid i would get a response like the below:

250 2.1.5 OK 

How would I automate this in a shell script? so far I have the below

#!/bin/bash
host=`dig mx +short $1 | cut -d ' ' -f2 | head -1`
telnet $host 25 

Thanks!

Kaffir answered 14/1, 2013 at 20:46 Comment(2)
It might be worth noting that most large mail providers, including GMail, Hotmail and Yahoo Mail disallow this technique to prevent spammers from harvesting live accounts.Occurrence
@that other guy I've actually tried with gmail and it does work, just in a different format ...... code mail from: <[email protected]> rcpt to: <[email protected]>codeKaffir
S
10

Try doing this :

[[ $4 ]] || {
    printf "Usage\n\t$0 <domain> <email> <from_email> <rcpt_email>\n"
    exit 1
}
{
    sleep 1
    echo "helo $2"
    sleep 0.5
    echo "mail from:<$3>"
    sleep 0.5
    echo "rcpt to:<$4>"
    echo
} | telnet $1 25 |
    grep -q "Unknown user" &&
    echo "Invalid email" ||
    echo "Valid email"

Usage :

./script.sh domain email from_email rcpt_email
Stockish answered 14/1, 2013 at 20:51 Comment(0)
R
1

You could always enter your commands into a plain text file, line after line, just as if you typed them on the command line. Then you can use something like

cat commands.txt | telnet mail.example.com 25 | grep -i '550 Unknown User'

Since you will probably need to consider this text file as template, (I am assuming you will probably want to parameterize the e-mail address) you may need to insert a call to awk to take the output of 'cat commands.txt' and insert your e-mail address.

Retake answered 14/1, 2013 at 20:53 Comment(0)
P
1

variables to change

BODY="open realy smtp test"
SMTP-SRV="server_ip"
SMTP-PORT="25"
RCPT="name@domain"
SRC="name@domain"

then run in bash

/bin/nc ${SMTP-SRV} ${SMTP-PORT} << EOL
ehlo example_domain.com
mail from:${SRC}
RCPT to:${RCPT}
data
From:${SRC}
To:${RCPT}
subject: Telnet test
${BODY}
.
quit
EOL
Patrilineal answered 26/6, 2019 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.