Link to home
Start Free TrialLog in
Avatar of Jaziar
Jaziar

asked on

Need help with a agent

I have a agent that runs through each New or Modified doucument in my inbox.  The agent is designed to create and send a email from some data in the body of the email and then it marks the document read.  The agent runs great.  The problem I am having is if a document contains wrong information and the shortname can not be found in the address book.  The agent stops and the rest of the documents are not processed.  
Example:  

If I have 5 unread documents in my inbox
The agent runs
The first document has a correct shortname
A email is sent and that document is marked read
The agent then goes to the second document
If the shortname is invalid
The agent stops there and the rest of the documents are not processed.

It would be nice if the agent would skip the bad document and finish the rest of the list.  I will attach the agent code
Sub Initialize
	Dim s As New NotesSession
	Dim db As NotesDatabase
	Dim c As NotesDocumentCollection
	Dim cur As NotesDocument
	
	Set db = s.CurrentDatabase
	Set c = db.UnprocessedDocuments
	
	Set cur = c.GetFirstDocument
	Do Until cur Is Nothing
		If Not cur.HasItem( "flagProcessed" ) Then                'if it doesn't have flag - process it
			Call SendMemoFromTag( cur, "UserName", Chr(10) )
			Call cur.ReplaceItemValue( "flagProcessed", "1" )      'flag the processed e-mail
			Call cur.Save( True, False, True )                                'you must save the change in the doc
		End If
		Set cur = c.GetNextDocument( cur )
	Loop
End Sub
 
 
 
 
Sub SendMemoFromTag(memo As NotesDocument, Byval tagStart As String, Byval tagEnd As String)
	
	Dim sess As New NotesSession
	Dim parts As Variant
	Dim body As String, sendto As String
	sess.ConvertMime = True
	body = memo.GetItemValue("Body")(0)
	If (Instr(1, body, tagStart, 5) = 0) Then Exit Sub
	parts = Split(body, tagStart)
	If (Instr(1, parts(1), tagEnd, 5) = 0) Then sendto = Trim(parts(1)) Else sendto = Trim(Strleft(parts(1), tagEnd))
	
	Dim memo2 As NotesDocument
	Set memo2 = memo.ParentDatabase.CreateDocument
	
	line2 = "Requester Name: " & sendto & Chr(10)
	line3 = "Your job has been completed by the Prototype Technology Center." & Chr(10) & Chr(10)
	line4 = "Please do not respond to this email.  This email is for notification purposes only" & Chr(10)
	line5 = memo.Body & Chr(10)
	
	Memo2.ReplyTo = "Please Do Not Reply"
	memo2.Form = "memo"
	memo2.Body = line5 +  line3 + line4 
	memo2.SendTo = sendto
	memo2.Subject = memo.Subject
	Call memo2.Send(False)
	
End Sub

Open in new window

Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Sure, just add an error trap to SendMemoFromTag.  The example below simply ignores all errors, but you could add code to the CATCH block to handle the error.  For example, you could log that the message was not sent.
Sub SendMemoFromTag(memo As NotesDocument, Byval tagStart As String, Byval tagEnd As String)
        On Error Goto CATCH
        Dim sess As New NotesSession
        Dim parts As Variant
        Dim body As String, sendto As String
        sess.ConvertMime = True
        body = memo.GetItemValue("Body")(0)
        If (Instr(1, body, tagStart, 5) = 0) Then Exit Sub
        parts = Split(body, tagStart)
        If (Instr(1, parts(1), tagEnd, 5) = 0) Then sendto = Trim(parts(1)) Else sendto = Trim(Strleft(parts(1), tagEnd))
        
        Dim memo2 As NotesDocument
        Set memo2 = memo.ParentDatabase.CreateDocument
        
        line2 = "Requester Name: " & sendto & Chr(10)
        line3 = "Your job has been completed by the Prototype Technology Center." & Chr(10) & Chr(10)
        line4 = "Please do not respond to this email.  This email is for notification purposes only" & Chr(10)
        line5 = memo.Body & Chr(10)
        
        Memo2.ReplyTo = "Please Do Not Reply"
        memo2.Form = "memo"
        memo2.Body = line5 +  line3 + line4 
        memo2.SendTo = sendto
        memo2.Subject = memo.Subject
        Call memo2.Send(False)
        Exit Sub
