Attach Excel file to email
This article shows step-by-step configurations of exporting a data page list to an Excel file and then sending the Excel file as an email attachment.
This demo was initially developed using Pega Platform 8.8.2 and later updated to 23.1.1.
Here is a high-level view of the activity. See the details of each step below.
Step 1 – Set an output Excel file name.
- Notice the file extension (.xlsx) used.
Step 2 – Generate an Excel file by using an OOTB pxGenerateExcelFile activity.
- Do NOT select the DownloadFile checkbox.
- Refer to this article to learn more about how to export a data page list to an Excel file.
Step 3 – Set an output Excel file location.
- System automatically saves the output Excel file to pxProcess.pxServiceExportPath.
Step 4 – Generate an output Excel file stream byte. This Java step converts the Excel output file to a byte stream.
- A local variable (strFileData, String) must be defined in the Parameters tab of the activity.
- A local variable (attach, Boolean) must be defined in the Parameters tab of the activity.
- Java source code:
String strFileName = tools.getParamValue("OutputLocation");
PRFile objFile = new PRFile(strFileName);
//some error checking
if (!objFile.exists())
{
attach = false;
}
if(!objFile.isFile())
{
attach = false;
}
tools.putParamValue("OutputLocation", objFile.getName());
if (!objFile.canRead())
{
attach = false;
throw new PRRuntimeException("Can't continue with file upload. File \"" + strFileName + "\" is unreadable.");
}
//read the file into a buffer.
java.io.DataInputStream dis = null;
byte buffer [] = null;
try
{
dis = new java.io.DataInputStream(new PRInputStream(objFile));
buffer= new byte[dis.available()];
dis.readFully(buffer);
dis.close();
}
catch (Exception e)
{
attach = false;
throw new PRRuntimeException("Can't continue with file upload. Can't read File \"" + strFileName + "\"");
}
//encode the file to Base64 so that we can store it on the database
strFileData = Base64Util.encodeToString(buffer);
if (strFileData == null)
{
attach = false;
throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");
}
- Best practice - Put the Java code in a Rule-Utility-Function rule (see attached doc) and call it from the activity to eliminate the severe Java guardrail warning.
Step 5 – Set the attachment properties.
- Under ‘Pages & Classes’ of activity, define a page name (Attachment) with Class (Embed-EmailAttachment).
- Note that .pyData property is set with local.strFileData which contains the Excel file stream byte generated by the previous Java step.
Step 6 – Add the Attachment page to AttachmentList page.
- Under ‘Pages & Classes’ of activity, define a page name (AttachmentList) with Class (Data-EmailAttachments).
- CopyInto = AttachmentList.pyAttachments(1)
- To add another attachment, copy it to AttachmentList.pyAttachments(2) and so on..
Step 7 – Open an Email Account instance, which will be used to send an email with attachment.
- Under ‘Pages & Classes’ of activity, define a page name (Email) with Class (Data-EmailAccount).
For this demo, a Gmail account was used as Sender.
Step 8 – Set the email parameters.
Step 9 – Call an OOTB SendEmailNotification activity to send out an email with attachment.
Step 10 – Clean up temporary pages.
Click “Run” to test the activity. It should send an email with an Excel attachment.