Solved

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

Posted on 2012-04-09
10
661 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

785 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