Jump to content

Looking for Solution / Read GMail Subject > Execute Bat


Recommended Posts

Hey All,

I'm trying to help a friend out, I have some VM's running on Virtual Box, and at the moment, I have Outlook running in the background, checking a 'special' email address, and when the email has certain text in the subject line, then Outlook calls a .bat script, which runs the VM's (start up, or shut down, etc)

I really don't want to have Outlook running ALL the time, and I remembered about AutoIT..

Is it possible to have a script, that can be combined/converted into an application, that can sit in the tray, checking this email account, every couple of minutes, and running the script if it matched a criteria? I've searched these forums and the scripts either don't work, are not complete, or are trying to get the whole email, which I don't want.

If AutoIT can't do this, does anybody know anyway to do this, maybe with a 3rd part app? (I've tried searching google, the moment you put gmail into the search box the results you get, do not help)

Thanks for your time in advance.

Link to comment
Share on other sites

hello. Welcome to AutoIt Forum.

Now I can think in two solutions for handle this. 

1.- Use some IMAP UDF or Library (I don't know if there is one in the forum) to connect with you IMAP Server. 

I noticed that you want to use Gmail, so this in the second solution that came to my mind.

2.-Using Gmail's Atom feeds. I think this would be an easy way to handle this.

You need to create a Application Password. Check here.

Then you could do something like this to, keep reading the Gmail Feeds and perform something based in the mail's subject.

 

So Then you can do something like this. ( Run The Script and Send an Email  You will see the new email subject,email address and date in the SciTE Console)

 

#include <InetConstants.au3>
#include <Array.au3>
#include <StringConstants.au3>
#include <String.au3>
#include <Date.au3>

HotKeySet("{ESC}", "_Terminate")


Local $sData = ''
Local $aEntry = ''
Local $sDate = ''
Local $sEmail = ''
Local $sSubject = ''
Local $iDateCalc = 0
Local $sUserName='' ;Your gmail user (without @gmail)
Local $sApplicationPassword='' ;your application password

While Sleep(5000) ;Check every 5 secounds
    $sData = InetRead('https://' & $sUserName& ':' & $sApplicationPassword & '@mail.google.com/mail/feed/atom/unread/', $INET_FORCERELOAD)
    $sData = BinaryToString($sData, $SB_UTF8)
    $aEntry = _StringBetween($sData, '<entry>', '</entry>')

    For $i = 0 To UBound($aEntry) - 1
        $sDate = _ConvertDate(_GetItem($aEntry[$i], 'issued'))
        $sEmail = _GetItem($aEntry[$i], 'email')
        $sSubject = _GetItem($aEntry[$i], 'title')
        $iDateCalc = _DateDiff('s', $sDate, _NowCalc())
        If Abs($iDateCalc) < 30 Then
            ConsoleWrite("+This is a New Message..." & @CRLF)
            ConsoleWrite(">Subject: " & @TAB & $sSubject & @CRLF)
            ConsoleWrite(">Date: " & @TAB & $sDate & @TAB & "Diff: " & $iDateCalc & @CRLF)
            ConsoleWrite(">Email: " & @TAB & $sEmail & @CRLF)
            ConsoleWrite(@CRLF)
            ;check $sSubject and do something
            Sleep(30 * 1000) ;wait 30 seconds
        EndIf
    Next
WEnd

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Func _ConvertDate($sDate)
;~ ConsoleWrite($sDate & @CRLF)
    $sDate = StringReplace($sDate, "-", "")
    $sDate = StringReplace($sDate, "Z", "")
    Local $sYear = StringLeft($sDate, 4)
    Local $sMonth = StringMid($sDate, 5, 2)
    Local $sDay = StringMid($sDate, 7, 2)
    Local $sHour = StringMid($sDate, 10, 2)
    Local $sMinute = StringMid($sDate, 13, 2)
    Local $sSecond = StringMid($sDate, 16, 2)
    Local $sEncodedGMT = _Date_Time_EncodeSystemTime($sMonth, $sDay, $sYear, $sHour, $sMinute, $sSecond)
    Local $sLocalTime = _Date_Time_SystemTimeToTzSpecificLocalTime(DllStructGetPtr($sEncodedGMT))
    $sLocalTime = _Date_Time_SystemTimeToDateTimeStr($sLocalTime, 1)
    Return $sLocalTime
EndFunc   ;==>_ConvertDate


Func _GetItem($sData, $Item)
    Local $aItem = _StringBetween($sData, $Item, StringReplace($Item, '<', '</'))
;~ _ArrayDisplay($aItem)
    If IsArray($aItem) Then Return StringReplace(StringReplace($aItem[0], '>', ''), '</', '')
EndFunc   ;==>_GetItem

Saludos

 

 

 

 

 

Edited by Danyfirex
typo
Link to comment
Share on other sites

Thanks for this, this is at least getting me in the right direction, it looks like I can tweak a few things, and I know were to get the information I need to fill in the blanks.

From experience, reducing the 30 minute check

Sleep(30 * 1000) ;wait 30 minute

to something lower, will that cause a problem with GMail? i.e. down to 5 minutes? that being said, is that actually what is happening? as further up

While Sleep(5000) ;Check every 5 secounds

it looks like your checking every 5 seconds? or am I misreading the script? (totally new this AUTOIT)

Also, where you have put

;check $sSubject and do something

I'm guessing that is where I'd need to put in something to check if the subject line matched a certain criteria, like, if the subject line says.

if $sSubject = "Something Here" then Run("C:\bat_script.bat")  ?

"Boot Linux VM"  would execute "c:\start_linux_vm.bat" and "Shutdown Linux VM" would execute "c:\shutdown_linux_vm.bat" [wording/location for example purposes only]

not to sound like a n00b (but I'm going too), how would I do this?

Link to comment
Share on other sites

Hello. I did some typo I'm my above post.

Here I wait 30 seconds(I put minutes just a typo :S) after I run something that match my criteria. ( I go to edit first post to change minute for seconds)

leep(30 * 1000) ;wait 30 minute

I check every five seconds the Gmail Atom Feeds ( you can set less econds But maybe slep(1000) = 1 Seconds or Sleep(3000) =3 seconds

I think check Atom Feeds every 5 seconds is enough at least for me. I dont think you get problem with gmails considering that this is something like a RSS feature.

While Sleep(5000) ;Check every 5 secounds

 

to match your criteria do something like this:

;check $sSubject and do something
            If StringInStr($sSubject,"Boot Linux VM") Then
                Run("C:\start_linux_vm.bat") 
            EndIf
            
            If StringInStr($sSubject,'Shutdown Linux VM') Then
                Run("C:\shutdown_linux_vm.bat")
            EndIf

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

I thank you very much for your time, your effort and your patience with me.

I managed to get it up and running, changing a few things, and I even delved into google to find a few things, like I've managed to change the tray icon, and I now have it flashing when it does something, and then stops flashing after the 30 seconds. ;)

It's a start...

If I could buy you a beer I would, if you don't drink, it's the thought that counts. :)

 

Link to comment
Share on other sites

Hello. glad for helping you. thank you for the virtual beer. (I don't drink.) :-)

 

Regards

Edited by Danyfirex
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...