ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Send an entire directory of "documents" Zipped Via Email

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Send an entire directory of "documents" Zipped Via Email

    ===> must get Scott's BASE64 "stuff" and install first. then this will
    just compile... http://www.scottklement.com/base64.



    * This program will take all documents within a passed in folder on
    * the IFS and zip them into a zip file then email them to the user
    * that initiated the program.
    * Depending on flag RemoveItems documents and/or folders will be
    * removed once zipped up and emailed

    PHP Code:
       H BNDDIR('QC2LE':'BASE64'OPTION(*SRCSTMT:*NOSHOWCPY)
          *-------------------------------------------------------------------
          * 
    This program will take all documents within a passed in folder on
          
    the IFS and zip them into a zip file then email them to the user
          
    that initiated the program.
          * 
    Depending on flag RemoveItems documents and/or folders will be
          
    removed once zipped up and emailed
          
    *
          * 
    Before compiling:
          *   -- 
    This requires the BASE64 open source utility from
          
    *       http://www.scottklement.com/base64.
          
    *   -- make sure base64 utility has been compiled.
          *
          * 
    To Compile:
          *    
    CRTRPGMOD MODULE(jamielib/MSC46)
          *              
    SRCFILE(jamilib/source)
          *              
    DBGVIEW(*LIST)
          *
          *    
    CRTPGM PGM(jamielib/MSC46)
          *           
    MODULE(jamielib/MSC46)
          *           
    BNDSRVPGM(QTCP/QTMMSNDM)
          *
          *   
    Parameters
          
    *   ___________________________________________
          
    *   InAttachname  The name of the zip file that will hold *ALL
          
    *                   Documents in the passed in Directory.
          *
          *   
    InDirectory   - *ALL Documents in this Directory will be Zipped
          
    *
          *   
    InToUserName  Passed in username this will allow for emails
          
    *                   to be sent to individuals outside the company
          
    *
          *   
    InToUserEmail Passed in user email this to need to allow
          
    *                   emails to be sent outside the company
          
    *
          *   
    InSubject     This is the subject line on the email
          
    *
          *
          *   
    RemoveItems   flag to indicate what items to remove
          
    *                   *blanks nothing
          
    *                   "F"     folders (this will by default delete documents)
          *                   
    "D"     Documents
          
    *
          *-------------------------------------------------------------------
           
    //
           //  *entry plist
           //

         
    d MSC46           pr
         d  inAttachName                 50
         d  inDirectory                  50
         d  inToUserName                 50
         d  inToUserEmail                50
         d  inSubject                    50
         d  RemoveItems                   1

         d MSC46           pi
         d  inAttachName                 50
         d  inDirectory                  50
         d  inToUserName                 50
         d  inToUserEmail                50
         d  inSubject                    50
         d  RemoveItems                   1

          
    **********************************************************************
          *  
    Flags for use in open()
          *
          * 
    More than one can be used -- add them together.
          **********************************************************************
         
    d O_RDONLY        C                   1
         d O_WRONLY        C                   2
         d O_RDWR          C                   4
         d O_CREAT         C                   8
         d O_EXCL          C                   16
         d O_CCSID         C                   32
         d O_TRUNC         C                   64
         d O_APPEND        C                   256
         d O_SYNC          C                   1024
         d O_DSYNC         C                   2048
         d O_RSYNC         C                   4096
         d O_NOCTTY        C                   32768
         d O_SHARE_RDONLY  C                   65536
         d O_SHARE_WRONLY  C                   131072
         d O_SHARE_RDWR    C                   262144
         d O_SHARE_NONE    C                   524288
         d O_CODEPAGE      C                   8388608
         d O_TEXTDATA      C                   16777216
         d O_INHERITMODE   C                   134217728
         d O_LARGEFILE     C                   536870912

          
    **********************************************************************
          * 
    My own special MODE shortcuts for open() (instead of those above)
          **********************************************************************
         
    d M_RDONLY        C                   const(292)
         
    d M_RDWR          C                   const(438)
         
    d M_RWX           C                   const(511)

          *--------------------------------------------------------------------
          * 
    Close a file
          
    *
          * 
    int close(int fildes)
          *
          * 
    Note:  Because the same close() API is used for IFSsockets,
          *        and 
    pipesit's conditionally defined here.  If it's
          
    *        done the same in the sockets pipe /copy members,
          *        
    there will be no conflict.
          *--------------------------------------------------------------------
         
    d/if not defined(CLOSE_PROTOTYPE)
         
    d close           PR            10I 0 ExtProc('close')
         
    d  fildes                       10I 0 value
         d
    /define CLOSE_PROTOTYPE
         d
    /endif

          *--------------------------------------------------------------------
          * 
    Open a File
          
    *
          * 
    int open(const char *pathint oflag, . . .);
          *--------------------------------------------------------------------
         
    d open            PR            10I 0 ExtProc('open')
         
    d  path                           *   value options(*string)
         
    d  openflags                    10I 0 value
         d  mode                         10U 0 value options
    (*nopass)
         
    d  ccsid                        10U 0 value options(*nopass)
         
    d/if defined(*V5R2M0)
         
    d  txtcreatid                   10U 0 value options(*nopass)
         
    d/endif

          *--------------------------------------------------------------------
          * 
    Open a FileLarge File Enabled
          
    *
          * 
    int open64(const char *pathint oflag, . . .);
          *
          * 
    NOTEThis is identical to calling open(), except that the
          
    *       O_LARGEFILE flag is automatically supplied.
          *--------------------------------------------------------------------
         
    d open64          PR            10I 0 ExtProc('open64')
         
    d  filename                       *   value options(*string)
         
    d  openflags                    10I 0 value
         d  mode                         10U 0 value options
    (*nopass)
         
    d  codepage                     10U 0 value options(*nopass)

         
    d/if defined(*V5R2M0)
         
    d  txtcreatid                   10U 0 value options(*nopass)
         
    d/endif

          *--------------------------------------------------------------------
          * 
    Open a Directory
          
    *
          * 
    DIR *opendir(const char *dirname)
          *--------------------------------------------------------------------
         
    d opendir         PR              *   EXTPROC('opendir')
         
    d  dirname                        *   VALUE options(*string)

          *--------------------------------------------------------------------
          * 
    Read From a File
          
    *
          * 
    ssize_t read(int fildesvoid *buffersize_t bytes);
          *--------------------------------------------------------------------
         
    d read            PR            10I 0 ExtProc('read')
         
    d  fildes                       10i 0 value
         d  buf                            
    *   value
         d  bytes                        10U 0 value

          
    *--------------------------------------------------------------------
          * 
    Remove Link to File.  (Deletes Directory Entry for File, and if
          *    
    this was the last link to the file datathe file itself is
          
    *    also deleted)
          *
          * 
    int unlink(const char *path)
          *--------------------------------------------------------------------
         
    d unlink          PR            10I 0 ExtProc('unlink')
         
    d   path                          *   Value options(*string)

          *--------------------------------------------------------------------
          * 
    Write to a file
          
    *
          * 
    ssize_t write(int fildes, const void *bufsize_t bytes)
          *--------------------------------------------------------------------
         
    d write           PR            10I 0 ExtProc('write')
         
    d  fildes                       10i 0 value
         d  buf                            
    *   value
         d  bytes                        10U 0 value

          
    // end of ifsio copybook
          // start of sendmail copybook

         
    d QtmmSendMail    PR                  ExtProc('QtmmSendMail')
         
    d   FileName                   255A   const options(*varsize)
         
    d   FileNameLen                 10I 0 const
         
    d   MsgFrom                    256A   const options(*varsize)
         
    d   MsgFromLen                  10I 0 const
         
    d   RecipBuf                          likeds(ADDTO0100)
         
    d                                     dim(32767)
         
    d                                     options(*varsize)
         
    d   NumRecips                   10I 0 const
         
    d   ErrorCode                 8000A   options(*varsize)

         
    d ADDTO0100       ds                  qualified
         d                                     based
    (Template)
         
    d   NextOffset                  10I 0
         d   AddrLen                     10I 0
         d   AddrFormat                   8A
         d   DistType                    10I 0
         d   Reserved                    10I 0
         d   SmtpAddr                   256A

         d ADDR_NORMAL     C                   
    CONST(0)
         
    d ADDR_CC         C                   CONST(1)
         
    d ADDR_BCC        C                   CONST(2)

          
    // end of sendmail copybook

          
    *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          *  
    base64_encode:  Encode binary data using Base64 encoding
          
    *
          *       
    Input = (inputpointer to data to convert
          
    *    InputLen = (inputlength of data to convert
          
    *      Output = (outputpointer to memory to receive output
          
    *     OutSize = (inputsize of area to store output in
          
    *
          *  
    Returns length of encoded data
          
    *      data. If this value is greater than OutSize
          
    *      output may have been truncated.
          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         
    d base64_encode   PR            10U 0
         d   Input                         
    *   value
         d   InputLen                    10U 0 value
         d   Output                        
    *   value
         d   OutputSize                  10U 0 value

          
    *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          *  
    base64_inAlphabet(): Check if a given character is in the
          
    *                       base64 alphabet
          
    *
          *        
    Char = (inputcharacter to check
          
    *
          * 
    Returns *ON if it's in the alphabet
          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         D base64_inAlphabet...
         D                 PR             1N   extproc(*CL:'
    BASE64_INALPHABET')
         D   Char                         1a   value

         D $command        PR                  ExtPgm('
    QCMDEXC')
         D   cmd                      32702A   const options(*varsize)
         D   len                         15P 5 const

         D tmpnam          PR              *   extproc('
    _C_IFS_tmpnam')
         D   string                      39A   options(*omit)

         D ReportError     PR
         D MailDate        PR            31A


         D att             s             10I 0
         D attName         s             50A   varying
         D boundary        s             78A   varying
         D cmd             s            500A   varying
         D cmdlength       s             15  5
         D cmdstring       s            512
         D CRLF            c                   x'
    0d25'
         D data            s             54A
         d EmailBody       s             93A   DIM(11) CTDATA PERRCD(1)
         D encData         s             74A
         D encLen          s             10i 0
         D body            s            500A   varying
         D fd              s             10I 0
         D filename        s             50A   varying
         D fromName        s            100A   varying
         D fromAddr        s            300A   varying
         D header          s           2000A   varying
         d len             s             10i 0
         d Q               s              1    inz('''')
         D toName          s            100A   varying
         D toAddr          s            300A   varying
         D subject         s             80A   varying
         D tempAttach      s             50A   varying
         d wait            s              1A
         d x               s             10I 0

         d recip           ds                  likeds(ADDTO0100)
         d                                     dim(1)

         d NullError       ds
         d   BytesProv                   10I 0 inz(0)
         d   BytesAvail                  10I 0 inz(0)

          /free

              fromName = '
    My Company IT';
              fromAddr = '
    helpdesk@mycompany.com';

              toName   = %trim(InToUserName);
              toAddr   = %trim(InToUserEmail);
              subject  = %trim(InSubject);

              // This is the name of the attachment sent back to the user
              // This is a passed in parm (Required)
               attname  = %trim(inAttachname);

                //=======================================================
                // zip them up
                // STRQSH CMD('
    jar cfM zip/profit.zip zip/*.*')
                //=======================================================

                    cmdstring = 'STRQSH CMD(' + Q + 'jar cfM '  +
                    %trim(inDirectory) +
                    %trim(attname) +
                    '  ' + %trim(InDirectory) + '*.*' + Q + ')';

                    cmdlength = %len(%trim(cmdstring));
                    monitor;
                     $command(cmdstring:cmdlength);
                    on-error;
                    endmon;

                 // The name of the attached zip file
                    tempAttach = %trim(InDirectory) +
                                 %trim(attname);

              // ------------------------------------------
              // create a temporary file in the IFS.
              // mark that file as ccsid 819 (ISO 8859-1 ASCII)
              // ------------------------------------------

              filename = %str(tmpnam(*omit));
              unlink(filename);

              fd = open( filename
                       : O_CREAT+O_EXCL+O_WRONLY+O_CCSID
                       : M_RDWR
                       : 819 );
              if (fd = -1);
                 ReportError();
              endif;

              // ------------------------------------------
              // close file & reopen in text mode so that
              // data will be automatically translated
              // ------------------------------------------

              callp close(fd);
              fd = open( filename : O_WRONLY + O_TEXTDATA );
              if (fd = -1);
                 ReportError();
              endif;

              // ------------------------------------------
              //  build an e-mail header in a variable
              // ------------------------------------------
              Boundary = '-----------------------';

              header =
               'From: ' + fromName + ' <' + fromAddr + '>' + CRLF
              +'To: '   + toName   + ' <' + toAddr   + '>' + CRLF
              +'Date: ' + maildate() + CRLF
              +'Subject: ' + subject + CRLF
              +'MIME-Version: 1.0' + CRLF
              +'Content-Type: multipart/mixed;'
              +   ' boundary="' + Boundary + '"' + CRLF
              + CRLF
              + 'Your mail reader doesn''t support MIME!' + CRLF
              + CRLF;

              callp write(fd: %addr(header)+2: %len(header));

              // ------------------------------------------
              //  Insert the headers for the text part
              // ------------------------------------------

              body =
               '--' + boundary + CRLF
              +'Content-type: text/plain' + CRLF
              + CRLF;
              callp write(fd: %addr(body)+2: %len(body));

              // ------------------------------------------
              //  Copy the text part from the compile-time
              //  array to the IFS stream file
              // ------------------------------------------

              for x = 1 to %elem(emailbody);
                 body = %trimr(emailbody(x)) + CRLF;
                 callp write(fd: %addr(body)+2: %len(body));
              endfor;

              // ------------------------------------------
              //  Insert the headers for the CSV file
              // ------------------------------------------

              body =
               '--' + boundary + CRLF
              +'Content-Type: text/csv; name="' + attname + '"' + CRLF
              +'Content-Transfer-Encoding: base64' + CRLF
              +'Content-Disposition: attachment;'
              +    ' filename="' + attname + '"' + CRLF
              + CRLF;
              callp write(fd: %addr(body)+2: %len(body));

              // ------------------------------------------
              //  Read the attachment file, and base64
              //  encode it.  Write the results to the
              //  e-mail message.
              // ------------------------------------------

              att = open( tempAttach: O_RDONLY );
              if (att = -1);
                 ReportError();
              endif;
              unlink(tempAttach);       // this deletes the zipped file after sending

              dow '1';
                 len = read(att: %addr(data): %size(data));
                 if (len < 1);
                    leave;
                 endif;

                 enclen = base64_encode( %addr(data)
                                       : len
                                       : %addr(encdata)
                                       : %size(encdata)-2 );

                 %subst(encdata:enclen+1) = CRLF;
                 callp write(fd: %addr(encdata): enclen+2);
              enddo;

              callp close(att);

              // ------------------------------------------
              //  Finish the message & close the file
              // ------------------------------------------
              body = CRLF + '--' + boundary + '--' + CRLF;
              callp write(fd: %addr(body)+2: %len(body));
              callp close(fd);

              // ------------------------------------------
              //  Use the QtmmSendMail() API to send the
              //  IFS file via SMTP
              // ------------------------------------------

              recip(1).NextOffset = %size(ADDTO0100);
              recip(1).AddrFormat = 'ADDR0100';
              recip(1).DistType   = ADDR_NORMAL;
              recip(1).Reserved   = 0;
              recip(1).SmtpAddr   = toAddr;
              recip(1).AddrLen    = %len(toAddr);

              QtmmSendMail( FileName
                          : %len(FileName)
                          : fromAddr
                          : %len(fromAddr)
                          : recip
                          : %elem(recip)
                          : NullError );

              select;
              when removeItems = 'D';
               cmdstring = 'RmvLNK ' + Q + %trim(InDirectory) + '*.*' + Q;
               cmdlength = %len(%trim(cmdstring));
               $command(cmdstring:cmdlength);

              when removeItems = 'F';
               cmdstring = 'RmvLNK ' + Q + %trim(InDirectory) + '*.*' + Q;
               cmdlength = %len(%trim(cmdstring));
               $command(cmdstring:cmdlength);

               cmdstring = 'rmvdir ' + Q + %trim(InDirectory) + Q ;
               cmdlength = %len(%trim(cmdstring));
               $command(cmdstring:cmdlength);

              endsl;

              *inlr = *on;
          /end-free

          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          * ReportError():  Send an escape message explaining any errors
          *                 that occurred.
          *
          *  This function requires binding directory QC2LE in order
          *  to access the __errno() function.
          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         p ReportError     B
         d ReportError     PI

         d get_errno       PR              *   ExtProc('__errno')
         d ptrToErrno      s               *
         d errno           s             10I 0 based(ptrToErrno)

         d QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
         d   MessageID                    7A   Const
         d   QualMsgF                    20A   Const
         d   MsgData                      1A   Const
         d   MsgDtaLen                   10I 0 Const
         d   MsgType                     10A   Const
         d   CallStkEnt                  10A   Const
         d   CallStkCnt                  10I 0 Const
         d   MessageKey                   4A
         d   ErrorCode                 8192A   options(*varsize)

         d ErrorCode       DS                  qualified
         d  BytesProv              1      4I 0 inz(0)
         d  BytesAvail             5      8I 0 inz(0)

         d MsgKey          S              4A
         d MsgID           s              7A

          /free

             ptrToErrno = get_errno();
             MsgID = 'CPE' + %char(errno);

             QMHSNDPM( MsgID
                     : 'QCPFMSG   *LIBL'
                     : ' '
                     : 0
                     : '*ESCAPE'
                     : '*PGMBDY'
                     : 1
                     : MsgKey
                     : ErrorCode         );

          /end-free
         p                 E

          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          *  MailDate():  Returns the current date, formatted for use
          *               in an e-mail message.
          *
          *     For example:  'Sat, 23 Oct 2004 14:42:06 -0500'
          *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         p MailDate        B
         d MailDate        PI            31A

         d CEELOCT         PR                  opdesc
         d   Lilian                      10I 0
         d   Seconds                      8F
         d   Gregorian                   23A
         d   fc                          12A   options(*omit)

         d CEEUTCO         PR                  opdesc
         d   Hours                       10I 0
         d   Minutes                     10I 0
         d   Seconds                      8F
         d   fc                          12A   options(*omit)

         d CEEDATM         PR                  opdesc
         d   input_secs                   8F   const
         d   date_format                 80A   const options(*varsize)
         d   char_date                   80A   options(*varsize)
         d   feedback                    12A   options(*omit)

         d rfc2822         c                   'Www, DD Mmm YYYY HH:MI:SS'
         d junk1           s              8F
         d junk2           s             10I 0
         d junk3           s             23A
         d hours           s             10I 0
         d mins            s             10I 0
         d tz_hours        s              2P 0
         d tz_mins         s              2P 0
         d tz              s              5A   varying
         d CurTime         s              8F
         d Temp            s             25A

          /free

             //
             //  Calculate the Timezone in format '+0000', for example
             //    CST should show up as '-0600'
             //

             CEEUTCO(hours: mins: junk1: *omit);
             tz_hours = %abs(hours);
             tz_mins = mins;

             if (hours < 0);
                tz = '-';
             else;
                tz = '+';
             endif;

             tz += %editc(tz_hours:'X') + %editc(tz_mins:'X');

             //
             //  Get the current time and convert it to the format
             //    specified for e-mail in RFC 2822
             //

             CEELOCT(junk2: CurTime: junk3: *omit);
             CEEDATM(CurTime: rfc2822: Temp: *omit);

             return Temp + ' ' + tz;

          /end-free
         P                 E

    ** ------------------ Email Body ----------------------

    This email and any files transmitted with it are confidential and intended solely for the use
    of the individual or entity to whom they are addressed. If you have received this email in
    error, please notify the system manager. This message contains confidential information
    and is intended only for the individual named.  If you are not the named addressee, you
    should not disseminate, distribute or copy this email. Please notify the sender immediately
    by email if you have received this email by mistake and delete this email from your system.
    If you are not the intended recipient, you are notified that disclosing, copying, distributing
    or taking any action in reliance on the contents of this information is strictly prohibited. 
    Attached Files
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com
Working...
X