Solved

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

Posted on 2012-04-09
10
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

717 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