Send Notes mail from an XML call
Next we’ll take a peak at the “setArrays” function.
Function setArrays(x As Integer, y As Integer, nodeNode2 As NotesDomElementNode) As Boolean
' fuction to set proper array value dependant on index value x
On Error Goto errorCatch
setArrays = False
If x = 0 Then
Redim Preserve arrMailTo(y-1)
arrMailTo(y-1) = nodeNode2.FirstChild.NodeValue
Elseif x = 1 Then
Redim Preserve arrCC(y-1)
arrCC(y-1) = nodeNode2.FirstChild.NodeValue
Elseif x = 2 Then
Redim Preserve arrBcc(y-1)
arrBcc(y-1) = nodeNode2.FirstChild.NodeValue
Elseif x = 3 Then
strMailSubject = nodeNode2.FirstChild.NodeValue
Elseif x = 4 Then
strMailBody = nodeNode2.FirstChild.NodeValue
Elseif x = 5 Then
strFrom = nodeNode2.FirstChild.NodeValue
Elseif x = 6 Then
If Not getAttachments(nodeNode2,y) Then
Exit Function
End If
End If
setArrays = True
Exit Function
errorCatch:
strOut = "Error in doSendMessage:setArrays in line: " & Erl() & " error$: " & Error$
setArrays = False
Exit Function
End Function
In a nutshell, we determine which parameter we are extracting (by using our index “x”), and set the appropriate global. In the case of attachments we call another function since we can have multiple attachments and each attachment has multiple parameters. The “getAttachments” function is very similar to getParams and shares alot of validation and redundency.
Function getAttachments(nodeNode As NotesDomElementNode, y As Integer) As Boolean
' function to grab the fields from the attachments/value node. the only required field is the
' actual mime encoded text string, file name and content type have default values if none are provided
On Error Goto errorCatch
Dim nodeNode2 As NotesDOMElementNode
Dim nodeList As NotesDomNodeList
getAttachments = False
Set nodeList = nodeNode.GetElementsByTagName("mime64")
If nodeList.NumberOfEntries > 0 Then ' if we have one
Set nodeNode2 = nodeList.GetItem(1) ' grab the actual node
If nodeNode2.HasChildNodes Then ' check to see if it has values
Redim Preserve arrMime64(y-1)
arrMime64(y-1) = nodeNode2.FirstChild.NodeValue ' grab value
Else
strOut = "when sending an attachment, the mime encoded text is required"
Exit Function
End If
Else
strOut = "when sending an attachment, the mime encoded text is required"
Exit Function ' we do not have a mime64 node
End If
Set nodeList = nodeNode.GetElementsByTagName("file_name")
If nodeList.NumberOfEntries > 0 Then ' if we have one
Set nodeNode2 = nodeList.GetItem(1) ' grab the actual node
If nodeNode2.HasChildNodes Then ' check to see if it has values
Redim Preserve arrFileName(y-1)
arrFileName(y-1) = nodeNode2.FirstChild.NodeValue ' grab value
Else
Redim Preserve arrFileName(y-1)
arrFileName(y-1) = "file"
End If
Else
Redim Preserve arrFileName(y-1)
arrFileName(y-1) = "file"
End If
Set nodeList = nodeNode.GetElementsByTagName("content_type")
If nodeList.NumberOfEntries > 0 Then ' if we have one
Set nodeNode2 = nodeList.GetItem(1) ' grab the actual node
If nodeNode2.HasChildNodes Then ' check to see if it has values
Redim Preserve arrContentType(y-1)
arrContentType(y-1) = nodeNode2.FirstChild.NodeValue ' grab value
Else
Redim Preserve arrContentType(y-1)
arrContentType(y-1) = "application/binary"
End If
Else
Redim Preserve arrContentType(y-1)
arrContentType(y-1) = "application/binary"
End If
getAttachments = True
Exit Function
errorCatch:
strOut = "Error in doMessageSend:getAttachments in line: " & Erl() & " error$: " & Error$
getAttachments = False
Exit Function
End Function
So now we have all of our message parameters resting in our global variables, so it is time to create the message. The first function to do that is the “doMail” function.
Function doMail(s As NotesSession, dbThis As NotesDatabase, strIpAddress As String) As Boolean
' function to send the message
On Error Goto errorCatch
Dim docMail As NotesDocument
doMail = False
Set docMail = dbThis.CreateDocument
If isArrayInit(arrFileName) Then
' Body field will need to be MIME with attachments.
' Setting all other fields after MIME insures no loss of field values
s.ConvertMime = False
If Not doAttachFile(s,docMail) Then
doMail = False
Exit Function
End If
s.ConvertMime = True
Else
docMail.Body = strMailBody
End If
docMail.Form = "Memo"
docMail.Principal = strFrom
docMail.SendTo = arrMailTo
docMail.CopyTo = arrCC
docMail.BlindCopyTo = arrBcc
docMail.Subject = strMailSubject
docMail.IpAddress = strIpAddress
Call docMail.Send(False)
doMail = True
Exit Function
errorCatch:
strOut = "Error in doSendMessage:doAttachFile in line: " & Erl() & " error$: " & Error$
doMail = False
Exit Function
End Function
We create the Body field as a MIME regardless of whether we have any attachments. All other fields for the message are set after the Body field because there was some issue where the e-mail would be missing values that were set before the body. I have a helper function in here that determines if an array has any values in it. The “isArrayInit” function is pretty basic:
Function isArrayInit(a) As Boolean
Dim u As Integer
On Error Goto skip
isArrayInit=True
u=Ubound(a) ' if a is not initialized, this produces an error
Exit Function
skip:
isArrayInit=False
Exit Function
End Function
March 30th, 2006 at 3:30 am
Hi there. Came across this article via codestore http://www.codestore.info/store.nsf/unid/BLOG-20060329, via Johan Känngård http://johankanngard.net/2005/12/13/sending-html-mails-via-lotusscript/ then you!
We have a PC on our network, that is used to run an Excel program, the Excel file is then emailed to various users (they should really stick it in a central repository etc…). The sending user could log into their Web Access mail file, but as an interesting exercise, I would like to give this a go. I keep getting a 404 error when trying to download your sample database.
Great site, interesting articles.
Nick