SendMail

You can use this function for Optiva workflows.

Purpose

The function SendMail sends an email to the system users that includes a file attachment or a file path.

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

long SendMail(string EMailTemplate, int addresseeType, string[] addressee, bool useTenantCustomMailConfiguration, params string[] args)

Arguments

Part Description
EMailTemplate The code of the Email template form to be used. This defines the message format and content.
addresseeType
  • A numeric code that tells the system what type of recipient you are sending to.
  • Values to be used - >"0" for User, "2" for Group, "1" for Role.
addressee(User/Group/Role) The recipient of the notification. Can be a specific user, a group, or a role.
useTenantCustomMailConfiguration The configuration flag or setting used in software systems, especially in multi-tenant applications, to determine whether an individual tenant (customer, client, or user group) uses their own custom email configuration rather than a global or default email setup.
  • true: The system uses the tenant-specific/custom email settings.
  • false: The system falls back to a global or default email configuration.
args Optional. The String array has additional values that are replaced by numeric tokens in the Communication Template at runtime.
Example:
SendMail("NEW_TEMPLATE2", 0, "FSI", false);

Arguments

Part Description
EmailTemplate NEW_TEMPLATE2
addresseeType 0
addressee FSI
useTenantCustomMailConfiguration false

Syntax

long SendMail(string EMailTemplate, int addresseeType, string[] addressee, List<EmailFileAttachment> attachments, bool useTenantCustomMailConfiguration, string[] args)

Arguments

Part Description
EmailTemplate The code of the email template form to be used. This defines the message format and content.
addresseeType

The numeric code that tells the system what type of recipient you are sending to. The values to be used are

  • >"0" for User
  • "2" for Group
  • "1" for Role
addressee(User/Group/Role) The recipient of the notification. Can be a specific user, a group, or a role.
useTenantCustomMailConfiguration The configuration flag or setting used in software systems, especially in multi-tenant applications, to determine whether an individual tenant (customer, client, or user group) uses their own custom email configuration rather than a global or default email setup.
  • true: The system uses the tenant-specific/custom email settings.
  • false: The system falls back to a global or default email configuration.
args The String array has additional values that are replaced by numeric tokens in the Communication Template at runtime.

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

Arguments

Part Description
EmailTemplate NEW_TEMPLATE2
addresseeType 0
addressee FSI,Admin
useTenantCustomMailConfiguration false
This example sends an email to specified recipients (FSI, ADMIN) and attachments using a given template NEW_TEMPLATE2, without applying tenant-specific email settings.
List<string> fileNames = new List<string> {"Example1.pdf", "Example2.pdf", "Image1.png", "Image2.jpg", "SummaryDocument1.docx"};
SendMail("NEW_TEMPLATE2", 0, "FSI;ADMIN", fileNames, false);

Purpose

To send an email notification from the system to one or more recipients, optionally with attachments, using either the default or tenant-specific email configuration.

Syntax

SendMail(ref string[] addressees, string subject, string message, ref string[] attachments, bool isHtml = true, bool useTenantCustomMailConfiguration = false)

Arguments

Part Description
addressees
  • An array of recipients of the notification means a list of one or more people or entities who receive the email when the Sendmail function is executed. (Can be a specific user, a group, or a role.)
  • The addressee’s array is passed by reference so the system can validate recipients, remove invalid or duplicate entries and update the existing array.
  • After execution, addressees can contain actual email addresses.
subject The email subject line.
message The body of the email. This can be plain text or HTML depending on isHtml (support of the mail system).
attachments
  • An array of strings representing file names (including full paths) that are attached to the email.
  • The attachments array is passed by reference so that the values in the array can be updated in the cache.
isHtml (Optional)
  • 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 html support on the outgoing email message.
useTenantCustomMailConfiguration [Optional]
  • It is typically a configuration flag or setting used in software systems, especially in multi-tenant applications, to determine whether an individual tenant (customer, client, or user group) uses their own custom email configuration rather than a global or default email setup.
  • If the argument is empty, it considers the default email setup.
  • Options:

    • true: The system uses the tenant-specific/custom email settings.

    • false: The system falls back to a global or default email configuration.

Example

string[] addressees = {"FSI", "ADMIN", "FSI_FR"};
	List<string> fileNames = new List<string> {"Example1.pdf", "Example2.pdf"};
	string message = GetMessageText("FORMULACALCFAILED", ref msg);
	SendMail(ref addressees, "Workflow updates", message, ref fileNames);

Arguments

Part Description
addressees
  • An array of recipients of the notification means a list of one or more people or entities who receive the email when the SendMail function is executed. (Can be a specific user, a group, or a role.)
  • The addressee’s array is passed by reference so the system can validate recipients, remove invalid or duplicate entries and update the existing array.
  • After execution, addressees can contain actual email addresses.
subject The email subject line.
message The body of the email. This can be plain text or HTML depending on isHtml (support of the mail system).
attachments
  • An array of strings representing file names (including full paths) that are attached to the email.
  • The attachments array is passed by reference so that the values in the array can be updated in the cache.
isHtml (Optional)
  • 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 html support on the outgoing email message
useTenantCustomMailConfiguration [Optional]
  • The configuration setting used in software systems, especially in multi-tenant applications, to determine whether an individual tenant (customer, client, or user group) uses their own custom email configuration rather than a global or default email setup.
  • If the argument is empty, it considers the default email setup.
  • Options:

    • true: The system uses the tenant-specific/custom email settings.

    • false: The system falls back to a global or default email configuration.

Example

string[] addressees = {"FSI", "ADMIN", "FSI_FR"};
	string[] attachments = {"Example1.pdf", "Example2.pdf"};
	string message = GetMessageText("FORMULACALCFAILED", ref msg);
	SendMail(ref addressees, "Workflow updates", message, ref attachments);