Sign in to follow this  
Followers 0
dmob

Download attachment from gmail

3 posts in this topic

#1 ·  Posted (edited)

My apologies if a similar question has been asked. I did search around, found tidbits of code

in the forums which I strung together.

I am trying to download an attachment from GMail. After many days of fumbling, I managed to

piece together a rough script to scrape the email and read some text, as well as the download

link for the attachment. It works but is unreliable.

I am now trying to download the attachment. I used Yashied's HTTPGetResponse script to try

and obtain the URL of the attachment from the link. I got what I thought is the link, but when I use

INetGet to download the URL, the HTML code for the GMail login screen is downloaded. I tried

IENavigate and I got the File Save Dialog.

Can someone please point me in the right direction. I have attached a portion of the script.

#include <IE.au3>
#include <String.au3>

Global $sGUser = "dalisuit"
Global $sGPass = "*******" ; PM me if you want to help and need password.

; URL of email where I scraped off Download link for attachment
; _IENavigate($oIE, "https://mail.google.com/mail/?shva=1#inbox/12ee354a7f226a94", 1)

; Download link
Global $sLink =  "https://mail.google.com/mail/?ui=2&ik=4168633e9c&view=att&th=12ee354a7f226a94&attid=0.5&disp=safe&zw"

GetAttachment($sLink)

Func GetAttachment($sUrl)

    ; login to gmail
    $oIE = GmailLogin($sGUser, $sGPass)
    Debug("Logged in")

    Local $sFilename = @DesktopDir & "\Attachment" ; extension intentionally left out
    Local $sResponse = _HTTPGetResponse($sUrl)
    Local $sLocation = sStringBetween($sResponse, "Location: ", @CR)
    Local $nSize = InetGetSize($sLocation)

    Debug("Initial Size: " & $nSize)


    ; >>>>>>>> this gives me File Save Dialog <<<<
;~  _IENavigate($oIE, $sLocation)
    ;  >>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ; ***** tried this code to download with no success ********************
;~ $sLocation = "dalisuit@gmail.com:officemail@" & $sLocation
; ************************************************************************

    Debug("$sLocation  = " & $sLocation)

    ; >>>>>>>> this downloads HTML page of GMail  login screen  <<<<<<<<<<<<<<<
    Local $hDownload = InetGet($sLocation, $sFilename, 0, 1)

    Do
        Debug("Bytes so far: " & InetGetInfo($hDownload, 0))
        Sleep(1000)
    Until InetGetInfo($hDownload, 2)    ; Check if download complete.

    Local $aData = InetGetInfo($hDownload)  ; Get download information.
    InetClose($hDownload)   ; Close handle to release resources.

    Debug("Bytes read: " & $aData[0])
    Debug("Size: " & $aData[1])
    Debug("Complete?: " & $aData[2])
    Debug("Successful?: " & $aData[3])
    Debug("@error: " & $aData[4])
    Debug("@extended: " & $aData[5])
    ;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
EndFunc

Func _HTTPGetResponse($sUrl)
    Local $Error, $Socket, $Recv = ''
    Local $Host = StringRegExpReplace($sUrl, '.*://(.*?)/.*', '\1')
    Local $Page = StringRegExpReplace($sUrl, '.*://.*?(/.*)', '\1')
    Local $Send = 'HEAD ' & $Page & ' HTTP/1.1' & @CRLF & _
                'Host: ' & $Host & @CRLF & _
                'User-Agent: AutoIt/' & @AutoItVersion & '  (Windows; U; Windows NT 5.1; en-US; rv:1.8.1)' & @CRLF & _
                'Referer: ' & $Host & @CRLF & _
                'Connection: close' & @CRLF & @CRLF

    TCPStartup()
    If @error Then
        Return SetError(@error, 0, '')
    EndIf
    $IP = TCPNameToIP($Host)
    If @error Then
        Return SetError(@error, TCPShutdown(), '')
    EndIf
    $Socket = TCPConnect($IP, 80)
    If @error Then
        Return SetError(@error, TCPShutdown(), '')
    EndIf
    If Not TCPSend($Socket, $Send) Then
        Return SetError(@error, TCPCloseSocket($Socket) + TCPShutdown(), '')
    EndIf
    Do
        $Recv &= TCPRecv($Socket, 16)
    Until @error
    If Not $Recv Then
        $Error = @error
    Else
        $Error = 0
    EndIf
    TCPCloseSocket($Socket)
    TCPShutdown()
    Return SetError($Error, 2, $Recv)
