Sign in to follow this  
Followers 0
russell

Screen shot then email

10 posts in this topic

Ok so im trying to take a screen shot then email it to myself and i am stuck as to how to address the issue of the new file name

#include <misc.au3>

; Capture full screen
; Fist parameter - filename, last - jpeg quality.
    DllCall("C:\test\captdll.dll", "int", "CaptureScreen", "str", @MON & @MDAY & @YEAR & @HOUR & @MIN & @SEC & ".jpg", "int", 85)
 Run ("C:\test\emailer.exe")
 Exit

Emailer code

;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "smtp.gmail.com"              ; address for the smtp-server to use - REQUIRED
$FromName = "Screen shot"                      ; name from who the email was sent
$FromAddress = "myself@gmail.com" ; address from where the mail should come
$ToAddress = "myself@gmail.com"   ; destination address of the email - REQUIRED
$Subject = "this is from you"                   ; subject from the email - can be anything you want it to be
$Body = "here is your scrrenshot"                              ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                       ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""       ; address for cc - leave blank if not needed
$BccAddress = ""     ; address for bcc - leave blank if not needed
$Importance = "High"                  ; Send message priority: "High", "Normal", "Low"
$Username = "myaccount@gmail.com"                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = "mypass"                  ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465                            ; port used for sending the mail
$ssl = 1                                ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465                          ; GMAIL port used for sending the mail
;~ $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
    MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf
;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
;~          ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 then $IPPort = 25
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
EndFunc   ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc

I also have trouble getting the program to close after complete. It would seem that its very close but i cant find the error


muppet hands are so soft :)

Share this post


Link to post
Share on other sites



What is the error?

Share this post


Link to post
Share on other sites

I had to change the DLLCall to "int:cdecl"...with just "int" I could never email the pic. As for your email function, I could never get it to send anything. Here is the GMail UDF floating around here with necessary Functions included...this script worked for me.

#include <misc.au3>

; Capture full screen
; Fist parameter - filename, last - jpeg quality.
$Name = @DesktopDir & '\' & @MON & @MDAY & @YEAR & @HOUR & @MIN & @SEC & ".jpg"
DllCall("captdll.dll", "int:cdecl", "CaptureScreen", "str", @MON & @MDAY & @YEAR & @HOUR & @MIN & @SEC & ".jpg", "int", 20)
Do
    Sleep(10)
Until FileExists($Name)

;##################################
$SmtpServer = "smtp.gmail.com"              ; address for the smtp-server to use - REQUIRED
$FromName = "Screen shot"                      ; name from who the email was sent
$FromAddress = "myself@gmail.com" ; address from where the mail should come
$ToAddress = "myself@gmail.com"   ; destination address of the email - REQUIRED
$Subject = "this is from you"                   ; subject from the email - can be anything you want it to be
$Body = "here is your scrrenshot"                              ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = $Name                       ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""       ; address for cc - leave blank if not needed
$BccAddress = ""     ; address for bcc - leave blank if not needed
$Importance = "High"                  ; Send message priority: "High", "Normal", "Low"
$Username = "myaccount@gmail.com"                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = "mypass"                  ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465                            ; port used for sending the mail
$ssl = 1                                ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465                          ; GMAIL port used for sending the mail
;~ $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

;##################################

_INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $Ssl)

