Using sendmail utility in K Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sendmail utility in K Shell script
# 1  
Old 06-21-2010
Using sendmail utility in K Shell script

Hi,
I am new to shell scripting and thus any help will be highly appreciated. I need to write a K shell script where in the email sending feature should be handled by sendmail utility and I have come up with the following :

Code:
#!/usr/bin/ksh
echo "This is a test mailest mail" | /usr/lib/sendmail -f abc.xyz@family.org xyz.abc@family.org

However, I need to alter the shell so that it can take the From field, To field and any file attachment as parameter.
Can anyone kindly help ?

Thanks a lot in advance....

Last edited by pludi; 06-22-2010 at 01:40 AM.. Reason: code tags, please...
# 2  
Old 06-21-2010
Try something like this.


Code:
  (
    echo "To: $1"
    echo "From: $2"
    echo "Subject: $3"
    echo "$4"                    ***
  ) | /PATH-TO/sendmail -t

Replace PATH-TO with the real path.

*** you could cat a file `cat $filename` or echo another variable echo "$variable" instead of having to type it out. Any one of the args could be variables.

I.E.
mailto="user@domain.com"
from="me@mydomain.com"
subject="Cool email"
body="this is the body of the email and can go on from quite some time"

Code:
  (
    echo "To: $mailto"
    echo "From: $from"
    echo "Subject: $subject"
    echo "$body"
  )  |  /PATH-TO/sendmail -t

# 3  
Old 06-22-2010
Hi

Hi mph,
Thanks a lot for the reply. The entire code looks like :

Code:
#!/usr/bin/ksh
  (
    echo "To: $mailto"
    echo "From: $from"
    echo "Subject: $subject"
    echo "$body"
  )  |  /usr/lib/sendmail -t

However, when I tried to run the same with the following command line
./test.ksh abc@xyz.com xyz@abc.com subject body , it gave an error as follows :
No recipient addresses found in header
Any idea ?
# 4  
Old 06-22-2010
You are not setting the local variables to the command line argument values. Try this:
Code:
#!/usr/bin/ksh
mailto=$1
from=$2
subject=$3
body=$4

  (
    echo "To: $mailto"
    echo "From: $from"
    echo "Subject: $subject"
    echo "$body"
  )  |  /usr/lib/sendmail -t

# 5  
Old 06-23-2010
Hi

Hi,
Thanks a lot for the same. Therefore, I will be running the shell script with the following command.

./new.ksh abc@xyz.com support@xyz.com subject body
[new.ksh is the script name]
Is this correct ?
Additionally, what does -t stand for ? For every parameter like To field, Cc field, body, subject, do I need to specify the options like -f, -t etc ?
I 'm afraid I don't know all the options that sendmail accepts in command line and how to use them.
Please reply...

---------- Post updated at 11:13 AM ---------- Previous update was at 10:21 AM ----------

Hi,
I wrote the script like as follows :
Code:
#/usr/bin/sh
subject=$1;
body=$2;
from=$3;
to=$4;
cc=$5;
sendmail_path="/usr/lib/sendmail";
send=`echo $subject| $body | $sendmail_path -f $from -t $to`;
echo $send is the return code;

While running the script, I am using the following at the command line:
./test.sh Sub Body support@abc.com support@xyz.com
It is running fine, however the subject line or the body is empty ( I expected at the subject line & body the word Sub and Body will be displayed Smilie )
Could you please let me know the following ?
  • If I need to add Cc, signature and attachment, what change needs to done in the code ?
  • Consequently, while running the script, what should be the exact command line so that whatever I write agaist subject field, body field and others will be populated accordingly ?
Thanks a lot in advance,
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with integrating shell script with sendmail

Hi friends, I want to converting a task ,of making few words in my task mail bold and underlined sometimes, automated through sendmail. I have never used sendmail/mailx before. What i want is my ouput which looks like below: +++++++++++++++ DETAILS: sqlid:6mbiosdfsdff parsing_schema_name:... (1 Reply)
Discussion started by: kunwar
1 Replies

2. UNIX for Advanced & Expert Users

which shell command or utility will call the net device driver?

Hi, all: Which shell command or utility will call the open(), close(), hard_start_xmit(), rtl8139_rx(), or interrupt() functions in a network device driver I am debugging? Can "ping", "ftp", "scp", or "internet browser" finally call them? Best regards! li, kunlun (0 Replies)
Discussion started by: liklstar
0 Replies

3. Shell Programming and Scripting

Sendmail Utility

Hi. Question from a novice. I'd like to have a unix script that sends me an email message, but I have no clue on the syntax. I do know this works from my mailtest.sh script: /usr/lib/sendmail steve@me.com < /dev/null This sends me a blank email. I'm hoping someone can help me to build... (3 Replies)
Discussion started by: buechler66
3 Replies

4. Shell Programming and Scripting

I need help to run this utility from a shell script

I need some help to run this executable from within a shell script. The Script is run the following way at the command prompt. $rateupd Main Menu --Standard Output ----------- --Standard Output 1. - Update Rate --Standard Output 2. - Exit. --Standard Output Enter Selection: 1 --User... (5 Replies)
Discussion started by: rajeeb_d
5 Replies

5. UNIX for Dummies Questions & Answers

Need help on SCRIPT(1M) utility

Hi All, I need to do a lot of manual entries at shell prompt. So to collect the logs(each command fired in that session, i use "SCRIPT(1) : make typescript of terminal session" this is kool,but the problem here is that it saves the linefeed, and backspaces along with the commands in the log... (1 Reply)
Discussion started by: amit4g
1 Replies

6. UNIX for Dummies Questions & Answers

can't find my NEDIT utility after shell change

Hello, My System Admin. just switched me from KSH to BASH and something happened to my "nedit" utility! Does anyone know how to reactivate nedit by anychance? Thanks very much! BobK (2 Replies)
Discussion started by: bobk544
2 Replies

7. Shell Programming and Scripting

How to write DB2 Load Utility in Shell script

Hi, I am a beginner. I need an example of using DB2 Load Utility in Shell script. I appreciate if anyone could help me about it. Thanks, Paris (0 Replies)
Discussion started by: parisbeginner
0 Replies

8. UNIX Desktop Questions & Answers

Recommendations for good shell utility to resize JPGs?

Not sure if this is the right place to be posting this. If not, let me know where it fits. I am running RedHat Linux 8.0. I've recently acquired a Sony Mavica digital camera. In short, this thing is awesome (uses cd-rw!). I have been taking high quality images, but I now have the need to try... (2 Replies)
Discussion started by: deckard
2 Replies

9. Shell Programming and Scripting

using FTP related commands in nawk utility in shell programming

I am facing problem while accesing FTP related commands in nawk code in bourne shell programming.I can able to connect to remote machine, but i can't able to get files from that machine. Please help me in this.If you send code along with the solution, then it will be usefull for me. (4 Replies)
Discussion started by: nrsekhar
4 Replies
Login or Register to Ask a Question