Solved

Cannot send an e-mail out of ksh script

Posted on 2013-05-10
33
1,207 Views
Last Modified: 2013-05-15
How  can I send an e-mail out of a ksh script with file attachment or with file contents (in html format) as a body of the e-mail?

I tried using

uuencode filename.html | mail -s  'Subject' user@example.com

But I get an error that it does not recognize uuencode or mail commands

This below didn't work either:

mail -s "Subject" user@example.com < filename.html

The error I get is

"SP2-0734: unknown command beginning &quot;mail -s &quot;T...&quot;"
0
Comment
Question by:YZlat
  • 17
  • 9
  • 7
33 Comments
 
LVL 4

Expert Comment

by:mgob
ID: 39157052
Check your environment variables and ensure that the location of both commands are in your path.  Or, set the full path to the command such as `/bin/mail` instead of just `mail`

( find the path with `which mail` and `which uuencode` )
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39157055
Which OS (and version) are you running?

If it can't find uuencode *or* mail, I would want to think its a PATH problem.

You're second command didn't work because you were trying to run that at at Oracle prompt and not a UNIX prompt.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39161381
I checked /usr/bin and both uuencode and mail are there

I ceched the PATH variable and it is as follows

/home/oracle:/usr/local/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/home/oracle/bin:/usr/bin/X11:/sbin:.:/u01/app/oracle/product/11.2.0.3/bin

Do you see anything that would prevent it from not finding mail or uuencode

I did try before running

/usr/bin/mail -s "Subject" user@email.com < myfile.html

but gotten the same error
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39161563
For giggles/testing, try it with full paths:


/usr/bin/uuencode filename.html | /usr/bin/mail -s  'Subject' user@example.com
0
 
LVL 35

Author Comment

by:YZlat
ID: 39167789
that gave me

<p>
SP2-0734: unknown command beginning &quot;/usr/bin/m...&quot; - rest of line ignored.
<br>
SP2-0734: unknown command beginning &quot;/usr/bin/u...&quot; - rest of line ignored.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 176 total points
ID: 39167897
You need to run that command from a UNiX prompt, not from in SQLplus.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169074
that's exactly what I do, I store the command in a ksh file and run it from the UNIX prompt
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169111
actually running

mail -s "Subject" user@example.com < filename.html

from UNIX command prompt directly worked fine. Except the e-mail was plain text and not html. How can I make it in html format?

Also I need to run it from within the ksh script and not manually from the UNIX prompt
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 176 total points
ID: 39169120
Just to let you know, "SP2-0734" is an Oracle error - there's no way you got this from ksh.  I'm pointing this out not in an attempt to correct you, but just to ask you to be careful when you cut and paste. ;-)

For HTML, you just need to add in the proper MIME header.

Try this for HTML mail:
mail -a "Content-type: text/html" -s "Subject" user@example.com < filename.html

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39169122
If it's a ksh script, you should be able to just cut/paste this into it.

What else does your ksh script do?  Does it do any Oracle stuff?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169144
yes, it retrieves data from oracle database, writes it to a fiel and then, supposedly sends this file over the e-mail
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169149
Oh, I see! I probably need to exit from sqlplus in my ksh file first, before sending an e-mail
0
 
LVL 4

Assisted Solution

by:mgob
mgob earned 174 total points
ID: 39169151
Yea, you're not exiting the Oracle CLI in your script, so the command tries to run in Oracle and obviously fails.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169157
How can I exit Oracle CLI in my script?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169171
mail -a "Content-type: text/html" -s "Subject" user@example.com < filename.html

gave me an error: "mail: Not a recognized flag: a"
0
 
LVL 4

Assisted Solution

by:mgob
mgob earned 174 total points
ID: 39169190
quit or exit to leave sqlplus.

If I recall -a is a mailx command, you just really need to put the text/html content-type in the html file I believe.

For example:

From: me@example.org
To: me@mail.com
Subject: MIME Test
Mime-Version: 1.0
Content-Type: text/html
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 35

Author Comment

by:YZlat
ID: 39169277
I tried using EXIT right before sending an e-mail but it loos like it exits the script altogether before even getting to the line that sends the e-mail
0
 
LVL 4

Expert Comment

by:mgob
ID: 39169293
How are you running the sqlplus commands?  Try sending quit instead of exit.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169357
tried both, same result

I tried

exit;

exit
/

quit;

and

quit
/
0
 
LVL 4

Expert Comment

by:mgob
ID: 39169371
Can you please post your full sqlplus section of your script? Or better yet the script in its entirety with anything sensitive removed?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169376
here is my code:

#!/usr/bin/ksh

export ORACLE_SID=DBName
ORAENV_ASK=NO
. oraenv
sqlplus -s /nolog <<EOF
CONNECT usr1/pwd
SET LINES 4000 TRIMS ON FEED OFF PAGES 0
SET MARKUP HTML ON SPOOL ON HEAD "<TITLE>SQL*Plus Report</title> -
<STYLE TYPE='TEXT/CSS'><!--BODY {background: ffffc6} --></STYLE>"
SET ECHO OFF
SPOOL myfile.html
SELECT * FROM my_view;
SPOOL OFF

exit;

mail -s "Subject" user@example.com < filename.html

Open in new window


if I remove eexit, I get an error, but if I leave the exit; there, it exits right after writing to the file
0
 
LVL 4

Assisted Solution

by:mgob
mgob earned 174 total points
ID: 39169387
You're missing your closing statement on the sqlplus section, you need a EOF at the end of it after the exit/quit.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 176 total points
ID: 39169401
Yup.  Put an EOF on line 15 and remove the 'exit' on line 16
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39169404
-a was *once* mailx only, but most standard OSes today have it on the standard 'mail' program.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169497
it worked sending the e-mail but it is still in text format. I am using AIX OS and -a does not seem to work. is there any other way?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169525
I tried using mailx command but got same error:

mailx: Not a recognized flag: a
0
 
LVL 4

Expert Comment

by:mgob
ID: 39169530
Add the headers into your mail that I had posted above.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169539
where do I add those headers?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169562
I already have this in my html file

<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
0
 
LVL 4

Expert Comment

by:mgob
ID: 39169588
Yea that's in the html file, but not in the mail headers, the easiest way to do this is to pipe it to mail with the headers already applied.

Put something like this at the top of that html file:

From: me@example.org
To: me@mail.com
Subject: MIME Test
Mime-Version: 1.0
Content-Type: text/html

Then punt that to mail with the -t flag like so:

cat filename.html | mail -t
0
 
LVL 35

Author Comment

by:YZlat
ID: 39169609
I can't really edit html file, it gets generated by sql plus
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 39169614
Thanks for answering my original question. I will open new question for the rest
0
 
LVL 4

Expert Comment

by:mgob
ID: 39169616
Okay so make a new file with it:

cat > ./outfile <<DELIM
From: me@example.org
To: me@mail.com
Subject: MIME Test
Mime-Version: 1.0
Content-Type: text/html
DELIM

cat filename.html >> outfile

cat outfile | mail -t

rm -f outfile

Or something similar
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now