Func _INETSMTPMAILCOM($S_SMTPSERVER, $S_FROMNAME, $S_FROMADDRESS, $S_TOADDRESS, $S_SUBJECT = "", $aS_BODY = "", $S_ATTACHFILES = "", $S_CCADDRESS = "", $S_BCCADDRESS = "", $s_Importance = "Normal", $S_USERNAME = "", $S_PASSWORD = "", $IPPort = 25, $Ssl = 0)
    Global $oMYRET
    $oBJEMAIL = ObjCreate("CDO.Message")
    $oBJEMAIL.From = '"' & $S_FROMNAME & '" <' & $S_FROMADDRESS & ">"
    $oBJEMAIL.To= $S_TOADDRESS
    Local $I_ERROR = 0
    Local $I_ERROR_DESCIPTION = ""
    If $S_CCADDRESS <> "" Then $oBJEMAIL.Cc = $S_CCADDRESS
    If $S_BCCADDRESS <> "" Then $oBJEMAIL.Bcc = $S_BCCADDRESS
    $oBJEMAIL.Subject = $S_SUBJECT
    If StringInStr($aS_BODY, "<") And StringInStr($aS_BODY, ">") Then
        $oBJEMAIL.HTMLBody = $aS_BODY
    Else
        $oBJEMAIL.Textbody = $aS_BODY & @CRLF
    EndIf
    If $S_ATTACHFILES <> "" Then
        Local $S_FILES2ATTACH = StringSplit($S_ATTACHFILES, ";")
        For $x = 1 To $S_FILES2ATTACH[0]
            $File = _PathSplitByRegExp($S_FILES2ATTACH[$x])
            $S_FILES2ATTACH[$x] = $File[0]
            If FileExists($S_FILES2ATTACH[$x]) Then
                $oBJEMAIL.AddAttachment($S_FILES2ATTACH[$x])
            Else
                $I_ERROR_DESCIPTION = $I_ERROR_DESCIPTION & @LF & "File not found to attach: " & $S_FILES2ATTACH[$x]
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $S_SMTPSERVER
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    If $S_USERNAME <> "" Then
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $S_USERNAME
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $S_PASSWORD
    EndIf
    If $Ssl Then
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    $oBJEMAIL.Configuration.Fields.Update
    ; Set email Importance
    Switch $s_Importance
        Case "High"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $oBJEMAIL.Fields.Update
    $oBJEMAIL.Send
    If @error Then
        SetError(2)
        Return $oMYRET[1]
    EndIf
EndFunc   ;==>_INETSMTPMAILCOM

Func _PathSplitByRegExp($sPath)
    If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1)
    Local $RetArray[8], $pDelim = ""
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\"
    If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//"
    If $pDelim = "" Then $pDelim = "/"
    If Not StringInStr($sPath, $pDelim) Then Return $sPath
    If $pDelim = "\" Then $pDelim &= "\"
    $RetArray[0] = $sPath
    $RetArray[1] = StringReplace($sPath, StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*|\\w*\\)', ''), "")
    $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '')
    $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '')
    $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*\\|\\w*\\)', '')
    $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
    $RetArray[6] = StringRegExpReplace($RetArray[5], '\.[^.]*$', '')
    $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '')
    Return $RetArray
EndFunc   ;==>_PathSplitByRegExp

Share this post


Link to post
Share on other sites

Varian it looks like it will work with your corrections but the script runs and produces nothing. Any idea?


muppet hands are so soft :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok, yes it did work for me as well, i had to move captdll.dll to the desktop to get it to run. And the script did email it to me. You nailed it on and head and are a great help! Thank you

Edited by russell

muppet hands are so soft :)

Share this post


Link to post
Share on other sites

Ok, yes it did work for me as well, i had to move captdll.dll to the desktop to get it to run. And the script did email it to me. You nailed it on and head and are a great help! Thank you

No problem!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hi! I tested this script and it works fine. Thanks!

But i want to add other files than *.jpg as attachaments (*.txt)

Can you help me?

I had to change the DLLCall to "int:cdecl"...with just "int" I could never email the pic. As for your email function, I could never get it to send anything. Here is the GMail UDF floating around here with necessary Functions included...this script worked for me.

#include <misc.au3>

; Capture full screen
; Fist parameter - filename, last - jpeg quality.
$Name = @DesktopDir & '\' & @MON & @MDAY & @YEAR & @HOUR & @MIN & @SEC & ".jpg"
DllCall("captdll.dll", "int:cdecl", "CaptureScreen", "str", @MON & @MDAY & @YEAR & @HOUR & @MIN & @SEC & ".jpg", "int", 20)
Do
    Sleep(10)
Until FileExists($Name)

;##################################
$SmtpServer = "smtp.gmail.com"              ; address for the smtp-server to use - REQUIRED
$FromName = "Screen shot"                      ; name from who the email was sent
$FromAddress = "myself@gmail.com" ; address from where the mail should come
$ToAddress = "myself@gmail.com"   ; destination address of the email - REQUIRED
$Subject = "this is from you"                   ; subject from the email - can be anything you want it to be
$Body = "here is your scrrenshot"                              ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = $Name                       ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""       ; address for cc - leave blank if not needed
$BccAddress = ""     ; address for bcc - leave blank if not needed
$Importance = "High"                  ; Send message priority: "High", "Normal", "Low"
$Username = "myaccount@gmail.com"                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = "mypass"                  ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465                            ; port used for sending the mail
$ssl = 1                                ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465                          ; GMAIL port used for sending the mail
;~ $ssl=1                               ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

;##################################

_INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $Ssl)

Func _INETSMTPMAILCOM($S_SMTPSERVER, $S_FROMNAME, $S_FROMADDRESS, $S_TOADDRESS, $S_SUBJECT = "", $aS_BODY = "", $S_ATTACHFILES = "", $S_CCADDRESS = "", $S_BCCADDRESS = "", $s_Importance = "Normal", $S_USERNAME = "", $S_PASSWORD = "", $IPPort = 25, $Ssl = 0)
    Global $oMYRET
    $oBJEMAIL = ObjCreate("CDO.Message")
    $oBJEMAIL.From = '"' & $S_FROMNAME & '" <' & $S_FROMADDRESS & ">"
    $oBJEMAIL.To= $S_TOADDRESS
    Local $I_ERROR = 0
    Local $I_ERROR_DESCIPTION = ""
    If $S_CCADDRESS <> "" Then $oBJEMAIL.Cc = $S_CCADDRESS
    If $S_BCCADDRESS <> "" Then $oBJEMAIL.Bcc = $S_BCCADDRESS
    $oBJEMAIL.Subject = $S_SUBJECT
    If StringInStr($aS_BODY, "<") And StringInStr($aS_BODY, ">") Then
        $oBJEMAIL.HTMLBody = $aS_BODY
    Else
        $oBJEMAIL.Textbody = $aS_BODY & @CRLF
    EndIf
    If $S_ATTACHFILES <> "" Then
        Local $S_FILES2ATTACH = StringSplit($S_ATTACHFILES, ";")
        For $x = 1 To $S_FILES2ATTACH[0]
            $File = _PathSplitByRegExp($S_FILES2ATTACH[$x])
            $S_FILES2ATTACH[$x] = $File[0]
            If FileExists($S_FILES2ATTACH[$x]) Then
                $oBJEMAIL.AddAttachment($S_FILES2ATTACH[$x])
            Else
                $I_ERROR_DESCIPTION = $I_ERROR_DESCIPTION & @LF & "File not found to attach: " & $S_FILES2ATTACH[$x]
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $S_SMTPSERVER
    $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    If $S_USERNAME <> "" Then
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $S_USERNAME
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $S_PASSWORD
    EndIf
    If $Ssl Then
        $oBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    $oBJEMAIL.Configuration.Fields.Update
    ; Set email Importance
    Switch $s_Importance
        Case "High"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $oBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $oBJEMAIL.Fields.Update
    $oBJEMAIL.Send
    If @error Then
        SetError(2)
        Return $oMYRET[1]
    EndIf
EndFunc   ;==>_INETSMTPMAILCOM

Func _PathSplitByRegExp($sPath)
    If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1)
    Local $RetArray[8], $pDelim = ""
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\"
    If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//"
    If $pDelim = "" Then $pDelim = "/"
    If Not StringInStr($sPath, $pDelim) Then Return $sPath
    If $pDelim = "\" Then $pDelim &= "\"
    $RetArray[0] = $sPath
    $RetArray[1] = StringReplace($sPath, StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*|\\w*\\)', ''), "")
    $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '')
    $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '')
    $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*\\|\\w*\\)', '')
    $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
    $RetArray[6] = StringRegExpReplace($RetArray[5], '\.[^.]*$', '')
    $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '')
    Return $RetArray
EndFunc   ;==>_PathSplitByRegExp

Edited by robeert

Share this post


Link to post
Share on other sites

What problems are you running into? I would think that adding the path of the txt to $AttachFiles will work.

P.S. Just open a new topic for new questions and optionally link to another topic where you got the source. Now you'll have a lot of people reading the post of the OP thinking before realising that he/she isn't the one requesting help.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I was just checking this script out and decided to run it 'as is' to get a feel for and I ran into an error.

C:\Users\XXXXXXX\Documents\Autoit Files\screeny.au3 (52) : ==> Subscript used with non-Array variable.:
$S_FILES2ATTACH[$x] = $File[0]
$S_FILES2ATTACH[$x] = $File^ ERROR

Must be missing something stupid and I'll check again to make sure. (Already checked once)

[Edit]

Looks like it's looking for an array in $File which was never actually defined as an array...I'm pretty rusty so if I am missing something easy please let me know.

Edited by Deadphyre

Share this post


Link to post
Share on other sites

My mistake, I missed some of the code somehow when I highlighted and copied it. Now it starts to run but hangs and I'm not sure at which line it hangs at.

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