Trouble sending email via posix script

1

I have a smtp server to send emails through Red Hat 7.2 If I type the commands on the server the mail is send successfully. However, I have tried to create a script to send mails automatically but it fails. My script:

#!/bin/bash

(
echo "HELO server.com";

echo "MAIL FROM: account@server.com";

echo "RCPT TO: rcptoacc@server.com";

echo "DATA";

echo "From: account@server.com";

echo "To: rcptoacc@server.com";

echo "Subject: Test mail";

echo -e "\n";

echo "Ok.";

echo "Bye";

echo -e "\n\n.";

echo "QUIT";

) | /usr/bin/telnet smtp_server 25

What am I doing wrong?

The-0m3n

Posted 2016-12-20T18:21:08.770

Reputation: 11

On the server do you see any evidence of the connection and/or errors via the server? It could be all this gets sent too fast and the telnet connection completes before the server finishes sending its banner back. After all, SMTP is a dialog with the server, not a monologue – Eric Renouf – 2016-12-20T19:58:44.880

1Also, bash is not POSIX, nor is echo -e so this isn't a POSIX script – Eric Renouf – 2016-12-20T20:00:11.117

See this answer for a way to do it in pure bash (definitely not POSIX though)

– Eric Renouf – 2016-12-20T20:02:22.310

Thanks Eric. The server throws: Trying x.x.x.x... Connected to x.x.x.x. Escape character is '^]'. Connection closed by foreign host.

I put a sleep on each line but the result is the same. This script works on HP-UX – The-0m3n – 2016-12-20T20:25:31.407

Do you have one before the first line as well, and are the sleeps long enough? That's a fragile way to go, but when I just tried the script as-is against a mail server I have it complained about the client disconnecting after CONNECT, so it never saw the conversation – Eric Renouf – 2016-12-20T20:47:17.907

With the leading sleep and a sleep after each line that is part of the conversation it worked for me, though I still think you'd be better off using a tool for talking smtp or doing it the way in the linked answer – Eric Renouf – 2016-12-20T20:49:56.770

Ok, I will try to do it as the linked answer. By the way, the script worked adding a sleep after the QUIT line: echo "QUIT"; sleep 1; ) | /usr/bin/telnet ` Thanks for the advice. DASM – The-0m3n – 2016-12-21T00:14:20.010

No answers