Sending mail from a CL program

Log in to save

You need to notify someone when a batch job finishes. The shortest route is SNDSMTPEMM, an ordinary CL command that requires nothing to be installed.

DCL VAR(&TIME) TYPE(*CHAR) LEN(6)
RTVSYSVAL SYSVAL(QTIME) VAR(&TIME)

SNDSMTPEMM RCP((operations@company.com))            +
           SUBJECT('Month end close completed')     +
           NOTE('Processing finished at ' *CAT &TIME)

The time comes from RTVSYSVAL on QTIME, a six-character value in HHMMSS form.

To attach a file from the IFS, add ATTACH:

SNDSMTPEMM RCP((operations@company.com))     +
           SUBJECT('Close report')            +
           NOTE('Summary attached.')          +
           ATTACH(('/home/batch/close.csv'))

Example: the batch that reports how it went

Mail that arrives only when everything went well is worth nothing: nobody notices the mail that did not arrive. The useful one distinguishes the two outcomes.

PGM

  DCL VAR(&TIME) TYPE(*CHAR) LEN(6)

  CALL PGM(PRODLIB/CLOSE)
  MONMSG MSGID(CPF0000) EXEC(DO)
    RTVSYSVAL SYSVAL(QTIME) VAR(&TIME)
    SNDSMTPEMM RCP((operations@company.com))                      +
               SUBJECT('MONTH END FAILED')                        +
               NOTE('Stopped at ' *CAT &TIME *CAT                 +
                    '. Job log needs checking.')
    RETURN
  ENDDO

  RTVSYSVAL SYSVAL(QTIME) VAR(&TIME)
  SNDSMTPEMM RCP((operations@company.com))            +
             SUBJECT('Month end completed')           +
             NOTE('Finished at ' *CAT &TIME)

ENDPGM

The RETURN inside the DO is what stops the success mail going out right behind the failure one. Without it both arrive, and the second erases the first in the reader's head.

Worth putting the outcome in the subject rather than the body: people checking mail on a phone see only that.

Warning

the command fails without saying which prerequisite is missing, and there are two. The SMTP server must be started: after STRTCPSVR SERVER(*SMTP), check the jobs actually came up with WRKACTJOB SBS(QSYSWRK), looking for QTSMTPCLTD and QTSMTPSRVD. The profile must be registered: it needs an entry in the system directory (WRKDIRE) and an associated SMTP name (WRKNAMSMTP).

Tip

when the mail leaves but never arrives, the prerequisites are not the problem. The place to look is the forwarding mailhub, read with CHGSMTPA: without a valid forwarder the system accepts the message and delivers it to nobody.

Note

the NOTE text is limited in length. For longer messages, write the body to a file on the IFS and attach it rather than composing it in the program.


Releases. SNDSMTPEMM is present on every release still in support. The real constraint is not the version but the configuration: SMTP started and the profile present in the system directory. Checked with WRKDIRE, WRKNAMSMTP and WRKACTJOB SBS(QSYSWRK).

Sources

← Back to blog

Comments

No comments yet. Be the first to comment!

You need an account to comment. Log in · Sign up