mail via a bash script

If I manually open telnet and copy and paste from the HELO to the QUIT into the telnet session the mail send perfectly
telnet xxx.xxx.xxx.xxx 25

220 xxx.xxx.xxx.xxx Microsoft ESMTP MAIL Service, Version: 7.0.6001.18000 ready at  Sat, 23 Apr 2011 19:02:58 +0100
HELO
MAIL FROM:<d.f@sure.com>
RCPT TO:<hyperdaz@gmail.com>
DATA
From: [Daz] d.f@sure.com
To: [Dazzie] <hyperdaz@gmail.com>
Subject: Test Mess

hi
spacemen
spacemen
spacemen

:P
daz

.
QUIT250 xxx.xxx.xxx Hello [xxx.xxx.xxx.xxx]
250 2.1.0 d.f@sure.com....Sender OK
250 2.1.5 hyperdaz@gmail.com
354 Start mail input; end with <CRLF>.<CRLF>

250 2.6.0 <SBSFSC8wSA1XvpFVjCR00000012@xxx.xxx.xxx> Queued mail for delivery
221 2.0.0 xxx.xxx.xxx Service closing transmission channel
Connection closed by foreign host.


BUT if I do
telnet xxx.xxx.xxx.xxx 25 < test.message

This fails with
Trying xxx.xxx.xxx.xxx...
Connected to xxx.xxx.xxx (xxx.xxx.xxx.xxx).
Escape character is '^]'.
220 xxx.xxx.xxx Microsoft ESMTP MAIL Service, Version: 7.0.6001.18000 ready at  Sat, 23 Apr 2011 19:14:07 +0100
Connection closed by foreign host.

But why does it fail?? I thought the logic was correct? (does the script run to fast the mail server?)

I tried the same with netcat as well... I also tried using mail -s and mailx-s and piping it into netcat neither seemed to work..

all I want to do is run a simple mail message through a relay server once something fails

thanks
hdaz
LVL 1
hdazAsked:
Who is Participating?
 
farzanjCommented:
mail program would not know. It needs some MTA and MDA.  Use sendmail
0
 
perfectpcCommented:
If I remeber correctly. You need to call a text file like you do when you automate ftp. SO you would have the batch file which would then call a txt file with the email commands in
0
 
hdazAuthor Commented:
Hi PerfectPC thats what this is doing / meant to be doing.

telnet xxx.xxx.xxx.xxx 25 < test.message
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
hdazAuthor Commented:
changes the test message just in case.

telnet 192.168.0.210 25 < testmessage.txt



cat testmessage.txt
HELO
MAIL FROM:<d.f@sure.com>
RCPT TO:<hyperdaz@gmail.com>
DATA
From: [Daz Hacker] d.f@sure.com
To: [Dazzie] <hyperdaz@gmail.com>                                                      
Subject: Test Mess

hi
spacemen
spacemen
spacemen

:P
daz

.
QUIT
0
 
woolmilkporcCommented:
Try it this way:

telnet xxx.xxx.xxx.xxx 25 <<EOF
HELO sure.com
MAIL FROM:<d.f@sure.com>
RCPT TO:<hyperdaz@gmail.com>
DATA
From: [Daz] d.f@sure.com
To: [Dazzie] <hyperdaz@gmail.com>
Subject: Test Mess

hi
spacemen
spacemen
spacemen

:P
daz

.
QUIT
EOF


The above uses a so-called "Here" document, to be read by telnet until the the terminator string ("EOF" in my example) is reached.

< xxx

Will just read one line until the next linefeed is reached.

wmp
0
 
hdazAuthor Commented:
but the above still fails to send
0
 
hdazAuthor Commented:
Hi Woolmilkporc

Thanks for the suggestion, great idea unfortunately this still fails to send a mail.

heres the output from
telnet xxx.xxx.xxx.xxx 25 <<EOF
> HELO sure.com
> MAIL FROM:<d.f@sure.com>
> RCPT TO:<hyperdaz@gmail.com>
> DATA
> From: [Daz] d.f@sure.com
> To: [Dazzie] <hyperdaz@gmail.com>
> Subject: Test Mess

> hi
> spacemen
> spacemen
> spacemen

> :P
> daz

> .
> QUIT
> EOF
Trying xxx.xxx.xxx.xxx...
Connected to xxx.xxx.xxx (xxx.xxx.xxx.xxx).
Escape character is '^]'.
220 xxx.xxx.xxx.xxx Microsoft ESMTP MAIL Service, Version: 7.0.6001.18000 ready at  Sat, 23 Apr 2011 19:49:43 +0100
Connection closed by foreign host.
0
 
farzanjCommented:
When you do it telnet, it would fail as it has to be interactive, unless you make it run by Expect.

