1

I'm trying to access a SMTP remote server thru a remote shell using telnet and a command file command.txt containing command to send to that SMTP server as seen on https://tecadmin.net/ways-to-send-email-from-linux-command-line/# (note: that on the above website this is done interactivly) but here I want to do that inside a command file (I've just copied the yellow/red (the user input)) to the command file.

HELO yahoo.com
mail from: sender@tecadmin.net
rcpt to: myemail@mail.com
data

Hey
This is test email only

Thanks
.

quit

then using telnet IP smtp < command.txt always returns:

Trying 1.1.65.49...
Connected to 1.1.65.49.
Escape character is '^]'.
Connection closed by foreign host.

whereas when I do it interactily with:

perlhook@bbis:~/temp_25$ telnet 1.1.65.49 smtp
Trying 1.1.65.49...
Connected to 1.1.65.49.
Escape character is '^]'.
220 miraino-manabi.jp ESMTP Postfix
HELO yahoo.com
250 miraino-manabi.jp
mail from: sender@tecadmin.net
250 2.1.0 Ok
rcpt to: myemail@ymail.com
554 5.7.1 <myemail@ymail.com>: Relay access denied
^]
telnet> quit

I get a return codes 220 250 554.

I'v also tried inside a here-doc shell script as following:

telnet 1.1.65.49 smtp <<END_SCRIPT
HELO yahoo.com
mail from: sender@tecadmin.net
rcpt to: myemail@mail.com
data

Hey
This is test email only

Thanks
.

quit
END_SCRIPT

and get the same results.

How can I get around this and make the script act as is it was interactive?

4

telnet fails for a variety of reasons. First up, if you strace it the relevant errors are telnet wanting a TTY on standard input, of which there is none (because of the redirect) so telnet fails.

$ strace telnet mx.example.edu 25 < input
...
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B0 -opost -isig -icanon -echo ...}) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(0, SNDCTL_TMR_START or TCSETS, {B0 -opost -isig -icanon -echo ...}) = -1 ENOTTY (Inappropriate ioctl for device)

If instead nc or netcat is used, the non-interactive send may still be a problem; either too much is sent in one go (which will confuse the remote server) or the server will object to too much data too quickly; SMTP is an interactive conversation. If one side is blasting away the other may delay or reject that (sending too quickly can be an indication of a spammer). If your nc supports --delay and the remote mail server is forgiving you may be able to to send.

$ strace -s 80 nc --crlf --delay 1 mx.example.edu 25 < input
...
recvfrom(3, "220 mx.example.edu ESMTP OpenSMTPD\r\n", 8192, ...
...
sendto(3, "HELO client.example.edu\r\nmail from: jhqdoe@client.example.edu\r\n...
...
shutdown(3, SHUT_WR)                    = 0
...
recvfrom(3, "500 5.5.1 Invalid command: Pipelining not supported\r\n",

OpenSMTPD here rejects the send; Postfix on a different server (not shown here) allows the above send.

A SMTP client that speaks the SMTP protocol really should be used instead; this could be kludged together with something like expect https://stackoverflow.com/questions/12320592/telnet-smtp-with-expect-or-shell-script or otherwise there are various SMTP libraries in various languages that will support TLS, SMTP AUTH, handle errors, and so forth. The shell is not a good choice here...unless you use the TCP features of a shell like ZSH, but there's probably better things to do than to "not invented here" yet another SMTP client...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.