| Has Your System i Emailed Someone Today? |
|
|
|
| Programming - RPG | |||||
| Written by Rafael Victoria-Pereira | |||||
| Tuesday, 17 March 2009 19:00 | |||||
|
Depending on how much functionality you need, you have a variety of options for sending email from your System i.
Did you know your System i can send email messages? In this article, I'll demonstrate how you can easily integrate email into your existing programs, allowing them to send an email message as simply as (or more simply than) they produce a report. Four Solutions for One ProblemThe four email-sending procedures presented here were created over a couple of years, according to the company's needs. I'll start with the simplest solution--sending short, text-only email--and move up to the most complex--sending formatted text and several types of attachments (text files, Excel spreadsheets, and PDF files) to multiple addresses.
The SndEmail procedure (Figure 1) allows you to send an email message to a maximum of three addresses.
*-------------------------------------------------------------------------* * Send E-Mail Message *------------------------------------------------------------------------- PSndEMail B EXPOR D PI * Input Parm DP_SendTo 75 Valu DP_CopyTo 75 Valu DP_BlindCopyTo 75 Valu DP_Subject 30 Valu DP_Body 200 Valu
/FRE
// Set the e-mail destination string // (A) SetDestStr(P_SendTo : P_CopyTo : P_BlindCopyTo : P_DestStr)
// Send message // (B) P_Cmdlin = 'SNDDST TYPE(*LMSG) + ' TOINTNET(' + %Trim(P_DestStr) + ') + ' DSTD(' + '''' + %Trim(P_Subject) + '''' + ') + ' LONGMSG(' + '''' + %Trim(P_Body) + '''' + ') + ' PTY(*HIGH)' // (C) W_ErrorDS = ExecCmd (%Trim(P_CmdLin))
// If the mail was sent (no error message id), return *Of If W_ErrorDS.APIMsgId = *Blanks Return *Off Else Return *On Endif
/END-FRE
PSndEMail E
Figure 1: Use SndEmail to send email to up to three addresses.
The message is constrained by the subject and body lengths (30 and 200 characters, respectively) and doesn't allow any attachments. This might suffice if all you need is to alert a user with a short and simple message.
How does it work? Well, this procedure is actually a programmer-friendly mask to the SNDDST CL command: the procedure's parameters are transformed to fit the command's keywords. The P_SendTo, P_CopyTo, and P_BlindCopyTo parameters are rearranged by the SetDestStr procedure into the destination string that will fit the command's TOINTNET keyword (see A). The P_Subject and P_Body parameters are used in the DSTD and LMSG keywords, respectively (see B). When the CL command is built, the ExecCmd procedure is used to execute it (see C).
Sounds simple, doesn't it? Too simple? You need to send a message and an output file? Use SndFileByMail (Figure 2).
*-------------------------------------------------------------------------* * Send File By Mail *------------------------------------------------------------------------- PSndFileByMail B EXPOR D PI * Input Parm DP_SendTo 75 Valu DP_CopyTo 75 Valu DP_BlindCopyTo 75 Valu DP_Body 200 Valu DP_FileName 10 Valu DP_DocName 12 Valu DP_FolderName 10 Valu DP_Format 10 Valu
/FRE
// Set the e-mail destination string // (A) SetDestStr(P_SendTo : P_CopyTo : P_BlindCopyTo : P_DestStr)
// Retrieve the field separator for CSV format // (B) In DaFldSep W_FldSep = DaFldSep
// Set attachment (either copy PF to folder, or use existing doc in folder // (C) If P_DocName = *Blanks P_DocName = %Trim(P_FileName) + '.' + %Trim(P_Format) EndIf If P_FileName <> *Blanks Select When P_Format = 'TXT' P_Cmdlin = 'CPYTOPCD FROMFILE(' + %Trim(P_FileName) + ') + ' TOFLR(' + %Trim(P_FolderName) + ') + ' TODOC(' + '''' + %Trim(P_DocName) + '''' + ') + ' REPLACE(*YES)'
When P_Format = 'CSV' P_Cmdlin = 'CPYTOIMPF FROMFILE(' + %Trim(P_FileName) + ') + ' TOSTMF(' + '''' + 'QDLS/ + %Trim(P_FolderName) + '/' + %Trim(P_DocName) + ''' + ') MBROPT(*REPLACE) STMFCODPAG(437) + ' RCDDLM(*CRLF) STRDLM(' + '''' + ' ' + '''' + ') + ' FLDDLM(' + '''' + W_FldSep + '''' + ')' Other Return *On EndSl W_ErrorDS = ExecCmd (%Trim(P_CmdLin))
// If an error occurred (error message id is not blank), return *O // (D) If W_ErrorDS.APIMsgId <> *Blanks Return *On EndIf
// Send messag // (E) P_Cmdlin = 'SNDDST TYPE(*DOC)' ' TOINTNET(' + %Trim(P_DestStr) + ') + ' DSTD(' + '''' + 'DESC' + '''' + ') + ' MSG(' + '''' + %Trim(P_Body) + '''' + ') + ' DOC(' + '''' + %Trim(P_DocName) + '''' + ') + ' FLR(' + %Trim(P_FolderName) + ') PTY(*HIGH)' W_ErrorDS = ExecCmd (%Trim(P_CmdLin))
// If the mail was sent (no error message id), return *Of If W_ErrorDS.APIMsgId = *Blanks Return *Off Else Return *On Endif Endif
/END-FRE
PSndFileByMail E
Figure 2: Use SndFileByMail to send a message and an output file.
This procedure can be used to send an output file generated by your program in text or comma-separated values (CSV) format to the same three addresses as the previous procedure. Again, the procedure is a mask to the SNDDST CL command: the destination addresses are treated the same way as previously (see A), but the similarity to the first procedure ends here. The CSV conversion functionality requires a separator, usually a comma. Since this separator can vary in different countries or Excel versions, it's stored in a data area (DAFLDSEP). Because of this, the procedure's next step is retrieving the separator character (see B).
The attachment type, passed in parameter P_Format, determines the type of conversion that will be performed. For the text format, the procedure uses the CPYTOPCD CL command, while the CPYTOIMPF command is used to generate the CSV format (see C). If the conversion fails, the procedure ends, returning *On. This is the way to tell the calling program that something went wrong and the email was not sent (see D). Finally, the CL command string is formed and executed (see E). Notice that the P_Body is now placed in the MSG keyword and there is no P_Subject parameter. This happens because we are now using a different type of distribution that forces us to do these changes (see the SNDDST CL command explanation).
What if all you really need is to send a spool file (some sort of business report or purchase order) via email to a supplier or client, without any need for fancy subject or message text because the attachment says it all? In that case, use SndSplfByMail (Figure 3).
*-------------------------------------------------------------------------* * Send Spoolfile By Mail *-------------------------------------------------------------------------* PSndSplfByMail B EXPORT D PI N * Input Parms DP_SplfName 10 Value DP_SendTo 75 Value
DI_ProdSys S N Inz(*Off)
/FREE
// (A) // Check if this is the production system. // If it's not, e-mails won't be sent out of the Company I_ProdSys = ProductionSys;
// Retrieve the PDF transform printer name In DaPdfPrt; W_PdfPrt = DaPdfPrt;
// * Get the user's e-mail address (if internal) // (B) P_SendTo = RtvEmailAddr(P_SendTo); // If this it not the production system // and the destination mail is not from the company, send it. // (C) If Not I_ProdSys And Not InternalAddr(P_SendTo); Return *On; EndIf;
// Change spoolfile attributes and redirect to PDF transform printer // (D) P_Cmdlin = 'CHGSPLFA FILE(' + %Trim(P_SplfName) + ') SPLNBR(*LAST)' + ' OUTQ(*LIBL/' + %Trim(W_PdfPrt) + ')' + ' USRDFNDTA(' + '''' + 'MAILTAG(' + %Trim(P_SendTo) + ')' + '''' + ')'; W_ErrorDS = ExecCmd (%Trim(P_CmdLin));
// If the mail was sent (no error message id), return *Off If W_ErrorDS.APIMsgId = *Blanks; Return *Off; Else; Return *On; Endif;
/END-FREE
PSndSplfByMail E
Figure 3: Use SndSplfByMail to send only a spool file.
This very simple procedure takes advantage of the PDF conversion functionality provided by the InfoPrint Server licensed program. IBM's Printing Redbook explains how to install and configure this functionality, so I won't waste your time with that here; I'll just explain how it works. The conversion is performed by a virtual printer that also sends the generated PDF file via email to the address you specify in the USRDFNDTA keyword of the CHGSPLFA CL command.
Let's go over the code. In the beginning of the procedure (see A), the necessary data is gathered: system type (development, test, or production) and PDF conversion printer are retrieved. In the next step (see B), the P_SendTo parameter is checked and converted to an Internet email address if necessary (read about procedure RtvEmailAddr in the "Other Procedures" sidebar at the end of this article). In C, the address is checked (to prevent sending test data to the "outside world") using the system type information and the destination address (the procedure InternalAddr is also explained in the "Other Procedures" sidebar). Next, in D, the conversion and distribution are performed in one single step: the spool file name (P_SplfName), PDF conversion printer (W_PdfPrtf), and destin | |||||
View all articles by this author
|
|||||
| Last Updated on Tuesday, 17 March 2009 12:46 |






You must be logged in to view or make comments on this article