SendMail

You can use this function for Optiva workflows.

Purpose

Sends an email to system users that includes a file attachment or a file path. The file attachment is assigned to a function code for an object. For attachments, use SendMail instead of Notify.

The second syntax listed, useful when running with the optiva secured scripting enabled, uses the EmailFileAttachment object to transport raw data of the file attachment and the file name's between the secured scripting layer and the Optiva application.

A third syntax, with very different arguments, allows designation of a Communication Template (formerly e-mail template) to define the message subject and body. This format also allows file attachments, and the inclusion of tokens to be passed to the Communication Template to show live data at run-time.

Syntax


Dim Variable as Long = SendMail(UserCodes(),Subject, Message, FileNames(), isHtml)
Dim Variable as Long = SendMail(UserCodes(),Subject, Message, List(Of EmailFileAttachment), isHtml)

Arguments

Part Description
UserCode(s) A string array of system user codes for addresses.
Subject Text string for the subject line of the email.
Message Text string for the contents of the email.
FileName(s)
A string of array of file names (with path) assigned to attach the email. The path must be some UNC file path that is accessible to the optiva application.
Note: This option is not supported when secured scripting is enabled.
Or
A .Net List() of EmailFileAttachment objects that contains the name and binary data of the file attachments.
isHtml Enables users to embed HTML elements in the mail message. Depending on the support of the mail system they use, this can add additional functionality such as hyperlinks to the mail message.

By default, the parameter is set to True, which sets the appropriate property on the outgoing email message.

Syntax


Dim Variable as Long = SendMail(EMailTemplate, addresseeType, addressee, boolean useTenantCustomMailConfiguration, args)
Dim Variable as Long = SendMail(EmailTemplate,addresseetype, addressee, List(Of EmailFileAttachment), boolean useTenantCustomMailConfiguration, param Args)

Arguments

Part Description
EmailTemplate String Communication Template code or variable.
addresseeType Integer 0=User, 1=Role, 2=Group.
addressee String array, list of specific User/Group/Role codes or variables.
EmailFileAttachments A .Net List() of EmailFileAttachment objects that contains the name and binary data of the file attachments.
Args Optional input parameter. A String array of additional values to be replaced by numeric tokens in the Communication Template at runtime. Specify if required.

Description

SendMail returns a 1 if it completes the sending of the email. The message is sent through an external email package that is SMTP compliant. You must set up the system profiles to use an external email package in workflow notifications. External notification requires the SMTP process on the application server.

  • Unlike the Notify function, SendMail does not require a template, but use of a Communication Template provides multi-language options and does not require the entire message body to be created by scripting.
  • The Graphical formatting (color, bold, logos etc.) is allowed only if the Communication Template option is enabled.
  • The live object data in the message text and subject line can be included.
  • Use text functions such as string builder to create message text including live data and line returns when using the template-free syntax.
  • The subject line is specified in the script function when the template-free syntax is used.
  • Multiple system users can be specified in a single SendMail call.
    • When using non-template syntax: You can use functions in workflow such as TableLookup to build a group list before calling SendMail. The string array can only result in user codes.
    • When using the template-based syntax, addresses can be an array of user, group or role codes.
  • When you run an action set with the SendMail functions, emails are only sent to users that do not have Deactivate Email selected.

Example (non template-based syntax)

In this example, ObjProperty retrieves the HAACP function code. A separate email is sent to theFSI and ADMIN users for each file attached to HAACP for the current object.


Function wf_start() As Long
Dim textData As Object = ObjProperty("DOCTEXT.DOC", "", "", 
"HAACP","DOCCODE")
If Not textData Is Nothing AndAlso textData.Length > 0 Then
   Dim tmpStr() As String = cStr(textData).Split("*")
   If tmpStr.Length > 2 Then
      Dim attStr() As String = tmpStr(2).Split("|")
      For i As Integer = 0 to attStr.Length - 1
         If IsBlank(attStr(i)) = 0 Then
            SendMail(New String() {"FSI", "ADMIN"},"See attached", 

            "This is the body of the message",

            New String() {attStr(i)}, False)
         End If
      Next i
   End If

End If

Example (template-based syntax)

In this example, because the first argument of SendMail is a string (Communication Template code) rather than an array (String array of user codes), the system knows the template-based function is in use.

