Tuesday, September 6, 2016

Multiple email addresses in SNDSMTPEMM

sndsmtpemm multiple email addresses

Anonymous asked me the following question regarding the Send SMTP Email command, SNDSMTPEMM:

If multiple email id's are given in SNDSMTPEMM using a parameter, it does not work as the parameter is in CHAR type and it automatically uses quotes when passed as a parameter.

Any ideas how to resolve it?

As there is a lack of information available about this command I thought rather than answer the comment on the original post, Email IFS files, I would create new post as I am sure that Anonymous is not the only person to have encountered this.

The first parameter Recipient, RCP, is a list value which means if you want to provide multiple email addresses they need to be passed in individual variables, for example:

01  DCL VAR(&EMAIL1) TYPE(*CHAR) LEN(50)
02  DCL VAR(&EMAIL2) TYPE(*CHAR) LEN(50)

03  CHGVAR VAR(&EMAIL1) VALUE('simon@rpgpgm.com')
04  CHGVAR VAR(&EMAIL2) VALUE('someone.else@rpgpgm.com')

05  SNDSMTPEMM RCP((&EMAIL1) (&EMAIL2)) +
                 SUBJECT('This is a test') +
                 NOTE('This email is a test')

The real giveaway that RCP is a list value are the parentheses ( ( ) ) that surround each value.

Before you think I have given away my email address rest assured that neither of these email addresses is valid.

If I try to use a single variable for the recipients, like below, when I run the program I get a TCP5092 message.

01  DCL VAR(&EMAIL1) TYPE(*CHAR) LEN(50)
02  DCL VAR(&EMAIL2) TYPE(*CHAR) LEN(50)
03  DCL VAR(&RECIPIENTS) TYPE(*CHAR) LEN(200)

04  CHGVAR VAR(&EMAIL1) VALUE('simon@rpgpgm.com')
05  CHGVAR VAR(&EMAIL2) VALUE('someone.else@rpgpgm.com')

06  CHGVAR VAR(&RECIPIENTS) VALUE('(' || &EMAIL1 |< ') +
                                   (' || &EMAIL2 |< ')')

07  SNDSMTPEMM RCP(&RECIPIENTS) +
                 SUBJECT('This is a test') +
                 NOTE('This email is a test')

Email Address Invalid.
Send E-mail Failed.
Function check. TCP5092 unmonitored by TESTCL at statement 0000000600,

The way I have found to overcome list values in the past is to use the QCMDEXC API. I can build my command in a variable and then pass it to QCMDEXC, for example:

01  DCL VAR(&EMAIL1) TYPE(*CHAR) LEN(50)
02  DCL VAR(&EMAIL2) TYPE(*CHAR) LEN(50)
03  DCL VAR(&STRING) TYPE(*CHAR) LEN(256)
04  DCL VAR(&LENGTH) TYPE(*DEC) LEN(15 5)

05  CHGVAR VAR(&EMAIL1) VALUE('simon@rpgpgm.com')
06  CHGVAR VAR(&EMAIL2) VALUE('someone.else@rpgpgm.com')

07  CHGVAR VAR(&STRING) +
             VALUE('SNDSMTPEMM RCP((' || &EMAIL1 |< ' *PRI) +
                                   (' || &EMAIL2 |< ' *PRI)) +
                    SUBJECT(''This is a test'') +
                    NOTE(''This email is a test'')')

08  CHGVAR VAR(&LENGTH) VALUE(%LEN(&STRING))
09  CALL PGM(QCMDEXC) PARM(&STRING &LENGTH)

The built in function %LEN returns the length of the variable &STRING, not the length of the data contained within.

I presume that Anonymous has a file that contains the email addresses. If so they can create a RPG program that retrieves those addresses from the file, builds the command string, and then calls QCMDEXC.

 

This article was written for IBM i 7.3, and should work for 7.2 and some 7.1 TRs.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.