21
Sat, Dec
3 New Articles

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

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: