We help IT Professionals succeed at work.

mail via a bash script

Medium Priority
807 Views
Last Modified: 2013-12-16
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
Comment
Watch Question

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

Author

Commented:
Hi PerfectPC thats what this is doing / meant to be doing.

telnet xxx.xxx.xxx.xxx 25 < test.message

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
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

Author

Commented:
but the above still fails to send

Author

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.
CERTIFIED EXPERT

Commented:
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

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
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
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!

Author

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

Author

Commented:
woolmilkporc

took the angle brackets out and still get the same result.
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
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
"mail" does not need "| nc" because it uses "sendmail" to access the network.

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
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?
CERTIFIED EXPERT

Commented:
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

Author

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 ??

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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?

Author

Commented:
Yep that got it :) many thanks..

Author

Commented:
Many thanks to everyone who helped...
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
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

Author

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.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.