• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1589
  • Last Modified:

Cannot send an e-mail out of ksh script

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
YZlat
Asked:
YZlat
  • 17
  • 9
  • 7
6 Solutions
 
mgobCommented:
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
 
nemws1Database AdministratorCommented:
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
 
YZlatAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nemws1Database AdministratorCommented:
For giggles/testing, try it with full paths:


/usr/bin/uuencode filename.html | /usr/bin/mail -s  'Subject' user@example.com
0
 
YZlatAuthor Commented:
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
 
nemws1Database AdministratorCommented:
You need to run that command from a UNiX prompt, not from in SQLplus.
0
 
YZlatAuthor Commented:
that's exactly what I do, I store the command in a ksh file and run it from the UNIX prompt
0
 
YZlatAuthor Commented:
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
 
nemws1Database AdministratorCommented:
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
 
nemws1Database AdministratorCommented:
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
 
YZlatAuthor Commented:
yes, it retrieves data from oracle database, writes it to a fiel and then, supposedly sends this file over the e-mail
0
 
YZlatAuthor Commented:
Oh, I see! I probably need to exit from sqlplus in my ksh file first, before sending an e-mail
0
 
mgobCommented:
Yea, you're not exiting the Oracle CLI in your script, so the command tries to run in Oracle and obviously fails.
0
 
YZlatAuthor Commented:
How can I exit Oracle CLI in my script?
0
 
YZlatAuthor Commented:
mail -a "Content-type: text/html" -s "Subject" user@example.com < filename.html

gave me an error: "mail: Not a recognized flag: a"
0
 
mgobCommented:
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
 
YZlatAuthor Commented:
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
 
mgobCommented:
How are you running the sqlplus commands?  Try sending quit instead of exit.
0
 
YZlatAuthor Commented:
tried both, same result

I tried

exit;

exit
/

quit;

and

quit
/
0
 
mgobCommented:
Can you please post your full sqlplus section of your script? Or better yet the script in its entirety with anything sensitive removed?
0
 
YZlatAuthor Commented:
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
 
mgobCommented:
You're missing your closing statement on the sqlplus section, you need a EOF at the end of it after the exit/quit.
0
 
nemws1Database AdministratorCommented:
Yup.  Put an EOF on line 15 and remove the 'exit' on line 16
0
 
nemws1Database AdministratorCommented:
-a was *once* mailx only, but most standard OSes today have it on the standard 'mail' program.
0
 
YZlatAuthor Commented:
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
 
YZlatAuthor Commented:
I tried using mailx command but got same error:

mailx: Not a recognized flag: a
0
 
mgobCommented:
Add the headers into your mail that I had posted above.
0
 
YZlatAuthor Commented:
where do I add those headers?
0
 
YZlatAuthor Commented:
I already have this in my html file

<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
0
 
mgobCommented:
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
 
YZlatAuthor Commented:
I can't really edit html file, it gets generated by sql plus
0
 
YZlatAuthor Commented:
Thanks for answering my original question. I will open new question for the rest
0
 
mgobCommented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 17
  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now