EndFunc   ;==>_HTTPGetResponse

Func GmailLogin($user, $pass)

    Local $oIE = _IECreate("http://mail.google.com", 1, 1)

    ; get pointers to the login form and username and password fields
    Local $oform = _IEFormGetObjByName($oIE, "gaia_loginform")
    Local $ologin = _IEFormElementGetObjByName($oform, "Email")
    Local $opassword = _IEFormElementGetObjByName($oform, "Passwd")

    ; Set field values and submit the form
    _IEFormElementSetValue($ologin, $user)
    _IEFormElementSetValue($opassword, $pass)

    Debug("+> Submitting form")
    _IEFormSubmit($oform, 0)

    Debug("+> Waiting for Security Warning window")
    WinWait("Security Warning", "", 3)
    Debug("+> Done waiting for Security Warning window")

    If WinExists("Security Warning", "") Then
        Debug("+> Closing Security Warning window")
        ControlClick("Security Warning", "", "[CLASS:Button; INSTANCE:2]")
    EndIf

    ; check if login success
    Debug("+>Checking Login success...")
    ; how do I check for successful login
    Sleep(2000)
    Debug("Email login successful.")

    Return $oIE
EndFunc   ;==>GmailLogin

Func sStringBetween($s_String, $s_Start, $s_End, $v_Case = -1)
    Local $Between = _StringBetween($s_String, $s_Start, $s_End, $v_Case)
    If IsArray($Between) Then
        Return $Between[0]
    Else
        Return ""
    EndIf
EndFunc   ;==>sStringBetween

Func Debug($Text = "")
    ConsoleWrite($Text & @LF)
EndFunc   ;

I have zero knowledge of HTML or Java.

Edited by dmob

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

when i have a second, i'll post some sample code i've used for downloading from a link on a page (should be the same as an attachment in gmail, but i haven't looked really hard yet)

Edited by DarkLordZim

Share this post


Link to post
Share on other sites

i think you are approaching this task in the wrong way.

first, you are using inetget() to actually download the html as a file, instead of reading in the link of the attachment.

what you can do, is use _IELinkGetCollection to read in the links from the current page. and search for your attachment. then feed that attachment link to inetget()

for example, this is part of a script i wrote, because i was tired of going out to the bleepingcomputer website evertime i needed ComboFix.exe to clean anyone's infected computer.

#include <IE.au3>
$oIE = _IECreate ("http://www.bleepingcomputer.com/download/anti-virus/combofix", 0, 0)
$oLinks = _IELinkGetCollection ($oIE)
For $oLink In $oLinks
    If StringInStr($oLink.href, "download") And StringInStr($oLink.href, "/combofix.exe") Then ;identifies the desired link out of the array
        $url = $oLink.href
        ExitLoop
    EndIf
Next
_IEQuit($oIE)
$Filename = @DesktopDir & "\ComboFix.exe"
If $Filename Then ;check for combofix
    FileDelete($Filename)
EndIf
Local $download = inetget($Url,$Filename,1,1) ;Download newest combofix from bleeping computers
Do
    sleep(250)
Until InetGetInfo($download,2) ;check if download is complete
Local $aData = InetGetInfo($download)
InetClose($download) ;close to release resources

Share this post


Link to post
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
Sign in to follow this  
Followers 0