Link to home
Start Free TrialLog in
Avatar of nike_golf
nike_golfFlag for Afghanistan

asked on

Looking for a way to run a batch after an email arrives????

Anyone have any way to run a batch file (.bat) or generate a file in a watched folder after an email arrives in your inbox?

I'm looking to kick off a process after an email arrives from one of our server admins.

I'm open to suggestions or other ideas but it would need to based upon receiving an email from the admin.

Thanks.

NG,

Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Don't completely understand...

A mail comes in and then you want to start a (seemingly unrelated) batch file? Doing what exactly? Does it have to read the mail? Does it have to respond to the mail? Or is just a trigger enough? Why does it have to be a batch file?

Heavily assuming here: you could write an agent that opens the mail, reads it and generates a batch file in some external directory on disk, where a scheduled task picks it up and processes it.
Yes, or have the mail sent to a special mailbox which is a mail-in database instead and then have an agent triggered by the mail coming in then (which means it does not have to run for each mail in your normal inbox).
If running on server you can create an agent that is scheduled to run on event (after new mail arrives)
In this agent run the test against from field to see if it comes from admin then use the shell() command to run your batch script.

Make sure agent run time security is set to level 2. allow restricted operations.

On a server the agent will run as the localsystem account unless server service is setup to run as a specific account.
Avatar of nike_golf

ASKER

Sorry for being so vague.

A mail comes in and then you want to start a (seemingly unrelated) batch file? yes.

Doing what exactly? running some sql scripts

Does it have to read the mail? no, just to know who the sender was and what time it arrived, after 8:15 a.m.

Does it have to respond to the mail? no.

I have been looking at Agents but can't seem to get it to work maybe a little help and it could be the correct way to go. Also, I'm a bit lost on the correct syntax, etc.

The Agent seems to identify that an email arrives but never runs? If I "Test" it seems to identify that it has found a message that meets the criteria, so it appears to be setup correctly. I've attached pictures below.





image1.png
image2.png
image3.png
image4.png
image5.png
Meant to also attach the ACL.
image6.png
Since I'm very new to writing an Agent is there a quick way to get it to generate a text box with "hello world"?

NG,
It might seem funny, but it isn't: no, you can't do that, for the simple reason that the agent runs on the server.

Maybe this page can help you:
http://stackoverflow.com/questions/2291073/lotus-notes-export-emails-to-plain-text-file
As Sjef mentions you would need administrative level access to make an agent run a script on the server on new mail as it would need to run with unrestricted access (adjust setting in image5)

it would be feasible to have a script run on a local workstation if you change the local setting to allow local scheduled agents (Preferences) and have the agent run when documents are created or modified. On agent schedule set on server to be local.

As for writing agents the basic help in Notes Designer is a good starting point but you will need some base programming knowledge to get you started writing in lotusscript or Java.

As a starting point you would need to look into NotesDocumentCollection and UnprocessedDocuments method to get a list of documents and then loop to look for the specific email you want.
Well I just got the Agent to run a "Simple Action". So, at least I know its working...

Would the sub below go in the "Initialize" section as opposed to the "Action" in the Agent? When is the "Initialize" section supposed to run?

Sub runbatch()
On Error Resume Next
Dim intTemp1 As Integer
intTemp1 = Shell("\\servername\Batch_Files\test.bat")
On Error Goto Errormessage
Exit Sub

Errormessage:
Msgbox "Batch file error !!"
Exit Sub
exit
End sub
I'd never run a batch file started by the server. The batch job may take a long time, so server operation could be severely hindered by it. Other agents might be blocked, the batch might be cancelled while executing, and worst case you might crash the server (unlikely). I'd always have some external batch processor scheduled to execute the batch files.

The script you supplied contains some mistakes and typos. Here a corrected one if you want to try it:

Sub Initialize
      On Error Goto Errormessage
      Dim intTemp1 As Integer
      intTemp1 = Shell("\\servername\Batch_Files\test.bat")
      Exit Sub

Errormessage:
      Msgbox "Batch file error !!" ' will be printed in the server's log.nsf database
      Exit Sub
End Sub
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland 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
Thanks for ideas and hanging with me.

The strange thing I encountered was that I was only able to run a "Simple Action" script only once, I couldn't get it to run again. Maybe trying to run the batch file has something to do with that?

I think I follow that running the batch file on the server isn't a good thing which I now understand is where the batch file would be executed, not on the client machine. Is that because the agent runs on the server and not on the client?

So, assuming that is the case then the better route is to create a file in a watched folder which is what dragon-it is suggesting.

"Perhaps better would be to have the email trigger the creation of a flag file, and a scheduled task to run periodically on the server watching for that flag file and executing your batch file contents, i.e."


Would this code be placed in the "Initialize" area of the Agent?

Dim filename As String
Dim filenum As Integer
filenum=Freefile
filename = "d:\batches\flagfile.txt"

If Dir$(filename)<>"" Then
      Msgbox "The flag file still exists and is being replaced"
End If


Open filename For Output As #filenum
      Print #filenum,"Flag file created " & Now
      Msgbox "Flag file created"
Close #filenum
OK, making a little progress...

I created a new Agent and the inserted the below into the "Initialize" portion of the code. I created in the Document Section a condition to identify mail from me, sent the Agent properties to run on "After new mail has arrived" saved and then sent myself an email.

