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.

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
UserCodes() 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.
FileNames() A string array of files names (with path) assigned to attach to 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.

This parameter defaults to True, which sets the appropriate property on the outgoing email message.

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 an external template file residing on the server or other central location.
  • No graphical formatting (color, bold, logos etc.) are permitted.
  • Can include live object data in the message text and subject line.
  • Use text functions such as string builder to create message text including live data and line returns.
  • The subject line is specified in the script function.
  • Multiple system users can be specified in a single SendMail call. You can use functions in Workflow such as TableLookup to build a group list before calling SendMail.
  • When you run an action set with the SendMail functions, emails are only sent to users that do not have Deactivate Email selected.

Example

In this example, ObjProperty retrieves the HAACP function code. A separate email is sent to the FSI 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

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