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.
Is there a way to determine current user's email address in Cl program?
ReplyDeleteWhat happens if &EMAIL2 is left blank? In other words, can you build the CL so that you can prompt up to 10 email addresses but usually have fewer email addresses?
ReplyDeleteThe code I give in these posts is simple just to show how things work. If I was doing this in a program in a production environment I would check the value of &EMAIL2. If it is blank I would not include it in the variable &STRING.
Deleteis there a possibility to attach more files like xxx.* (all xxx )
ReplyDeleteWhat if we have 10 email parameters for TO and for CC ? Should we be checking each parameter as blanks and then if not blanks ,include in string...if that is the case, won't it have too many iterations? Kindly advice
ReplyDeleteThis sounds more like an issue with what you are using to load the command. If you have more email addresses than "fit" in the command you wi;; have to send the email multiple times with different email addresses, until you have included them all.
DeleteCan we use dclf and send the email to the feild of values at a time?
ReplyDeleteIf you are meaning can you format the email address:
Delete(email1 email2, email3)
Then no you cannot.
The need to the be the quotes and parentheses to format them in the manner the command expects.