Search code examples
bashshelltelnet

Using telnet command in script for send mail


I wanna send an email via a command in Centos7. I used 'telnet' command for this issue like this:

I touch example.sh file and save these command inside it:

echo "open mail.test.com 25"
sleep 3
echo "mail from:[email protected]"
echo "rcpt to:[email protected]"
echo "data"
echo "hello"
echo "."
sleep 3  

And use this command ./example.sh | telnet
but it doesn't work.


Solution

  • telnetis designed for interactive usage. Use netcat, ncat, nc, socat or any other tool of this family.

    Example:

    ./example.sh | ncat mail.test.com 25 
    

    Herefore you have to edit your script:

    echo "mail from:[email protected]"
    echo "rcpt to:[email protected]"
    echo "data"
    echo "hello"
    echo "."
    sleep 3  
    

    The last sleep 3 is important to give ncat enough time to handle result.

    btw, I have tested nothing (just written down)