Solved

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

Posted on 2012-04-09
10
654 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

21 Experts available now in Live!

Get 1:1 Help Now