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
Solved

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

Posted on 2012-04-09
10
662 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

829 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