Dim filename As String
Dim filenum As Integer
filenum=Freefile
filename = "d:\batches\flagfile.txt"

If Dir$(filename)<>"" Then
      Msgbox "The flag file still exists and is being replaced"
End If

Open filename For Output As #filenum
      Print #filenum,"Flag file created " & Now
      Msgbox "Flag file created"
Close #filenum


Nothing... I got no file no messages, etc. So I took a look at the Agent Log for the Agent that I created and got the 1st image below "never ran".

After that I right clicked on the agent and clicked "Run". It created the file to my amazement... and I also looked at the log got the second screenshot.

So, it seems the "trigger" isn't firing???

image1.png
Untitled.png
OK, so something is up with "Add Condition"

I choose "Author" and then select my name from the address book. It does seem to work as the "trigger"...???


If I right click on the Agent and check the log this is in the log:

Running on new mail messages: 1 total
Found 0 document(s) that match search criteria
add-condition.png
sorry, have been in bed.... Will look back when i can in a bit.
If you want to run a local agent: File/Preferences, select Basic Notes Client Configuration, Enable scheduled local agents

But: this only works with a local database, i.e. a replica of your mail database on the PC's hard disk. Plus, it won't work for mails being received, for the actual reception happens on the server. Hence, you might have to run a local agent triggered on When documents are created or modified. The agent then has to check whether it's a mail that falls in the category, and only then it'll run the batch. In that case, the flag file would serve little point, I think.

@Steve, what do you think?
@Steve: wake up man!!  :-)
@Sjef: Been dealing with unpleasant things that come out of poorly 2 year olds while wife drops others at school...

Can't say I've ever used scheduled local agents as frankly I doubt they would be reliable -- aside from anything else lets say you are on holiday, is this presumably important enough script then not going to get run?!

As an aside what is this email and why does it need to be followed.  Is it some sort of log from a certain person or an instruction saying "run the job now" from a human.

In both cases could it a) the original job or log set off this one, or b) have the person sending the email instead press a button in a database or email say, or shortcut on their desktop?

Steve

Sorry I mistyped.

"I choose "Author" and then select my name from the address book. It does seem to work as the "trigger"...???"

This should have said "It doesn't seem to work as the trigger..."


"If you want to run a local agent: File/Preferences, select Basic Notes Client Configuration, Enable scheduled local agents"

This was not checked so I went ahead and checked it with similar results, the trigger didn't work.


I sent myself an email and then ran a manual test on the Agent, this is the message. Also, the "Condition" I used was "By Field" and then I found "From":

The following will occur when this agent is run:

'CN=my name/OU=Corporate/O=my company' is allowed to run Personal/Formula/Simple Agents on 'CN=MAIL01/O=my company'.
'CN=my name/OU=Corporate/O=my company' has restricted rights to run LotusScript/Java agents on 'CN=MAIL01/O=my company'.
Allow restricted operations flag is selected in this agent, but will be ignored because the signer does not have unrestricted rights.
The agent will run on behalf 'CN=my name/OU=Corporate/O=my company'
Agent will be run by the Agent Manager after new mail messages are delivered.
Home mail server for 'CN=my name/OU=Corporate/O=my company' is 'CN=MAIL01/O=my company'.

Started running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM
Running on new mail messages: 1 total
Found 1 document(s) that match search criteria
Testing:  Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM

From what I read the message was identified correctly as "new" and the "criteria" was met but the Agent itself didn't run it only when ran manually??


When I manually "Run" the Agent this is the output:

Started running agent 'DOR_Arrived' on 09/21/2011 07:06:21 AM
Running on new mail messages: 1 total
Found 1 document(s) that match search criteria
Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:06:21 AM

The file is generated, I can run this manually as many times as I want, each time the file is generated.


When I change the "Condition" to "By Author" and then choose my name I get the following:

The following will occur when this agent is run:

'CN=my name/OU=Corporate/O=my company' is allowed to run Personal/Formula/Simple Agents on 'CN=MAIL01/O=my company'.
'CN=my name/OU=Corporate/O=my company' has restricted rights to run LotusScript/Java agents on 'CN=MAIL01/O=my company'.
Allow restricted operations flag is selected in this agent, but will be ignored because the signer does not have unrestricted rights.
The agent will run on behalf 'CN=my name/OU=Corporate/O=my company'
Agent will be run by the Agent Manager after new mail messages are delivered.
Home mail server for 'CN=my name/OU=Corporate/O=my company' is 'CN=MAIL01/O=my company'.

Started running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM
Running on new mail messages: 1 total
Found 0 document(s) that match search criteria
Testing:  Ran LotusScript code
Done running agent 'DOR_Arrived' on 09/21/2011 07:00:37 AM

I assume this means it doesn't know how to identify the Author correctly... stupid.

Just trying to help you help me... I hope this is helping.

As an aside what is this email and why does it need to be followed.  Is it some sort of log from a certain person or an instruction saying "run the job now" from a human.

This is a system generated email sent to a list of peeps when a server job completes telling them the system is ready for use.
ASKER CERTIFIED SOLUTION
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
too much to read on mobile but sounds like you are checking author field and might need to be different field.  why not select based on subject or something for now?  you could also create a view that only displays your relevant emails by filtering name/subject etc. except ones marked with a flag then process any documents in that view and mark the flag once processed so they only go once.