*====================================================================                         
      *  ILE-RPG pgm EMLDST                                                                         
      *  E-mail a message to a distribution list                                                    
      *  Invoked from command EMLDST (See MMAIL/SAMPLECMD mbr EMLDST)                               
      *                                                                                             
      *  After compiling this module,                                                               
      *  create the programn as follow:                                                             
      *  CRTPGM PGM(MMAIL/EMLDST) MODULE(MMAIL/EMLDST MMAIL/EMLDSTA                                 
      *         MMAIL/EMLDSTB) ACTGRP(*CALLER)                                                      
      *                                                                                             
      *====================================================================                         
      /copy *LIBL/qrpglesrc,hspecs                                                                  
      /copy *LIBL/qrpglesrc,hspecsbnd                                                               
      * File SENDERIDS- Used to identify the sender of the message                                  
     FSENDERIDS if   e           k disk    usropn                                                   
      /copy *LIBL/qrpglesrc,prototypeb                                                              
      /copy *LIBL/qrpglesrc,mailproto                                                               
      /copy *LIBL/qrpglesrc,usec                                                                    
      /copy *LIBL/qrpglesrc,variables3                                                              
      * Main procedure Prototype and Interface                                                      
     D EMLDST          PR                                                                           
     D SenderID                      20                                                             
     D Subject                       70                                                             
     D TextStmf                     256                                                             
     D DataLib                       10                                                             
     D DstL                           4                                                             
     D DstType                        1                                                             
     D AttachStmf                   256                                                             
     D AttachType                    10                                                             
     D BMode                          4                                                             
     D EMLDST          PI                                                                           
     D SenderID                      20                                                             
     D Subject                       70                                                             
     D TextStmf                     256                                                             
     D DataLib                       10                                                             
     D DstL                           4                                                             
     D DstType                        1                                                             
     D AttachStmf                   256                                                             
     D AttachType                    10                                                             
     D BMode                          4                                                             
      *                                                                                             
     D myAttachType    s             50                                                             
     D exeEnv          s              1                                                             
     D IfsFileNam      s           1024                                                             
     D IfsFileSiz      s             10i 0                                                          
     D IfsCrtStamp     s               z                                                            
     D cmd             s            500                                                             
     D r               s             10i 0                                                          
     D module          s             10                                                             
     D topgmq          s              5    inz('*PRV')                                              
     D msgtype         s              7    inz('*INFO')                                             
     D msg             s            512                                                             
     D NbrReceivers    s             10i 0                                                          
      *=====================================================================                        
     C     EmlDstList    plist                                                                      
     C                   parm                    SenderID                                           
     C                   parm                    Subject                                            
     C                   parm                    TextStmf                                           
     C                   parm                    DataLib                                            
     C                   parm                    DstL                                               
     C                   parm                    AttachStmf                                         
     C                   parm                    myAttachtype                                       
     C                   parm                    NbrReceivers                                       
      *=====================================================================                        
     C                   eval      rc=docmd('chgjob log(4 0 *seclvl)')                              
      *                                                                                             
      * Rephrase the AttachType                                                                     
     C                   select                                                                     
     C                   when      AttachType='*TEXT'                                               
     C                   eval      myAttachType='text/plain'                                        
     C                   when      AttachType='*HTML'                                               
     C                   eval      myAttachType='text/html'                                         
     C                   when      AttachType='*JPG'                                                
     C                   eval      myAttachType='image/jpg'                                         
     C                   when      AttachType='*PDF'                                                
     C                   eval      myAttachType='application/pdf'                                   
     C                   when      AttachType='*OTHER'                                              
     C                   eval      myAttachType='application/octet-stream'                          
     C                   other                                                                      
     C                   eval      myAttachType='application/octet-stream'                          
     C                   endsl                                                                      
      * Open files                                                                                  
     C                   exsr      OpnF                                                             
      * Check the message stream file                                                               
     C                   eval      IfsFilenam = TextStmf                                            
     C                   eval      rc = ChkIfs(IfsFileNam:IfsFileSiz:                               
     C                             IfsCrtStamp)                                                     
     C                   if        rc<>0                                                            
     C                   eval      msg='Stream file "' + %trim(IfsFileNam) +                        
     C                             '" cannot be accessed.'                                          
     C                   exsr      SndErrMsg                                                        
     C                   endif                                                                      
      * Check the attachment stream file                                                            
     C                   IF        AttachStmf<>'/NIL'                                               
     C                   eval      IfsFilenam = AttachStmf                                          
     C                   eval      rc = ChkIfs(IfsFileNam:IfsFileSiz:                               
     C                             IfsCrtStamp)                                                     
     C                   if        rc<>0                                                            
     C                   eval      msg='Stream file "' + %trim(IfsFileNam) +                        
     C                             '" cannot be accessed.'                                          
     C                   exsr      SndErrMsg                                                        
     C                   endif                                                                      
     C                   ENDIF                                                                      
      * Check the sender's ID                                                                       
     C     SenderID      chain     sidrcd                                                           
     C                   if        not %found                                                       
     C                   eval      msg='Sender ID "' + %trim(SenderID) +                            
     C                             '" not found in file ' +                                         
     C                             %trim(DataLib) + '/SENDERIDS.'                                   
     C                   exsr      SndErrMsg                                                        
     C                   endif                                                                      
      * If interactive job and batch mode (BMode) requested, submit batch job                       
     C                   eval      exeEnv=RtvExeEnv                                                 
     C                   IF        exeEnv='1' and BMode='*YES'                                      
     C                   eval      rc=docmd('chkobj MMAIL/EMLDST *cmd')                             
     C                   if        rc<>0                                                            
     C                   eval      msg='Command EMLDST not found +                                  
     C                             in library list.'                                                
     C                   exsr      SndErrMsg                                                        
     C                   endif                                                                      
     C                   eval      cmd='MMAIL/EMLDST +                                              
     C                             senderid(''' + %trim(SenderID) + ''') +                          
     C                             subject(''' + %trim(Subject) + ''') +                            
     C                             text(''' + %trim(TextStmf) + ''') +                              
     C                             datalib(' + %trim(DataLib) + ') +                                
     C                             dstl(' + %trim(DstL) + ') +                                      
     C                             dsttype(' + %trim(DstType) + ') +                                
     C                             attachment(''' + %trim(AttachStmf) + ''') +                      
     C                             attachtype(''' + %trim(AttachType) + ''')'                       
     C                   eval      rc=docmd('sbmjob EMLDST jobq(qsysnomax) +                        
     C                             cmd(' + %trim(cmd) + ')')                                        
     C                   if        rc<>0                                                            
     C                   eval      msg='Command EMLDST could not be submitted +                     
     C                             for batch execution.'                                            
     C                   exsr      SndErrMsg                                                        
     C                   endif                                                                      
     C                   exsr      Exit                                                             
     C                   ENDIF                                                                      
      * Call the appropriate module that will create and send the message                           
     C                   eval      nbrReceivers=0                                                   
     C                   select                                                                     
     C                   when      DstType='A'                                                      
     C                   callb     'EMLDSTA'     EmlDstList                                         
     C                   when      DstType='B'                                                      
     C                   callb     'EMLDSTB'     EmlDstList                                         
     C                   endsl                                                                      
      * Notify the total number of receivers                                                        
     C                   if        nbrReceivers=0                                                   
     C                   eval      msg='The message was not sent to anyone.'                        
     C                   else                                                                       
     C                   eval      msg='The message was sent to ' +                                 
     C                             %trim(%editc(NbrReceivers:'J'))  +                               
     C                             ' receivers.'                                                    
     C                   endif                                                                      
     C                   exsr      SndWrnMsg                                                        
      * Back to caller                                                                              
     C                   exsr      Exit                                                             
      *=====================================================================                        
      * Open files                                                                                  
      *=====================================================================                        
     C     OpnF          begsr                                                                      
      *                                                                                             
     C                   if        not %open(SENDERIDS)                                             
     C                   eval      rc=docmd('ovrdbf SENDERIDS '+                                    
     C                             %trim(datalib) + '/SENDERIDS +                                   
     C                             secure(*yes) ovrscope(*job)')                                    
     C                   open      SENDERIDS                                                        
     C                   endif                                                                      
      *                                                                                             
     C                   endsr                                                                      
      *=====================================================================                        
      * Close files                                                                                 
      *=====================================================================                        
     C     CloF          begsr                                                                      
      *                                                                                             
     C                   if        %open(SENDERIDS)                                                 
     C                   close     SENDERIDS                                                        
     C                   endif                                                                      
      *                                                                                             
     C                   endsr                                                                      
      *=====================================================================                        
      * Send warning message                                                                        
      *=====================================================================                        
     C     SndWrnMsg     begsr                                                                      
     C                   eval      module=psdsPgmnam                                                
     C                   callp     SndPgmMsg(module:topgmq:msgtype:msg)                             
      *                                                                                             
     C                   endsr                                                                      
      *=====================================================================                        
      * Send error message                                                                          
      *=====================================================================                        
     C     SndErrMsg     begsr                                                                      
     C                   eval      module=psdsPgmnam                                                
     C                   callp     SndPgmMsg(module:topgmq:msgtype:msg)                             
     C                   exsr      Exit                                                             
      *                                                                                             
     C                   endsr                                                                      
      *=====================================================================                        
      * Back to caller                                                                              
      *=====================================================================                        
     C     Exit          begsr                                                                      
     C                   exsr      CloF                                                             
     C                   eval      *inlr=*on                                                        
     C                   return                                                                     
      *                                                                                             
     C                   endsr