Solved

How to write messages to an email address in Unix bash shell script?

Posted on 2012-04-09
10
658 Views
Last Modified: 2012-06-27
I am trying to write a few lines with messages to an email address in Unix bash shell script, how can I do that?
Note: From my testing, I can see that my email is being sent to the proper email address and I also see the subject line, but there are no messages in the body of the email.  

Please refer to my script below:

DATESTAMP=$(date "+%Y-%m-%d")
TIMESTAMP=$(date "+%H:%M:%S")
mailSubject="ERROR: Madule10 - FAILED, REVIEW Program LOG"
mymail="mail@yahoo.com"

    mailx -s $mailSubject $mymail 
    echo "----------------------------------------------------------------"
     echo "Module10 Update Program"
     echo "Date:   $DATESTAMP    Time:  $TIMESTAMP"
     echo "ERR Msg: Module10 failed review program log"
     echo "----------------------------------------------------------------"

Open in new window

Any comments or suggestions will be very appreciated!
Thank you!
0
Comment
Question by:labradorchik
  • 6
  • 4
10 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37824141
Two ways:

1)

  (  echo "----------------------------------------------------------------"
      echo "Module10 Update Program"
      echo "Date:   $DATESTAMP    Time:  $TIMESTAMP"
      echo "ERR Msg: Module10 failed review program log"
      echo "----------------------------------------------------------------"  ) | mailx -s $mailSubject $mymail  
   
2)

cat <<EOF | mailx -s $mailSubject $mymail
----------------------------------------------------------------
Module10 Update Program
Date:   $DATESTAMP    Time:  $TIMESTAMP
ERR Msg: Module10 failed review program log
----------------------------------------------------------------
EOF
0
 

Author Comment

by:labradorchik
ID: 37824277
Big thank you!!
I tried your first way and it worked fine!! I tried to work your second way, but I am keep getting an error for the first line:  
error: unexpected end of file

Since your first way is working fine, - I will use it.
Thank you gain!!!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37824308
Please take note that the terminating "EOF" must start in the very first column of its line
and that no trailing characters are allowed, not even  whitespace characters!

Thx for the points!

wmp
0
 

Author Comment

by:labradorchik
ID: 37824411
OK, I will try again.
Thank you!!
0
 

Author Comment

by:labradorchik
ID: 37824571
I am just curious; by any chance, do you know how to include/attach a file to that email from the Unix Bash script?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37824862
Try this:

  (  echo "----------------------------------------------------------------"
      echo "Module10 Update Program"
      echo "Date:   $DATESTAMP    Time:  $TIMESTAMP"
      echo "ERR Msg: Module10 failed review program log"
      echo "----------------------------------------------------------------"  
      uuencode /file/to/attach attachmentname.txt ) | mailx -s $mailSubject $mymail  

Please note that not every email client (reader) can handle such attachments.
If your email reader is filetype aware (like Windows) take care to choose the correct filetype for the attachment (.txt in my example).
0
 

Author Comment

by:labradorchik
ID: 37825140
Just tried your way, but I am get this error:
 syntax error: unexpected end of file

Please see my script:
DATESTAMP=$(date "+%Y-%m-%d")
TIMESTAMP=$(date "+%H:%M:%S")
mailSubject="ERROR: Madule10 - FAILED, REVIEW Program LOG"
mymail="mail@yahoo.com"
mydir=/year/month/week/day


     (echo "----------------------------------------------------------------"
      echo "Module10 Update Program"
      echo "Date:   $DATESTAMP    Time:  $TIMESTAMP"
      echo "ERR Msg: Module10 failed review program log"
      echo "----------------------------------------------------------------"  
      uuencode $mydir/sasprog.log) | mailx -s $mailSubject $mymail  

Open in new window

Did I place my directory correctly?
0
 

Author Comment

by:labradorchik
ID: 37825954
It is all my fault, I misspelled my directory name... Script sends emails now, but my attachment does not open. I can open my email and can I see the attached .txt file, but when I try to open the attachment it appears to be empty.

Note: there is no script in the .txt file, just one sentence for testing purposes. Also, I am using Unix bash script on Windows and Lotus iNotes for my email. I always type my script in the Unix bash shell, - not in the windows, so there is no transferring involved.

Do you know if this command may for some other Unix shell and not for bash shell?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37826610
uuencode expects two parameters - the name of the file to encode and the name to place into the header of the encoded data.

uuencode $mydir/sasprog.log sasprog.log )  | mailx -s $mailSubject $mymail  

It might well be that yout mail reader will display the whole file in one line - so if you don't scroll to the right you would see just the first few bytes which might be whitespace.
If this is your issue please let me know - we'll find a way.

wmp
0
 

Author Comment

by:labradorchik
ID: 37838517
Works great!! Thank you!! :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 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

12 Experts available now in Live!

Get 1:1 Help Now