Cool Things: A Simple Utility for Sending HTML Emails on the IBM i

APIs
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

 

Send a simple message to notify a user when a batch job completes, or send a detailed notification to a customer, or anything in between.

If you've used the SNDDST command on the IBM i to send email messages, you know that this is a very useful way to communicate things like job completions. The one big drawback is that you have very little control over the format of the messages sent using this command. While this command does give you the ability to embed line feed and new paragraph commands into your message body, that's about all of the formatting you can do. What if you could embed HTML statements into your email messages? Read on and discover how to do this with the help of a simple little command.

QtmmSendMail API

The key to our utility is the use of the IBM system API QtmmSendMail. It allows you to send a MIME email without using the SNDDST command, and the best part is that it gives you full control over the message, including the ability to define the message body using HTML tags. This API, which is located in the QTMMSNDM service program located in the QTCP library, accepts parameters that identify the sender's name and email address, the recipients' names and email addresses, and a stream file on the IFS that contains the message body for the email. While I won't be covering it in this tip, this same API can also be used to send SMTP mail messages with multiple attachments.

The Process

To send an email that includes embedded HTML, a two-step process is required. First, you need to generate a file on the IFS that contains the message body, including the embedded HTML tags. Second, you need to call the QtmmSendMail API, passing the name of the file on the IFS to be used as the message body along with the list of recipient email addresses and the sender's name and email address. To simplify this process, I've created an ILE RPG program that handles both parts for you. This is the source for SNDHTMLEML:

      //*********************************************************************

      // SNDHTMLEML:  Send a MIME email message using HTML Tags for formatting

      //*********************************************************************

     h BNDDIR('QC2LE')

      // This program uses service program QTMMSNDM in library QTCP.

     DSNDHTMLEML       PR

     D  FROMADDR                    100A   CONST

     D  FROMNAME                    100A   CONST

     D  TOADDRS                            CONST LIKEDS(EMAILADDRS) DIM(20)

     D  SUBJECT                      80A   CONST

     D  HTMLMSG                    5000A   CONST

     DSNDHTMLEML       PI

     D PARMFROMADDR                 100A   CONST

     D PARMFROMNAME                 100A   CONST

     D PARMTOADDRS                         CONST LIKEDS(EMAILADDRS) DIM(20)

     D PARMSUBJECT                   80A   CONST

     D PARMHTMLMSG                 5000A   CONST

      // QTMmSendMail API Prototypes

     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

      // Recipient Email Address Data Structure

     D EmailAddrs      DS                  qualified

     D   unused                       4b 0

     D   type                         3A

     D   name                       100A

     D   address                     50A

      // Recipient Email Address Data Structure used by QTMMSENDMAIL

     D recipientList   ds                  likeds(ADDTO0100)

     D                                     dim(%elem(PARMTOADDRS))

     D recipientCount  s              3  0

     D tempFileName    s            100A

     D mailDate        s             30A

      // C Language IFS Prototypes

      //-----------------------------------------------------------------

      // createTempSTMF():  Creates a file name for a temporary stream file

      //

      //   filename = (input) path to file in the IFS

      //-----------------------------------------------------------------

     DcreateTempSTMF   PR              *   extproc('_C_IFS_tmpnam')

     D string                        39A   options(*omit)

      //-----------------------------------------------------------------

      // removeSTMF():  Deletes the defined streamed file.

      //

      //   filename = (input) path to file in the IFS

      //-----------------------------------------------------------------

     DremoveSTMF       PR            10I 0 extproc('_C_IFS_remove')

     D filename                        *   VALUE OPTIONS( *String)

      //-----------------------------------------------------------------

      // openSTMF():  Open File for buffered reading/writing

      //

      //   filename = (input) path to file in the IFS

      //       mode = (input) various open mode flags. (see manual)

      //

      //  returns *NULL upon error, or a pointer to a FILE structure

      //-----------------------------------------------------------------

     dopenSTMF         PR                  extproc('_C_IFS_fopen')

     d                                     like(pFILE)

     d filename                        *   value options(*string)

     d mode                            *   value options(*string)

      //-----------------------------------------------------------------

      // closeSTMF(): Close File

      //    stream = (input) pointer to FILE structure to close

      //-----------------------------------------------------------------

     dcloseSTMF        PR            10i 0 extproc('_C_IFS_fclose')

     dparStream                            like(pFILE) value

     dpFile            s               *   based(prototype_only)

     D fd              s                   like(openSTMF)

     D ix              s              3  0 inz(0)

     D header          s          32767a

      //-----------------------------------------------------------------

      // fputs(): Write string

      //

      //    string = (input) string to write to file

      //    stream = (input) FILE structure designating the file to

      //                write to.

      //

      //  returns a non-negative value if successful

      //       or -1 upon error

      //-----------------------------------------------------------------

     dfputsSTMF        PR            10i 0 extproc('_C_IFS_fputs')

     d String                          *   value options(*string)

     d fileStream                          like(pFILE) value

     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)

      // Variables used to generate RFC2822 date format

     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 timezone_hours  s              2P 0

     D timezone_mins   s              2P 0

     D timezone        s              5A   varying

     D currentTime     s              8F

     D tempDate        s             25A

     D nullErrorDS     ds

     D   BytesProv                   10I 0 inz(0)

     D   BytesAvail                  10I 0 inz(0)

      // Email Address Type Constants

     D CONST_TO_ADDRESS...

     D                 c                   0

     D CONST_CC_ADDRESS...

     D                 c                   1

     D CONST_BCC_ADDRESS...

     D                 c                   2

      // Line Feed Character

     D CONST_LF        c                   x'25'

     D CONST_CRLF      c                   x'0d25'

     c/free

        tempFileName = %trim(%str(createTempSTMF(*omit)));

        // create new output file

        fd = openSTMF(%trim(tempFileName): 'w codepage=1252');

        if (fd = *NULL);

           *INLR = *ON;

           return;

        endif;

        // ------------------------------------------

        // close file & reopen in text mode so that

        // data will be automatically translated

        // ------------------------------------------

        closeSTMF(fd);

        fd = openSTMF( %trim(tempFileName) : 'a codepage=37');

        if (fd = *NULL);

           *INLR = *ON;

           return;

        endif;

         //

         //  Calculate the Timezone in format '+0000', for example

         //    CST should show up as '-0600'

         //

         CEEUTCO(hours: mins: junk1: *omit);

         timezone_hours = %abs(hours);

         timezone_mins = mins;

         if (hours < 0);

            timezone = '-';

         else;

            timezone = '+';

         endif;

         timezone += %editc(timezone_hours:'X') + %editc(timezone_mins:'X');

         //

         //  Get the current time and convert it to the format

         //    specified for e-mail in RFC 2822

         //

         CEELOCT(junk2: CurrentTime: junk3: *omit);

         CEEDATM(CurrentTime: rfc2822: tempDate: *omit);

         maildate = tempDate + ' ' + timezone;

         recipientCount = 0;

         header = 'From: "' + %trim(parmfromName)

                   + ' "<' + %trim(parmFromAddr) + '>' + CONST_LF;

         for ix = 1 to %elem(parmToAddrs);

            if %trim(parmToAddrs(ix).type) = '';

               leave;

            endif;

            header = %trim(header) + %trim(parmToAddrs(ix).type) + ': '

                    + %trim(parmToAddrs(ix).name) + ' <'

                    + %trim(parmToAddrs(ix).address) + '>' + CONST_LF;

            recipientList(ix).NextOffset = %size(ADDTO0100);

            recipientList(ix).AddrFormat = 'ADDR0100';

            select;

               when parmToAddrs(ix).type = 'TO';

                  recipientList(ix).DistType   = CONST_TO_ADDRESS;

               when parmToAddrs(ix).type = 'CC';

                  recipientList(ix).DistType   = CONST_CC_ADDRESS;

               when parmToAddrs(ix).type = 'BCC';

                  recipientList(ix).DistType   = CONST_BCC_ADDRESS;

               other;

               recipientList(ix).DistType   = CONST_TO_ADDRESS;

            endsl;

            recipientCount += 1;

            recipientList(ix).Reserved = 0;

            recipientList(ix).SmtpAddr = %trim(parmToAddrs(ix).address);

            recipientList(ix).AddrLen = %len(%trim(parmToAddrs(ix).address));

         endfor;

         header = %trim(header) +'Date: ' + maildate + CONST_LF

                 +'Subject: ' + parmSubject + CONST_LF

                 +'MIME-Version: 1.0' + CONST_LF

                 +'Content-Type: multipart/related; boundary="MSG_PART"'

                 + CONST_LF + CONST_LF + '--MSG_PART' + CONST_LF

                 +'Content-Type: text/html' + CONST_LF

                 +'Content-Disposition: inline;' + CONST_LF + CONST_LF

                 + CONST_LF;

         fputsSTMF(%trim(header): fd);

         fputsSTMF(%trim(PARMHTMLMSG) + CONST_LF: fd);

         fputsSTMF('--MSG_PART--' + CONST_CRLF: fd);

         closeSTMF(fd);

          // ------------------------------------------

          //  Use the QtmmSendMail() API to send the

          //  IFS file via SMTP

          // ------------------------------------------

         QtmmSendMail( %trim(tempFileName): %len(%trim(tempFileName))

                     : %trim(parmFromAddr): %len(%trim(parmFromAddr))

                     : recipientList: recipientCount: nullErrorDS);

         removeSTMF(%trim(tempFileName));

         *inlr = *on;

         return;

      /end-free

 

