#include und @error

  • Hallo,

    Ich habe 3 Fragen und hoffe, dass mir geholfen werden kann.

    1. Ich habe mir einen Mailer von folgender Seite heruntergeladen. http://www.autoitscript.com/forum/index.php?showtopic=23860

    Das Skritp habe ich in ein seperates .au3 File getan.

    Wenn ich nun in meinem Skript, dass obige .au3 File durch #include aufrufe, dann wir anscheinend direkt das Skript ausgeführt, also eine Mail gesendet. Ist das normal, dass durch #include das Skript ausgeführt wird?

    Wie kann ich aber bevor die Mail gesendet wird, noch 2 Variablen im Skript ändern ($Subject und $Body)? Also das Skript soll eben an verschiedenen Stellen ausgeführt werden, das kann ich ja mit #include machen, aber $Subject und $Body, soll jedes Mal anders sein.

    2. Wenn ich das obige Skript in ein anderes Skript mit #include einbinde, dann sehe ich in der Autovervollständigung die Funktion dieses Skripts nicht. Warum? Also beim obigen Mail Skript hat es ja eine Funktion _INetSmtpMailCom. Wenn ich dann in einem anderen Skript das Mail Skript mit #include einbinde, dann kommt bei der Autovervollständigung die Funktion _INetSmtpMailCom nicht.

    3. Noch eine Frage zu @error. Wenn eine Funktion einen Fehler erzeugt und @error auf 1 setzt, ist dann @error für das gesamte restliche Skript auf 1?

    2 Mal editiert, zuletzt von Helveticus (21. Februar 2010 um 23:43)

  • 1. An der Stelle, wo #include steht, wird das Skript ausgeführt. Eigentlich werden in UDFs nur Funktionen definiert, aber keine aufgerufen.

    2. Die Autovervollständigung geht nur für die Standart befehle und Standart-UDFs.
    Soweit ich weiß kann man sich eine Vollversion des Editors runterladen und da auch neue FUnktionen bei der Autovervollständigung eintragen.

    3. Nach jeder Aktion wird @error neu gesetzt.

  • Ich danke dir.

    zu 1. Um ein Mail zu schicken, muss ich also an der Stelle, wo ich das Mail schicken will, #include hinzufügen?

    Ich werde das Skript also per #include aufrufen, möchte aber zwei Variablen in diesem Skript gleichzeitig ändern. Wie geht das? Also das Skript soll in verschiedenen Situationen per #include aufgerufen werden und bei jedem Aufruf möchte ich diese zwei Variablen eben anders definieren.

    zu 2. Wie kann ich eine UDF aufrufen? Wenn ich das Skript mit #include einbide, wird ja einfach das Skript ausgeführt. Ich möchte aber die Funktion aufrufen und eben auch Variablen übergeben etc.

  • zu 1. Um ein Mail zu schicken, muss ich also an der Stelle, wo ich das Mail schicken will, #include hinzufügen?

    nein.

    am anfang des scripts machst du einfach include "blablabla"
    und da wo du die email senden willst rufst du dann die funktion auf _InetSmtpIrgendwas(Parameter1 , ...)

    das includen machst du quasi nur damit du die funktion in deinem script verfügbar hast. wann du sie dann aufrufst bleibt dir überlassen ;)

  • Wenn ich das Skript aber am Anfang per include einbinde, dann wird bei mir direkt ein Mail gesendet.

    Bei meinem oben angegebenen Skript werden die Variablen bereits am Anfang definiert.

  • achso :D
    du solltest nicht das ganze beispielscript einbinden sondern nur die funktion

    Spoiler anzeigen
    [autoit]

    ; 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(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
    If FileExists($S_Files2Attach[$x]) Then
    $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

    [/autoit]

    also entweder kopierst du das in ne au3 und includest diese oder du schreibst die funktion einfach unten in dein hauptscript rein

  • Ah ok vielen Dank. :rock:
    1. Ich habe jetzt nur die Funktion _INetSmtpMailCom in ein seperates .au3 File getan und kann dann auch diese Funktion in einem anderen Skript aufrufen, wenn ich das .au3 File include. Allerdings funktioniert es nicht, es kommen Fehlermeldungen. Wenn ich das ganze Skript ein ein .au3 File kopiere dann funktioniert es. Könntest du das vielleicht einmal bei dir ausprobieren? Ich sehe den Fehler nicht.

    2. Was hat die Funktion eigentlich für einen Rückgabewert (Bei Erfolg und bei Fehler)? Und wie sieht es mit dem error handling der Funktion aus (@error)? Sehe das irgendwie nicht.

    3. Und noch eine Nebenfrage. Wie kann ich ein Programm minimieren?

    2 Mal editiert, zuletzt von Helveticus (21. Februar 2010 um 13:20)

  • Das hier als include: (alles ungetestet, nur im Texteditor schnell zusammenkopiert, weil ich bei nem kollegen sitze)

    Spoiler anzeigen
    [autoit]

    #include-once
    #Include<file.au3>
    Global $oMyRet[2]
    Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
    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(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
    If FileExists($S_Files2Attach[$x]) Then
    $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

    [/autoit]

    So rufst du die Funktion auf:

    Spoiler anzeigen
    [autoit]

    $SmtpServer = "MailServer" ; address for the smtp-server to use - REQUIRED
    $FromName = "Name" ; name from who the email was sent
    $FromAddress = "your@Email.Address.com" ; address from where the mail should come
    $ToAddress = "your@Email.Address.com" ; destination address of the email - REQUIRED
    $Subject = "Userinfo" ; subject from the email - can be anything you want it to be
    $Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail
    $AttachFiles = "" ; the file you want to attach- leave blank if not needed
    $CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed
    $BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed
    $Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
    $Username = "******" ; username for the account used from where the mail gets sent - REQUIRED
    $Password = "********" ; password for the account used from where the mail gets sent - REQUIRED
    $IPPort = 25 ; port used for sending the mail
    $ssl = 0 ; 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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit]


    Nein, du brauchst KEINE Variablen zu benutzen, sondern kannst die Daten auch direkt so eintragen...

    Zitat

    2. Was hat die Funktion eigentlich für einen Rückgabewert (Bei Erfolg und bei Fehler)? Und wie sieht es mit dem error handling der Funktion aus (@error)? Sehe das irgendwie nicht.


    @error 1: Dateianhang nicht gefunden.
    @error 2: Fehler beim Senden
    Ansonsten ist noch ein Error Handler für die Com-Events eingebaut.


    @andere Frage:
    WinSetState("Fenster","",@SW_MINIMIZE) ?

  • Hallo Helveticus,

    wie Raupi schon sagte, ist in dem von dir genannten Post: http://www.autoitscript.com/forum/index.php?showtopic=23860 die aufzurufende Funktion und das Beispiel wie man diese aufruft. Du musst das ganze also entweder splitten, oder so anpassen dass es nur bei Bedarf gesendet wird.

    Skript für variablen Teil:

    Spoiler anzeigen
    [autoit]

    #include<file.au3>
    #include<SMTPUDF.au3>
    ;##################################
    ; Variables
    ;##################################
    ; *** Start added by AutoIt3Wrapper ***
    #include <GUIConstantsEx.au3>
    ; *** End added by AutoIt3Wrapper ***
    $SmtpServer = "MailServer" ; address for the smtp-server to use - REQUIRED
    $FromName = "Name" ; name from who the email was sent
    $FromAddress = "your@Email.Address.com" ; address from where the mail should come
    $ToAddress = "your@Email.Address.com" ; destination address of the email - REQUIRED
    $Subject = "Userinfo" ; subject from the email - can be anything you want it to be
    $Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail
    $AttachFiles = "" ; the file you want to attach- leave blank if not needed
    $CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed
    $BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed
    $Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
    $Username = "******" ; username for the account used from where the mail gets sent - REQUIRED
    $Password = "********" ; password for the account used from where the mail gets sent - REQUIRED
    $IPPort = 25 ; port used for sending the mail
    $ssl = 0 ; 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

    [/autoit] [autoit][/autoit] [autoit]

    ;##################################
    ; Script
    ;##################################
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_UseUpx=n
    #AutoIt3Wrapper_Add_Constants=n
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 331, 164, 193, 125)
    GUICtrlCreateLabel("Empfänger:",5,5,100,20)
    $hinpToAdress = GUICtrlCreateInput("", 110, 5,150,25)
    ;hier auch noch die anderen Felder definieren für $subject etc.
    $hbtnSend = GUICtrlCreateButton("&Senden", 110, 64, 75, 25)
    $hbtnExit = GUICtrlCreateButton("Abbr&echen", 110, 100, 75, 25)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE, $hbtnExit
    Exit
    Case $hbtnSend ;jetzt wird gesendet
    $ToAddress = GUICtrlRead($hinpToAdress)
    $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
    EndSwitch
    WEnd

    [/autoit]


    UDF:

    Spoiler anzeigen
    [autoit]

    ; The UDF
    Global $oMyRet[2]
    Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

    [/autoit] [autoit][/autoit] [autoit]

    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(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
    If FileExists($S_Files2Attach[$x]) Then
    $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

    [/autoit]

    Edit: man sollte den Fuchs öfters aktualiesieren
    mfg (Auto)Bert