The programs mail or mutt or mailx should work correctly though


echo "Test message - this is body of email" | mail -s "Test message" hyperdaz@gmail.com

0
 
perfectpcCommented:
I am a little confused. You posted onlinux but the telnet is windows. What server is the report coming from. If it's linux then you can just use the send mail or if windows could use a vbscript instead
0
 
woolmilkporcCommented:
OK, I didn't notice that "test.message" is a file containing all the mail commands.

This should work, just as my here document would.

What I also overlooked:

You should not use the angle brackets < > around the mail addresses!

0
 
hdazAuthor Commented:
farzanj,

Thanks for the post... well the telnet is happening on a linux machine, but the mail server is windows.

I did try
echo "Test message - this is body of email" | mail -s "Test message" hyperdaz@gmail.com | nc xxx.xxx.xxx.xxxx 25

but that did not seem to work either

just get the line
220 xxx.xxx.xxx.xxx Microsoft ESMTP MAIL Service, Version: 7.0.6001.18000 ready at  Sat, 23 Apr
2011 20:05:05 +0100

which looks promising but no email arrived :-| hence trying to go back to telnet basics

0
 
hdazAuthor Commented:
woolmilkporc

took the angle brackets out and still get the same result.
0
 
perfectpcCommented:
If your on a linux box just install sendmail  then use this example. This emails out backup log

mail -s "Zimbra Backup" backup@domain.com < /backup-log/backup-log-Zimbra.txt
0
 
woolmilkporcCommented:
"mail" does not need "| nc" because it uses "sendmail" to access the network.
0
 
hdazAuthor Commented:
only the windows server has access out through the network (i don't control the windows server which is acting as mail relay nor do I control firewall).

I can send mail via JavaMail from Linux to the windows server as well as from telnet interactively so logically it should work..

the only thing i can think of is that windows mail server is refusing to accept the commands for some reason

0
 
woolmilkporcCommented:
By the way - it's normal that you don't see any messages when using telnet non-interactively! Are you sure that the mail is not sent?
0
 
farzanjCommented:
Is it possible for you to enable sendmail so that the server has MDA -- Mail delivery agent in it.


Since you are trying so many things, why don't you also try

<<<EOF
0
 
hdazAuthor Commented:
woolmilkporc How does mail know how to get to the external address that is the windows server is there a config setting for this ??

0
 
woolmilkporcCommented:
That's a setting (under Linux/Unix) in /etc/sendmall.cf - the DS ("smart relay") or DH ("mail hub") macros.

The DS relay will receive all mail not in the local domain, the DH hub will receive all mail.

wmp
0
 
hdazAuthor Commented:
Ok i have installed sendmail

and set DS to
DS xxx.xxx.xxx.xxx
restarted sendmail with service sendmail restart

Shutting down sm-client:                                   [  OK  ]
Shutting down sendmail:                                    [  OK  ]
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]

echo "Test message - this is body of email" | mail -s "Test message" hyperdaz@gmail.com

still no mail though :-p
0
 
hdazAuthor Commented:
I did try the <<EOF here doc.. the output is in the six post below.. personally there should be no difference with doing a <<EOF and < test.message

0
 
woolmilkporcCommented:
DS macro:

1) Only mail sent to a domain other than the local domain will go over this relay!

2) Seems as if you used an IP address. DS expects a hostname! If you must use an IP address you'll have to use the square brackets <123.123.123.123> to indicate this to sendmail (as opposed to the email addresses above, where the mail server would add them by itself).

3) The machine where you're sending the mail from must be authorized to use the relay (seems that's the case, though).

0
 
hdazAuthor Commented:
I have made the changes as in placed the IP address in < > but looking from /var/log/maillog

Apr 23 21:55:17 baseserv sendmail[32514]: p3NKtHGF032514: to=hyperdaz@gmail.com, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30083, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p3NKtHpr032517 Message accepted for delivery)

relay=[127.0.0.1]

i'll try putting in the name of the server

are there any good examples for the sendmail log?
0
 
hdazAuthor Commented:
Yep that got it :) many thanks..
0
 
hdazAuthor Commented:
Many thanks to everyone who helped...
0
 
woolmilkporcCommented:
Thx for the points!

Btw., I made a typo as always :-(

Although I wrote "square brackets" (which is correct) I typed "< ... >" instead of "[ ... ]"

Sorry!

wmp

0
 
hdazAuthor Commented:
no problemo mr woolmilk i either tbought it was a typo or you did it because the site might of miss represented the brackets :-) my only thoughts are there must be a downside to using sendmail over using telnet or nc. And that is any mail will go through the rather than only the mail messages i 100% demand want to go through
not a big issue for now but it does seem strange the redirection or the here document did not work as exepected.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.