Emailing File in Kornshell Script

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Emailing File in Kornshell Script

  • Creator
    Topic
  • #49589
    Ariba Jones
    Participant

      Does anyone have a kornshell script that emails a .txt file to the receiving system?  I need to take a .txt file that I receive via ftp and email it to the receiving system.  I have never written my own kornshell script.  Any help would be appreciated.

      Thanks,

      Ariba

    Viewing 19 reply threads
    • Author
      Replies
      • #62615
        John Hamilton
        Participant

          Over all sending mail is pretty easy is you system is setup.

          Very simple scrip to send a file.

          #!/bin/ksh

          mail -s “Subect With Space ” john.hamilton@Someaddress.com  < /Full_path/+filename. Hope this helps.

        • #62616
          Ariba Jones
          Participant

            John,

            Thanks for this information.  I do already send emails to people to let them know that files are out on their system. I just didn’t know how to attach an actual file to that email.  This does look very similar to my email scripts already setup.  I didn’t write them, but I definitely can follow them.  I know I can do this.  

            Thanks so much for your help. I will let you know how it goes.

            Ariba

          • #62617
            Huy Tran
            Participant

              Ariba,

              Why not use Cloverleaf to perform this task. I’d set up and inbound thread to pick up the file and an outbound thread to route it raw to the downstream system via TCP/IP.  If the downstream system can’t open a socket for TCP/IP then I’d opt for FTP instead. I think it’ll be easier to get messages from archived SMAT for support (resend) down the road.

              I only use email to send alerts to apps support analysts about their systems but never use it to send files.  Also, what if the file is too big…check the size limit on your organization email system.

              Sorry I know this is not much help to you.  Good luck!

            • #62618
              Ariba Jones
              Participant

                Huy,

                You are correct.  I need to check on the file size for this.

                I thought I would just set this up as an outbound tcp/ip thread, but when the receiving system said they needed me to email the file to them, I started looking into the emailing thing.  One of our network guys actually mentioned that I should be able to write a script to email this file.  I’ll double check with him about the email file sizes though.  

                If I setup the outbound thread as ftp, can I also setup the thread to email the file?  I didn’t think that was possible.  Maybe I am misunderstanding what you are saying.

                Thanks,

                Ariba

              • #62619
                Michael Hertel
                Participant

                  Here’s how we attach a file in AIX 5.3 land…(in a tcl proc)

                  if { ([file exists $log] == 0) && ([file exists $HciSiteDir/archive/$logname$yesterday.log] == 1)} {

                • #62620
                  John Hamilton
                  Participant

                    There is a product free ware. Called Mutt, that can send files as an attachment rather then the body of the text. I use it for several things.

                    That may be more to what you are looking for.

                    http://hep1.snu.ac.kr/pub/computing/doc/mutt/

                  • #62621

                    Here’s a block of code from that does what I think you need to do.

                    Code:

                       subject=”$2 – Error Database: $lines lines”
                       echo “To:  me@there.org” > $mailfile
                       echo “Reply-To:  me@there.org” >> $mailfile
                       echo “CC:  me@there.org” >> $mailfile
                       echo “Subject:  $subject” >> $mailfile
                       echo “” >> $mailfile
                       echo “ATTENTION CLOVERLEAF DEVELOPERS:” >> $mailfile
                       echo “” >> $mailfile
                       echo “Cloverleaf Interface Engine Error DB Results.” >> $mailfile
                       echo “Please review the following file for more information:” >> $mailfile
                       echo “=============================================================================” >> $mailfile
                       echo “The $hcisite error database contains $lines lines, see: $FILEDIR/$today.txt.” >> $mailfile
                       echo “=============================================================================” >> $mailfile
                       echo “n” >> $mailfile
                       echo “This email was generated by: $0” >> $mailfile

                       /usr/sbin/sendmail -fme@there.org -t < $mailfile >/dev/null
                       rm $mailfile

                    -- Max Drown (Infor)

                  • #62622
                    David Barr
                    Participant

                      Here’s a Perl script that will send a file as an attachment.  To use the script, you need to install the “Mail::Sender::Easy” module.  You can run it from the command line (or a ksh script) by typing

                      Code:

                      script.pl -f file.txt -s “message subject” someone@somewhere.org <

                      The “-f” argument is for a file to attach.  The standard input to the script will also be included as the body of the message.

                      Code:

                      #!/usr/local/bin/perl
                      use Mail::Sender::Easy qw(email);
                      use vars qw/ %opt /;
                      use Getopt::Std;

                      # accept -s as an arg for subject
                      # accept -f as an arg for file attachment
                      my $opt_string = ‘f:s:’;
                      getopts( “$opt_string”, %opt );

                      #store args in a hash for calling email function
                      my %args;

                      #set mail server and From: address
                      $args{from} = ‘myaddress@medstar.net’;
                      $args{smtp} = ‘smtp01.medstar.net’;

                      #read message from stdin
                      @lines = ;
                      $args{’_text’} = join(””, @lines);

                      #turn on debugging
                      #$args{debug} = ‘-‘;

                      #set subject
                      $args{subject} = “”;
                      if($opt{s}) {
                         $args{subject} = $opt{s};
                      }

                      if($opt{f}) {
                         $args{_attachments} = { $opt{f} => { file => $opt{f} } };
                      }

                      # process additional args as recipients
                      $args{to} = “”;
                      for(@ARGV) {
                         if ($args{to} eq “”) {
                             $args{to} = $_;
                         } else {
                             $args{to} = $args{to} . “,” . $_;
                         }
                      }

                      email({%args}) or die “email() failed: $@”;

                    • #62623
                      Glenn Friedenreich
                      Participant

                        Here’s still another way, using ksh:

                        #!/usr/bin/ksh

                        # ’emailFile’ – email file attachment to Outlook

                        # usage:

                        #    emailFile {fileName} {emailAddress} [{optional subject}] [{optional body}]

                        SCRIPTDIR=/hciScripts

                        SCRATCHDIR=$SCRIPTDIR/tmp

                        if $# < 2; then

                          print “nYou need at least 2 parameters: the file name and the email address.nA 3rd parameter, ‘subject text’, is optionalnA 4th parameter, ‘body text’, is optionalnExiting”

                          exit 1

                        fi

                        # get parameters

                        fileName=$1

                        emailAddress=$2

                        if $# > 2; then

                          subject=$3

                        else

                          subject=”Here’s a file for you from Cloverleaf”

                        fi

                        if $# > 3; then

                          bodyText=$4

                        else

                          bodyText=”Your file is attached.”

                        fi

                        # first convert to DOS delimter format

                        cp $fileName $fileName.txt

                        $SCRIPTDIR/unixToDos $fileName.txt

                        # uuencode the file

                        /bin/uuencode $fileName.txt $(/bin/basename $1).txt > $SCRATCHDIR/emailAttachment.tmp

                        cd $SCRATCHDIR

                        print $bodyText > emailBodyText.tmp

                        /bin/cat emailBodyText.tmp emailAttachment.tmp > emailData.tmp

                        /bin/mail -s “$subject” “$emailAddress” < emailData.tmp rm -f emailData.tmp emailBodyText.tmp emailAttachment.tmp cd – 1>/dev/null

                        rm -f $fileName.txt

                        exit 0

                        ======================

                        Here’s the unixToDos script used by the above:

                        #!/usr/bin/ksh

                        # ‘unixToDos’- convert from UNIX (0a) to DOS (0d0a) line terminators

                        # usage: unixToDos {fileName}

                        # This script converts a file in place

                        #   (in other words, the output file replaces the input file)

                        # UNIX-style line-feed (hex 0a) end-of-line terminators are changed to

                        #   DOS-style carriage-return line-feed (hex 0d0a) end-of-line terminators

                        fileName=$1

                        /hciScripts/bin/perl -i -pe ‘s/n/rn/g’ $fileName

                      • #62624
                        Ariba Jones
                        Participant

                          Thanks for all of the suggestions. I will look into these and try to find the one that best suits my need (and abilities).

                          Thanks,

                          Ariba

                        • #62625
                          Chris Brossette
                          Participant

                            Here’s my take on it….sends the file as an attachment….and ftp’s the file to a server….

                            #

                            # SET PATH

                            #

                            PATH=/hbo/jaanew:/hbo/jaasav:/usr/bin:/etc:/usr/sbin:/usr/ucb:/home/hci/bin:/usr/bin/X11:/sbin:.:/usr/local/bin:/usr/local/scripts

                            #echo “jaa_adt_copy”

                            export PATH

                            ALL=”cbrossette@mbhs.org mboyd@mbhs.org rclower@mbhs.org

                            S=”JAA ADT”

                            #

                            VALID=`ping -c 1 -q 10.49.56.71 | wc -l`

                            if [ $VALID -eq 0 ]; then

                            echo `date +%H:%M` > /tmp/timestamp

                            mail -s “JAA FTP Transfer Failed” cbrossette@notes_bhs < /tmp/timestamp        exit fi echo “open 10.49.56.71” > /tmp/ftptmp.scr

                            echo “user jaaftp jaa1ftp” >> /tmp/ftptmp.scr

                            echo “close” >> /tmp/ftptmp.scr

                            ftp -inv < /tmp/ftptmp.scr > /tmp/ftp_out

                            FTPSTAT=`grep “Login failed” /tmp/ftp_out | wc -l`

                            if [ $FTPSTAT -eq 1 ]; then

                            echo `date +%H:%M` > /tmp/timestamp

                                   mail -s “JAA FTP Login Failed” cbrossette@notes_bhs < /tmp/timestamp        exit fi # # copy the file to jaasav # cd /hbo/jaanew JAAFILE=”jaa”`date +%y%m%d`”.txt” JAALINE=`wc -l $JAAFILE` JAALINECNT=`cat $JAAFILE | wc -l` if [ $JAALINECNT -eq 0 ]; then # echo `date +%H:%M` > /tmp/timestamp

                            #       mail -s “JAA No messages” cbrossette@notes_bhs < /tmp/timestamp        exit fi NEWFILE=’/hbo/jaanew/mail_jaa’ echo “From: star_root” >$NEWFILE

                            echo “To: $ALL” >>$NEWFILE

                            echo “Subject: $S” >>$NEWFILE

                            echo “MIME-Version: 1.0” >>$NEWFILE

                            echo ‘Content-Type: Multipart/Mixed; boundary=”~-~-~-~-~”‘ >>$NEWFILE

                            echo “” >>$NEWFILE

                            echo “–~-~-~-~-~” >>$NEWFILE

                            echo “Content-type: Application/text; name=$JAAFILE” >>$NEWFILE

                            echo “Content-disposition: attachment; filename=`basename $JAAFILE`” >>$NEWFILE

                            echo “Content-transfer-encoding: 7BIT” >>$NEWFILE

                            echo “” >>$NEWFILE

                            cat $JAAFILE >>$NEWFILE

                            sendmail -t <$NEWFILE rm $NEWFILE # cp $JAAFILE /hbo/jaasav echo “JAA ADT file ( $JAAFILE )has # of lines in file: $JAALINE ” > /tmp/chgmsg

                            #

                            #

                            # ftp the file to the bhs002764

                            #

                            echo “open 10.49.56.71” > ftptmp.scr

                            echo “user jaaftp jaa1ftp” >> ftptmp.scr

                            echo “put $JAAFILE” >> ftptmp.scr

                            echo “close” >> ftptmp.scr

                            ftp -inv < ftptmp.scr > http://ftp.log

                            rm $JAAFILE

                          • #62626
                            Ariba Jones
                            Participant

                              Thanks for replying to this, Chris.  I will also look at your suggestion.

                              Thanks,

                              Ariba

                            • #62627
                              Ariba Jones
                              Participant

                                I got many replies on this topic.  I have gotten back to working on this and have a problem.

                                I will receive a file via tcp/ip (I previously stated that it would be via ftp, but that changed).  I need to make a copy of that file and save in a backup directory with the date stamp.  I then need to email this file to multiple email addresses as an attachment. I need to specify a particular subject and also specific information in the body of the email.

                                I have tried to use a script I received for the email part, but I am having an issue.  I also have a kornshell script to put a copy of this file in a backup directory with the date stamp.

                                I think I have too much going on with this.  Is there an easier or simpler way for me to accomplish these two things…1) make a backup copy of the file with a date stamp and 2) email the file as an attachment to multiple addresses?

                                My inbound thread is a tcp/ip.  My outbound thread is fileset-Local.

                                Here is the error I receive.

                                Tc[b]l error:

                                       msgId   = message0

                                       proc    = ’email_infogen’

                                       args    = ”

                                       result  = ‘/quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: .txt:  not found.

                                The flags you gave make no sense since you’re not sending mail.’

                                       errorInfo: ‘

                                /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: .txt:  not found.

                                The flags you gave make no sense since you’re not sending mail.

                                   while executing

                                “exec /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh”

                                   (“run” arm line 9)

                                   invoked from within

                                “switc[/b]

                                This is my email script:

                                #!/usr/bin/ksh

                                #list of addresses to send the message to

                                set emaillist “emailadd1@something.com,emailadd2@something.com,emailadd3@something.com

                                #body of the email message

                                set msg “Please import GAFILE.”

                                #file to be attached to the email

                                set filename “LOGAIMPORT.txt”

                                set tmpfile “/lawson_backup/lawson/infogenesisout/test/$filename”

                                #unix commands to use

                                set ux2dos “/usr/bin/ux2dos”

                                set uuencode “/usr/bin/uuencode”

                                set mailx “/usr/bin/mailx”

                                exec $ux2dos $tmpfile | $uuencode $filename.txt | mailx -s “Your file is readyn$msg” $emaillist

                                Can anyone tell me what is wrong with this script?

                                Thanks in advance.

                                Ariba Jones

                              • #62628
                                Gary Atkinson
                                Participant

                                  Ariba-

                                  I have a similar set-up, but I use UPoC in the outbound thread.  How are you calling your shell script?

                                  Gary

                                • #62629
                                  Greg Eriksen
                                  Participant

                                    I don’t know if this will make your script work, but I notice that you are setting a variable to a path:

                                    set mailx “/usr/bin/mailx”

                                    So don’t you need to put a dollar sign, $, in front of “mailx” in your exec line like this?:

                                    exec $ux2dos $tmpfile | $uuencode $filename.txt | $mailx -s “Your file is readyn$msg” $emaillist

                                  • #62630
                                    Ariba Jones
                                    Participant

                                      Gary,

                                      I call my shell script (to make a backup copy of the file) through a crontab entry.  I have my email script setup as a kornshell script that gets called in a tcl. I have this tcl attached to the Outbound tab of my outbound thread on the TPS Outbound Data field.  

                                      I have a feeling I have too much going on here.  I think I just need to start over with this logic completely.

                                      Are you doing something like this on your end?

                                      Thanks,

                                      Ariba

                                    • #62631
                                      Ariba Jones
                                      Participant

                                        Greg,

                                        I made the change that you suggested and now I get a different error when my script runs.

                                        Tcl error:

                                               msgId   = message0

                                               proc    = ’email_infogen’

                                               args    = ”

                                               result  = ‘/quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: .txt:  not found.

                                        /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: -s:  not found.’

                                               errorInfo: ‘

                                        /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: .txt:  not found.

                                        /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: -s:  not found.

                                           while executing

                                        “exec /quovadx/qdx5.2/integrator/sbin/email_infogen.ksh”

                                           (“run” arm line 9)

                                           invoked from

                                        I think I need to possibly abandon this script and try a different approach (and/or script).  I just don’t know where to go from here.

                                        Thanks,

                                        Ariba

                                      • #62632
                                        Chris Williams
                                        Participant

                                          Ariba,

                                          The major problem is that you are using tcl syntax to write a ksh script. The two languages are not interchangeable.

                                          The error message gives you a clue:

                                               result  = ‘/quovadx/qdx5.2/integrator/sbin/email_infogen.ksh[17]: .txt:  not found

                                          It says “.txt” is not found.

                                          Your only reference to “.txt” is “$filename.txt”.

                                          “set” under ksh does not do what you think it does.

                                          filename does not exist as a variable.

                                          However, this would probably work if you changed the beginning of the script to run under tcl rather than ksh.

                                        • #62633
                                          Ariba Jones
                                          Participant

                                            Chris, I am not sure what you mean when you say to change the beginning of the script to be tcl and not ksh.  I am not good at tcl, so, I don’t know where to start.  

                                            Thanks,

                                            Ariba

                                          • #62634
                                            Tom Rioux
                                            Participant

                                              What Chris means is that instead of the following at the beginning of your script:

                                              #!/usr/bin/ksh

                                              Change it to something like this:

                                              #!/qdx/qdx5.2/integrator/bin/tcl

                                              Of course, the root path may be different on your site.

                                              Hope this helps…

                                              Tom Rioux

                                          Viewing 19 reply threads
                                          • The forum ‘Cloverleaf’ is closed to new topics and replies.