Outlook rule to run VBA script to pass the body of email to external program - email

I've set up an Outlook rule that filters emails. I want to run an external program (python script) to parse each such email.
I know of the SHELL function, but I need a way to pass the body of the email to my external program.

Google is your friend for this one, I got this snippet by searching "outlook vba script".
Basically for the body of the email you want to pass Item.Body to your python script.
http://support.microsoft.com/kb/306108
Sub CustomMailMessageRule(Item As Outlook.MailItem)
MsgBox "Mail message arrived: " & Item.Subject
End Sub`
Sub CustomMeetingRequestRule(Item As Outlook.MeetingItem)
MsgBox "Meeting request arrived: " & Item.Subject
End Sub

You'd need a VBA script to parse python in outlook.
Press alt+F11. You will get a VBA window.
Sub python(Item As Outlook.MailItem)
Shell ("python C:\path\tp\your\filename.py")
End Sub
I hope you have set windows variable path for python.
Shell command passes the command to windows shell prompt. You can test this by running your python script in command prompt. If it is working there, then it should work here as well.

Related

How to create mail using Mail::Outlook with Outlook 2007

I am trying to create a mail using Mail::Outlook. I followed this answers I believe is correct:
Sending email using Perl
Mail::Outlook CPAN
I created a simple code based from the tutorials:
use strict;
use warnings;
use Mail::Outlook;
use Data::Dumper;
my $outlook = new Mail::Outlook();
print Dumper($outlook);
print Dumper(Win32::OLE->LastError()); #added in response to comment
my $message = $outlook->create();
$message->To('sample#gmail.com');
$message->Cc('another#gmail.com');
$message->Subject('Testing sending mail from perl');
$message->Body('Hi, This is the body! wahahah!');
$message->save();
1;
The emails I used are real but I replaced it here for privacy's sake..
When I run the script, an error appeared:
$VAR1 = undef;
Can't call method "create" on an undefined value at send_mail.pl line 14.
It seems that the variable $outlook did not initialized during new Mail::Outlook(). The module Mail::Outlook returns undef if initiating a new object failed.. Now, I wonder why this happened.. I am thinking it was because of security issues of outlook but I don't know how to tweak that. Please perl masters out there, if anyone has the same experience or have encountered this, it would be helpful..
I am using Microsoft Outlook 2007 in windows 7 and I installed ppm install Mail-Outlook.
My main question is: How can I create a mail using Mail::Outlook in Outlook 2007
UPDATE
I tried using print Dumper(Win32::OLE->LastError()); and it printed this error:
$VAR1 = 'Win32::OLE(0.1709) error 0x80080005: "Server execution failed"';
After following what Tim Tom has instructed, with a little search, I saw an article about the error Win32::OLE(0.1709) error 0x80080005: "Server execution failed"
COM Process Elevation Mismatching
It says that the access level of the outlook application and perl script must be the same:
To make a long (and frustrating) story short, the problem was that I was running the script from a CMD.EXE window which was elevated (“Run as Administrator”). When I would run Outlook from a non elevated process (as a normal user would) there appeared to be a process elevation mismatch.
this is the same in my case.. I was running my cmd as Administrator while my outlook was running normally..
MSDN has a say to this:
COM security is aware of integrity levels and does not allow lower-integrity clients to bind to class instances running at a higher integrity level.
after changing my command line with the same elevation level as the outlook app, the perl script worked perfectly!
Note: perl crashes while using print Dumper(Win32::OLE->LastError()); if it has no errors..

Trying to send an email through a groovy shell script

today I was trying to replace a bash script with a groovy script. Everything runs smooth and I managed to use the execute() command to invoke other command.
Then I was trying to send an email with a subject:
mail -s "this is a test" my.mail#example.com < mail.tmp
turned into
'mail -s "this is a test" my.mail#example.com < mail.tmp'.execute()
does not work since groovy will split up the one argument "this is a test" into four arguments "this is a test".
So far so good. Google helped me to turn this into
['mail', '-s', "this is a test", 'my.mail#example.com', '<', 'mail.tmp'].execute()
Now the subject is recognized as one parameter, but the < is also recognized as parameter and not as the file redirection.
Any idea how I could solve this?
PS: no, I would not like to use java code for sending mail since I guess the code will be more complex. But if you have a java one-liner, you are welcome...
You'll have to handle writing the output from the process to a file ...
new File('mail.tmp').withWriter { it << """mail -s "this is a test" my.mail#example.com""".execute().getText() }
I only tested above with "ls -al" as the command and it worked as expected, I'm not sure if a longer running process would require you to tweak the way you go about it-- if so you might need to use waitForProcessOutput:
new File('mail.tmp').withWriter { """mail -s "this is a test" my.mail#example.com""".execute().waitForProcessOutput(it, it) }
Found another solution which looks easier to me, but I guess isn't as clean as the one provided by #chrixian:
['sh','-c','mail -s "this is a test" my.mail#example.com < mail.tmp'].execute()
this command creates another shell in order to execute the mail command. This way, the 'mail -s "this is a test" my.mail#example.com < mail.tmp' is interpreted by a shell and it knows how to correctly handle the parameters and < symbol.
Drawback: it works on *nix systems. For windows systems, the shell is executed in a different way.

Email an alert when sFTP connection cannot be reached?

I have a very small office environment, and my team sends created pdfs to an sFTP server daily.
Occasionally, I will get a call that someone can't log in to upload the files.
My normal course of action is to connect to the sFTP server myself, run a commmand like ls to determine it is responding.
I would like to be able to automate this with notification if there is a failure:
Login to the sFTP server (with credentials).
Run an LS command
Email if connection times out or login fails.
I have limited experience with writing Batch files, but I can't seem to figure a way to get only a 'failed' / no response to send an email.
Could anyone help with ideas? I'd like to run this as a VB or Batch in Scheduled Tasks, as I have a Server 2000 machine this could run on. I know batch has issue sending emails, but i have another batch file that uses Blat.exe to send an email with passed variables, so i could use that if i could get batch to send failed responses...
You should be able to do this with a batch file.
Create a file called logon.ftp. This file contains the FTP logon script. Mine contains:
open Ftp_server
ftpuser
ftppassword
ls -l
quit
The testftp.bat file:
ftp.exe < logon.ftp | grep "Not connected" > nul && call :alert_someone
#echo Logon successful
goto exit
:alert_someone
#echo %date% %time% > alert.txt
#echo ftp_server appears to not be taking logins. >> alert.txt
blat alert.txt -to you -from ftp_watcher -subject "alert %date% %time% ftp_server not taking logins"
:exit
You'll need to get blat, and grep so you can do the string checking. My winxp ftp doesnt support errorlevels, so I'm using the errorlevel returned from grepping the 'Not connected' string to figure out if this worked or not.
You can get wget or curl to do this as well, and they do support errorlevels.
Batch files can be a bit too basic for this kind of thing.
If you were able and willing to experiment with the Python programming language ( http://www.python.org ) and additionally install the Paramiko module ( http://www.lag.net/paramiko/ ) then it would be possible to write a script along the lines of...
import paramiko
try:
t = paramiko.Transport(('TheHostname', 22))
t.connect(username='MyUsername', password='MyPassword')
sftp = paramiko.SFTPClient.from_transport(t)
dirlist = sftp.listdir('.')
except:
print "It's Broken"
#Send e-mails and such here
that you could then schedule to run on a regular basis.

Quick, portable way to launch perl script on Mac (equiv. to Batch on Win)

I have a perl script shared in a repository with several users across Mac and Windows machines. It performs its work on files located in the folder with it. Because I run it frequently and because some users don't even know what a command line is, I created a batch file in the directory to make it easy to launch. Because some paths will differ, I made it portable:
::figure out your directory path
pushd %~dp0
:: launch the script
perl my_perl_script.pl
popd
The batch works perfectly on Windows and I put it in each project folder with each perl script and tell my users to just "double-click that thingy."
How can I do the same on Mac? I don't know much about bash, and I can't figure out how to
1) Have a bash file figure out its directory path, and
2) Make the bash a simple "double-click that thingy" executable for my Mac users.
Would a simple Applescript be better (I don't really know Applescript either...)
Thanks for any help.
EDIT: Thanks, dj bazzie wazzie. I didn't want to run both a bash and applescript to get my perl to run, but I did use your first line to get an applescript that works perfectly for me.
set currentWorkingDirectory to do shell script "dirname " & quoted form of POSIX path of (path to me)
tell application "Terminal"
set currentTab to do script "cd " & currentWorkingDirectory
do script "perl xml2epub_3689_7KeysSpWellness.pl" in currentTab
end tell
With AppleScript you have the ability run a shell command. Let's say your bash file is located next to the script and is named maintenance.sh your script would look something like this
set currentWorkingDirectory to do shell script "dirname " & quoted form of POSIX path of (path to me)
do shell script quoted form of (currentWorkingDirectory & "/maintenance.sh")
EDIT: (I can't comment your post so I change my post)
For such a simple command as yours you don't need the terminal application. I can understand that a working application is enough but for a much smoother script I would put that command into a do shell script. So the code for your per command would be something like this
set currentWorkingDirectory to do shell script "dirname " & quoted form of POSIX path of (path to me)
do shell script "perl " & quoted form of (currentWorkingDirectory & "/xml2epub_3689_7KeysSpWellness.pl")

WScript.Echo throwing object required error in vbscript

I'm trying to display messages as my vbscript program runs. It runs off a command prompt in xp, for example: cscript.exe test.vbs. I don't want to use msgBox while this is running as I just want it to post the scripts progress, but I don't want any user interaction.
I tried using Wscript.echo "Some text", but I'm getting compile errors when I step through the program using Words built-in vbeditor.
I found this code and it runs fine in another file:
Option Explicit
Dim strComputer
strComputer = "LocalHost"
WScript.Echo "Computer: " _
& strComputer
WScript.Quit
I then tried using Dim and set to setup a Wscript variable, but that didn't work either.
Any ideas as to what I'm doing wrong? I did verify Wscript is running on this machine.
Thanks,
James
Word uses VBA (Visual Basic for Applications), not VBScript. Although both languages belong to the Visual Basic family, they have differences. One of them is that the WScript object isn't available in VBA - that's why you're getting errors when debugging your script in Word.
Having said that, your code is valid and runs perfectly fine with both cscript and wscript.