0

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:sender@test.com"
echo "rcpt to:receiver@test.com"
echo "data"
echo "hello"
echo "."
sleep 3  

and use this command ./example.sh | telnet
but it dosn't work

could you please help me thanks

BEHXAD
  • 43
  • 1
  • 6
  • Why not use an actual command-line-based email program like [sendmail(1)](http://www.manpagez.com/man/1/sendmail/)? – Shawn May 18 '19 at 14:26

1 Answers1

3

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:sender@test.com"
echo "rcpt to:receiver@test.com"
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)

Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • when i run './example.sh | ncat mail.test.com 25 ' i face to this error: "554 SMTP synchronization error" – BEHXAD May 18 '19 at 13:00
  • Then it is most likely a timing error (maybe: don't send answer before question). Use more `sleep`. But better is to use `socat` with redirection of stdin and stdout to the script. Read `man socat` for details. – Wiimm May 18 '19 at 15:16
  • I used more sleep time 40 seconds, it is working now. thank you – BEHXAD May 19 '19 at 04:09
  • addition to short sleeps, when using telnet, we need to use `echo "QUIT"` to break the telnet connection after the completion – R S Muthu Kumaran Dec 12 '21 at 07:46