CATCH:
        Exit Sub
End Sub

Open in new window

Avatar of Jaziar
Jaziar

ASKER

Made the change waiting to see if it works - thanks for the quick repsonse
Avatar of Jaziar

ASKER

One quick question - all the emails the agent sends out, has me as the sender.  I am guessing it because I wrote the agent.  Is there a way to have the sender as a generic person, something like Tool Shop?
I think your Catch is working, but none of the documents have been wrong so far.
Avatar of SysExpert
You either need to set up a User ID for this and have it sign the agent or use the workaround of sending via SMTP via the Server mail box directly, messy but works.


I hope this helps !
>> "Is there a way to have the sender as a generic person, something like Tool Shop?"

Yes.

SysExpert is right, that you can sign the agent using the id that you want to use for the sender, but if you are using Notes R6 or later, all you have to do is select the user in the Agent's 'Run on behalf of' property (see screen shot below).

Also, make sure that you set the 'From' and 'Principal' fields to the sender's name.

        Memo2.ReplyTo = "Please Do Not Reply"
        memo2.From = "Some User/Organization"
        memo2.Principal = "Some User/Organization"
        memo2.Form = "memo"
        memo2.Body = line5 +  line3 + line4
        memo2.SendTo = sendto
        memo2.Subject = memo.Subject
AgentRunOnBehalfOf.gif
I just ran a quick test to see how hard it is to spoof the reply address.  Here's what I tried:

Sub Initialize
      Dim sess As New NotesSession
      Dim db As NotesDatabase
      Dim memo As NotesDocument
      Set db = sess.CurrentDatabase
      Set memo = db.CreateDocument
      memo.Form = "memo"
      memo.From = "Do Not Reply"
      memo.Principal = "Do Not Reply"
      memo.ReplyTo = "Do Not Reply"
      memo.Subject = "ReplyTo Test"
      memo.Body = "Please do not reply"
      Call memo.Send(False, Split("My Name/Org,myaccount@gmail.com", ","))
End Sub

Notice that one copy goes to my Notes account and another goes to my gmail account.  Also, the agent was signed by myself with no value in the agent's 'Run on behalf of' property.

Results:

In the Notes client, the sender is "Do Not Reply", but it also shows "Sent by: My Name/Org".  When I reply, the reply attempts to send to "Do Not Reply".  So, the only way to truly spoof the Notes client is to sign the agent with another id or use the agent's 'Run on behalf of' property.

In gmail, the spoof is complete.  There is no mention of my name, only "Do Not Reply".

So, in a nutshell, to spoof the Notes client, you need to use another id, but for external mail, setting From, Principal, and ReplyTo is enough.
Avatar of Jaziar

ASKER

Sorry about the delay in repsonse.  I am soory that I kind of got off task with the run by question.  When I used the ERROR / CATCH method, I can only guess that it worked.  Right now when new documents come in to my inbox they are red(usread) and once the agent runs they turn black(read).  So now when the agent stops becuase of a invalid shortname it will be red(unread), but it stops the rest of the documents from being runned by the agent.  I know where the problem is becuase of the red(unread) document.  When using the code above, all the documents are black(read), so I do not know which ones where valid and which ones where not.  What I am really needing is when I open the in-box the ones still red(unread) are the prtoblems and the ones black(read) are good and I know they have ran.

I really need to get this working!!
ASKER CERTIFIED SOLUTION
Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jaziar

ASKER

That Works Great!!!  Thank you so much