views:

1018

answers:

4

I am setting up a Lotus Notes account to accept emails from a client, and automatically save each email as a plain text file to be processed by another application.

So, I'm trying to create my very first Agent in Lotus to automatically export the emails to text.

Is there a standard, best practices way to do this?

I've created a LotusScript Agent that pretty much works. However, there is a bug - once the Body of the memo exceeds 32K characters, it starts inserting extra CR/LF pairs.

I am using Lotus Notes 7.0.3.

Here is my script:

 Sub Initialize
 On Error Goto ErrorCleanup
 Dim session As New NotesSession
 Dim db As NotesDatabase
 Dim doc As NotesDocument
 Dim uniqueID As Variant 
 Dim curView As NotesView
 Dim docCount As Integer
 Dim notesInputFolder As String 
 Dim notesValidOutputFolder As String
 Dim notesErrorOutputFolder As String 
 Dim outputFolder As String
 Dim fileNum As Integer
 Dim bodyRichText As NotesRichTextItem
 Dim bodyUnformattedText As String
 Dim subjectText As NotesItem

 '''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'INPUT OUTPUT LOCATIONS 
 outputFolder = "\\PASCRIA\CignaDFS\CUser1\Home\mikebec\MyDocuments\"
 notesInputFolder = "IBEmails" 
 notesValidOutputFolder = "IBEmailsDone"
 notesErrorOutputFolder="IBEmailsError"
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''

 Set db = session.CurrentDatabase
 Set curview = db.GetView(notesInputFolder ) 
 docCount = curview.EntryCount
 Print "NUMBER OF DOCS "  & docCount
 fileNum = 1
 While (docCount > 0)
  'set current doc to 
  Set doc = curview.GetNthDocument(docCount)

  Set bodyRichText = doc.GetFirstItem( "Body" )
  bodyUnformattedText = bodyRichText.GetUnformattedText()
  Set subjectText = doc.GetFirstItem("Subject")
  If subjectText.Text = "LotusAgentTest" Then
   uniqueID = Evaluate("@Unique")
   Open "\\PASCRIA\CignaDFS\CUser1\Home\mikebec\MyDocuments\email_" & uniqueID(0) & ".txt" For Output As fileNum
   Print #fileNum, "Subject:" & subjectText.Text
   Print #fileNum, "Date:" & Now
   Print #fileNum, bodyUnformattedText
   Close fileNum
   fileNum = fileNum + 1
   Call doc.PutInFolder(notesValidOutputFolder)
   Call doc.RemoveFromFolder(notesInputFolder)
  End If
  doccount = doccount-1
 Wend
 Exit Sub
    ErrorCleanup: 
     Call sendErrorEmail(db,doc.GetItemValue("From")(0))
     Call doc.PutInFolder(notesErrorOutputFolder)
     Call doc.RemoveFromFolder(notesInputFolder)
    End Sub

Update Apparently the 32KB issue isn't consistent - so far, it's just one document that starts getting extra carriage returns after 32K.

+2  A: 

I'm not sure what is causing the 32K bug, but I know there are lots of limitations in the order of 32K or 64K within Lotus Notes, so perhaps you're running into one of those. I can't imagine what would add extra CR/LFs. Perhaps you could try using the GetFormattedText method on the NotesRichTextItem class and see if it fares better?

It's more complicated, but you might also be able to use the NotesRichTextNavigator class to iterate through all the paragraphs in the memo, outputting them one at a time. Breaking up the output that way might eliminate the CR/LF problem.

Lastly I always suggest Midas' LSX for dealing with rich text in Lotus Notes. They sell an add-on that gives you much more control over rich text fields.

As for best practices, one that comes to mind when I read your code is the looping construct. It is more efficient to get the first document in a view, process it, and then get the next doc and check whether it is equal to Nothing. That sets the loop to run through the view in index order, and eliminates the need to search through the index to find the Nth document each time. It also saves you from maintaining a counter. The gist is as follows:

Set doc = curview.GetFirstDocument()
While Not (doc Is Nothing)

    'Do processing here...

    Set doc = curview.GetNextDocument(doc)
Wend
Ken Pespisa
Thanks Ken. By the way, have you ever come across a sample script online to save an email to a text file? All of the examples that I've found involve saving attachments to files, but not the body of an email.
mbeckish
I hadn't, but I just did a quick search and found a couple of examples. One looks like it uses the DXL features of Notes: http://tech.niques.info/projects/lotus-notes-email-export/ The other is written in Perl, but might have some logic you could extract http://www.perlmonks.org/?node_id=136382
Ken Pespisa
+3  A: 

With regards the 32Kb thing, instead of this:

Set bodyRichText = doc.GetFirstItem( "Body" )

... you might want to consider iterating all "Body" fields in the email document. When dealing with large amounts of rich text, Domino "chunks" said content into multiple rich text fields. Check some documents you're processing: you may well see multiple instances of the "Body" field when you look at document properties.

Ben Poole
Thanks Ben - I'll check that out.
mbeckish
+1  A: 

The external eMail most likely comes in as MIME. So you could check the document.hasMime and then use the mime classes to get to the content. Then you don't have a 64k limit. Samples are in the help - or reply if you want code.

stwissel
A: 

I've found a good worked solution there:

http://tech.niques.info/export-lotus-notes-email-free/

Its a set of LotusScripts which help me to export a lot of messages into mbox (RFC) format.

Denis Barmenkov