Adding an attachment to a notification VBScript

Hi, I am trying to create a script that attaches an M-Files generated document to an email notification. Normal email scripts don't seem to work with our servers, so I was wondering if it is possible to download a document from M-Files and attach it to a notifications via a workflow state script with VBScript? I have attached the the piece of code that I would like to use to send the email notification (and attachment) below.


Option Explicit

' User or user group recipients from the vault
Dim oUsers : Set oUsers = CreateObject("MFilesAPI.UserOrUserGroupIDs")

Dim oUser : Set oUser = CreateObject("MFilesAPI.UserOrUserGroupID")
oUser.UserOrGroupID = 1	' User ID 32
oUser.UserOrGroupType = MFUserOrUserGroupType.MFUserOrUserGroupTypeUserAccount

oUsers.Add -1, oUser

' External recipients
Dim oExternalUsers : Set oExternalUsers = CreateObject("MFilesAPI.Strings")
oExternalUsers.Add -1, ""

Vault.NotificationOperations.SendCustomNotification oUsers, False, oExternalUsers, True, "Test email", "Test email body"

  • Hello,

    I think the easiest way to send a notification with attachment is to create a VAF application for that.

    Here you can find how to do it:

    VAF.Extensions.Community/MFiles.VAF.Extensions/Email at master · M-Files/VAF.Extensions.Community · GitHub

  • Hi Kay_G,

    As far as I know API VaultNotificationOperations, there is no method for attachments. If the CDO script is used needs to be sent via SMTP relay that allows the M-Files server to send without authentication or to review the CDO script settings and configure it with a username and password.

    Тhe other option is what Radu has offered.

  • Thanks for the quick repsonse everyone! 

    Will investigate it all. I appreciate the help. 

  • Hi Kay_G

    The following script may help you along with your journey.  It uses a CDO Message object as NeoTron mentioned. I agree with both Radu and Neo that this may be best served as a custom Vault Application, however, it really depends on your scenario and timeframe for needing a solution.  

    Hope this helps...


    option Explicit

    ' Setup variables for e-mailing the documents as attachments - only omessage used currently
    Dim szFileName, oFiles, szExtension, szTitle, szNewFilePath, oMessage, oVaps, oLookups, oLookup
    ' Setup the e-mail object, some of these are not used due to security being turned off on smtp server.
    Set oMessage = CreateObject("CDO.Message")
    oMessage.Configuration.Fields.Item("">") = 2
    oMessage.Configuration.Fields.Item("">") = "mfdemosrv01"
    oMessage.Configuration.Fields.Item("">") = 25
    oMessage.Configuration.Fields.Item("">") = 10
    oMessage.Configuration.Fields.Item("">") = 0
    oMessage.Configuration.Fields.Item("">") = False
    'oMessage.Configuration.Fields.Item("">") = "username"
    'oMessage.Configuration.Fields.Item("">") = "password"
    ' Start building and inserting the message fields

    ' Set the message subject line
    oMessage.Subject = "Monthly Report"
    ' Set the message from line
    oMessage.From = ""

    'Since we want to use the email address property on the workflow item, Grab the Email Address property from alias M-Files.Property.EmailAddress using GetPropertyID and GetPropertyValue helper functions
    Dim iPDEmailAddress : iPDEmailAddress = GetPropertyID("M-Files.Property.EmailAddress")
    Dim txtEmailAddress : txtEmailAddress = GetPropertyValue(PropertyValues, iPDEmailAddress)

    ' Set the email address from the property to the To: field on the email
    oMessage.To = txtEmailAddress
    oMessage.TextBody = "Please find the attached test file. Thank you."

    ' Download the file from the object and save it to the server
    Dim objFiles : Set objFiles = Vault.ObjectFileOperations.GetFiles(objVer)
    Dim ofile
    For Each ofile In objFiles
    Dim sFilePath : sFilePath = "C:\test\" & ofile.GetNameForFileSystem()
    'Err.Raise mfscriptcancel, sFilePath
    Call Vault.ObjectFileOperations.DownloadFile (ofile.ID, ofile.Version, sFilePath)
    ' Attach downloaded document to email message
    oMessage.AddAttachment sFilePath
    ' Send the Message

    Dim filesys : Set filesys = CreateObject("Scripting.FileSystemObject")
    ' Delete the temp file from the M-Files serverFor Each ofile In objFiles
    For Each ofile In objFiles
    Dim sFilePaths : sFilePaths = "C:\test\" & ofile.GetNameForFileSystem()
    filesys.DeleteFile sFilePaths

    ' M-files Helper Functions
    ' Gets the property definition ID by alias
    Function GetPropertyID(alias)
    Dim iID
    iID = Vault.PropertyDefOperations.GetPropertyDefIDByAlias(alias)
    If iID = -1 Then
    Err.Raise MFScriptCancel, "Property definition not found by the specified alias of: " & alias
    End If
    GetPropertyID = iID
    End Function
    ' Gets a property definition value
    Function GetPropertyValue(propVals, propID)
    GetPropertyValue = propVals.SearchForProperty(propID).TypedValue.DisplayValue
    End Function

    ' Gets object version and properties for a given lookup
    Function GetOvapsByLookup( lookup )
    Dim oObjID : Set oObjID = CreateObject( "MFilesAPI.ObjID" )
    Call oObjID.SetIDs( lookup.ObjectType, lookup.Item )
    Dim oObjVer: Set oObjVer = Vault.ObjectOperations.GetLatestObjVer( oObjID, False )
    Set GetOvapsByLookup = Vault.ObjectOperations.GetObjectVersionAndProperties(oObjVer)
    End Function

  • Hi Kay_G - here is a generic vbscript (Event Handler or Workflow Sub) to send email.

    Just pass in your parameters and make sure you have CDO support on the M-Files server ...

    Sub SendEmail(EmailTo, EmailSubjectTest, EmailBodyTest, EmailFrom)

    Dim EmailMsg

    Set EmailMsg = CreateObject("CDO.Message")

    EmailMsg.To = EmailTo
    EmailMsg.From = EmailFrom
    EmailMsg.Subject = EmailSubjectTest
    EmailMsg.HTMLBody = EmailBodyTest
    EmailMsg.Configuration.Fields.Item("">") = 2
    EmailMsg.Configuration.Fields.Item("">") = 1
    EmailMsg.Configuration.Fields.Item("">") = "smtpservername"
    EmailMsg.Configuration.Fields.Item("">") = 25
    EmailMsg.Configuration.Fields.Item("">") = "username"
    EmailMsg.Configuration.Fields.Item("">") = "password"
    EmailMsg.Configuration.Fields.Item("">") = False

    End Sub

  • So we initially had a similar script to send emails that worked great, but the server we now use to send emails fails to communicate with the Gmail SMTP server for some reason; we just keep receiving SMTP error messages in M-files. My next alternative was to try a custom notification script, but cannot add attachments or add new lines in this type of script.

  • Sounds like your security settings in the CDO.Message do not comply with requirements on the SMTP server. CDO is - if I have understood things correctly - an outdated method. I do use it in a couple of places in the lack of better alternatives that come within my limited coding capabilities. In those cases I have configured a local SMTP server to receive the CDO message and forward it to the external SMTP server using secure protocols and settings accepted by that external SMTP server.