This program starts by using the _C_IFS_tmpnam C language API to generate a temporary file name to be used as the name for the file that will contain the message body.

Next, we create that file and then immediately close and re-open it to ensure proper CCSID conversion.

Then, we need to do some time/date conversions to generate a date in the accepted email date format as defined in RFC2822. This program accepts the list of recipient email addresses as an array of data structures. Each data structure contains the recipient type (TO, CC, or BCC), the recipient name, and the recipient email address. The program loops through the array and translates those recipients into the list format required by QtmmSendMail. At the same time, it also generates the list of recipients with the string that contains the message header. That message header also contains the "from" name and address along with the RFC2822 formatted "sent date" and the message subject.

Finally, the program adds the message headers to identify the content type of our message along with the boundary identifier that is used to define the message body. Once all of the message headers have been generated, the string containing those message headers is written out to the temporary file that we created earlier. After the message headers have been written, the entire content of the HTML message body is written out to the same file, followed by an identifier for the end of this part of the message. Note that in the case of messages containing a message body and attachments, the message would have multiple parts identified. After closing the temporary file, we call the QtmmSendMail API to send our email message. After that API has completed, we remove the temporary file.

That's all that's required to generate and send an HTML email message. Included in the code for this tip (located here), I've also created a CLLE program and CMD. The table below shows the parameter list for the SNDHTMLEML command.

 

 