This example sends an e-mail to members of the RL_QA role, using the QATEST_REVIEW Communication Template, passing in nine additional arguments (global or local variables defined elsewhere). These extra arguments are identified as [%1], [%2], [%3], etc. in the Communication Template.

dim sUser as string = "RL_QA"
dim ExtraArgs() as string = {sClassName, sObjectCode, sDescription,_sourceuser, _WipId, _ActionSetCode, sWorkflowDesc, sStatusDesc, sDateTime}
dim arrUser() as string = {"RL_QA"}
SendMail("QATEST_REVIEW",arrUser, 1, false, ExtraArgs)

EmailFileAttachment object

This object is used for transporting the raw data of the file attachment and the file's name between the secured scripting layer and the Optiva application.
Public Class EmailFileAttachment
	Public BinaryData As Byte()
	Public FileName As String

	Public Sub New(ByVal binaryData As Byte(), ByVal fileName As String)
		Me.BinaryData = binaryData
		Me.FileName = fileName
	End Sub
End Class

Simple Example

To simulate fetching file attachments from IDM, this example will use the WebReports Virtual Directory as the place where the files to be attached to the email are stored on disk. The Optiva workflow will use an HTTP call to retrieve the contents of the file.

Typically WebReports is located at C:\inetpub\wwwroot\FsWebReports and the PDF output files are in the tmp subfolder.

Add some PDF files or record the names of files already in that folder that will be used for the following tests.
Option Strict On
Imports Formation.Shared.Defs

Imports System
Imports System.Collections.Generic
Imports System.Net.Http
Imports System.Net.Mail

Class ActionScript
    Inherits FcProcFuncSetEventWF

    Function wf_start() As Long
        Dim attachments As New List(Of EmailFileAttachment)
        Using httpClient As New HttpClient()
            attachments.Add(GetEMailAttachmentFromWebReports(httpClient, "Example.pdf"))
        End Using
        
        Dim emailTo() As String = {"FSI"}
        Dim subject As String = "Optiva SendMail w/ Attachments " & DateTime.Now.ToShortTimeString()
        Dim body As String = "Test from Optiva with attachments and secured scripting."
        Dim rc As Long = SendMail(emailTo, subject, body, attachments, false)
        MessageList("Message sent.")
        Return 111
    End Function

    Function GetEMailAttachmentFromWebReports(httpClient As HttpClient, fileName As String) As EmailFileAttachment
        Dim url As String = FileLocation("WEBREPORTS", "URL") & "/tmp/" & fileName
        Dim fileData As Byte() = httpClient.GetByteArrayAsync(url).Result
        Return New EmailFileAttachment(fileData, fileName)
    End Function
End Class

Complex example

In this example, multiple file attachments are being made to the email. This example calls the HTTP resource in a parallel fashion making for a faster running script, particularly if there are many files and large files being attached.

Option Strict On

Imports Formation.Shared.Defs

Imports System
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net.Http
Imports System.Threading.Tasks

Class ActionScript
 Inherits FcProcFuncSetEventWF

    Function wf_start() As Long
        Dim fileNames As New List(Of String)() From {"Example1.pdf", "Example2.pdf", "Image1.png", "Image2.jpg", "SummaryDocument1.docx"}
        Dim fileAttachments As New ConcurrentBag(Of EmailFileAttachment)
        Using httpClient As New HttpClient()
                Parallel.ForEach(fileNames, Sub(fileName)
                                                            fileAttachments.Add(GetEMailAttachmentFromWebReports(httpClient, fileName))
                                                         End Sub)
        End Using

        Dim emailTo() As String = {"FSI"}
        Dim subject As String = "Optiva SendMail w/ Multiple Attachments in parallel " & DateTime.Now.ToShortTimeString()
        Dim body As String = "Test from Optiva with multiple attachments and secured scripting."
        Dim rc As Long = SendMail(emailTo, subject, body, fileAttachments.ToList(), false)
        Return 111
    End Function

    Function GetEMailAttachmentFromWebReports(httpClient As HttpClient, fileName As String) As EmailFileAttachment
        Dim url As String = FileLocation("WEBREPORTS", "URL") & "/tmp/" & fileName
        Dim fileData As Byte() = httpClient.GetByteArrayAsync(url).Result
        Return New EmailFileAttachment(fileData, fileName)
    End Function
End Class