When a Mi-Forms session is "Finished" the client can run DataPathExports if they are configured in the form. A common use case is to email a PDF or CSV representation of the data collected to an email address. In this case we email any PDF or CSV files created as a DataPathExport.


In the Script editor, double click AfterDataPathRun on the right side of the screen as seen here:


The script editor will then present you with this code:

  

    <MiCode(FormScriptEventType.AfterDataPathsRun)> _ 
    Public Sub Form_AfterDataPathsRun(ByVal e As AfterDataPathsRunEventArgs)

    End Sub

  

Select the blank space between lines of code here and paste the following code: 

     

	Dim EmailTo As String = "sales@mi-corporation.com"
	Dim EmailToName As String = "Mi-Corporation Sales Engineer"
	Dim EmailFrom As String = "support@mi-corporation.com"
	Dim EmailFromName As String = "Mi-Corporation Support"	

	If _component.Scope = MiCo.MiForms.Scope.Server_Processing Then
        	Dim fileName As String = String.Empty
		Dim erl As New System.Collections.Generic.List(Of ExportResult)
		For Each er As ExportResult In _form.Validator.ExportResults
                   erl.Add(er)
        		fileName = System.IO.Path.GetFileNameWithoutExtension(er.ExpandedFilePath)
               Next

        	Try
            	Dim fromAddress As New MailAddress(EmailFrom, EmailFromName)
            	Dim toAddress As New MailAddress(EmailTo, EmailToName)
            	Using msg As New MailMessage(fromAddress, toAddress)
                    msg.Body = "This will be the body of the message you are sending." & VbCrLf & "Thank you for your purchase."
                    msg.Subject = (Me._form.Name & " (" & fileName & ")")

        			' Add the mail body - an HTML file attached to this form.
        			For Each attData As AttachmentData In _form.Attachments
        				If String.Compare(attData.Name, "Lead Generation.html") = 0 Then
                               msg.Body = System.Text.UTF8Encoding.UTF8.GetChars(attData.BinaryData())
        					msg.Body = msg.Body.Replace("[filename]", fileName)
        				End If
        			Next

        			' Add pdf/csv file attachments to mail - they are datapaths of the form.
        		    For Each er As ExportResult In erl
        				If er.Success And ( er.DataPathName = "PDF" Or er.DataPathName = "CSV" ) Then
        		            msg.Attachments.Add(New Attachment(er.ExpandedFilePath))
               			End If
        			Next        

                    Dim client As New SmtpClient("localhost", 25)
        			client.Send(msg)
        			Me.RecordExportResult("Email", True, "Sent email", "Sent email to " & EmailToName & "(" & EmailTo & ")", "")
                End Using
	        Catch ex As Exception
	            Me.RecordExportResult("Email", False, ex.Message, ex.Message, ex.Message)
	            e.CustomExportFailure = True
	            e.CustomExportDetails = ex.Message
				
	        End Try
	    End If

     

You will want to modify the first four lines of code to reflect the email address of the sender and receiver. As a minor change, you could change the EmailTo address to be defined by a field on the form.

 

Now you have a flexible method to send email notifications of your completed forms!