Keyword

Description

Choices

Notes

SUBJECT

Message Subject

Character value

Required, Positional 1

HTMLMSG

Message Body

Character value (Max 5000 char)

Required, Positional 2

TOADDRS

Recipients

Values (up to 20 repetitions): Element list

Required, Positional 3

Element 1: Recipient Type

TO, CC, BCC

Element 2: Recipient Name

Character value

Element 3: Recipient Email Address

Character value

FROMADDR

From Email Address

Character value, *CURRENT

Optional, Positional 4

FROMNAME

From Email Name

Character value, *CURRENT

Optional, Positional 5

         

 

Note that the FROMADDR and FROMNAME parameters can be provided, or the default value *CURRENT can be used for each. When that's done, the RTVSMTPNAM program (also included with the code for this article) is used to retrieve the SMTP email address for the user as identified on the directory entry for the user executing the command. If the FROMNAME parameter is specified as *CURRENT, the user text from the current user's profile is passed in as the "from" name. Below is an example of how to use this command.

 

SNDHTMLEML SUBJECT('This is a test message')                       

           HTMLMSG('<H1>This is a test<h1><br><p style="font       

                   -family:arial;color:red;font-size:20px;">       

                   This is a test of the <i>SNDHTMLEML</i> c       

                   ommand.<br></p>')                               

           TOADDRS((TO 'Mike Faust' This email address is being protected from spambots. You need JavaScript enabled to view it.'))         

           FROMADDR(This email address is being protected from spambots. You need JavaScript enabled to view it.')                        

           FROMNAME('Mike F Faust')                                

 

Note that the "from" and "to" addresses shown are the same; however, I don't generally send email to myself like this. Figure 1 shows the email message that is sent by this command.

030212Faustcoolthingshtmlfig02 

Figure 1: This is a sample of a message sent by SNDHTMLEML. (Click image to enlarge.)

Sending the Message

Whether you need to send a message to simply notify the user when a batch job completes, or you need to send a detailed notification to a customer, this simple command gives much more control over the message format using standard HTML tags.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$