tsohnges
Goto Top

Neue Datei auf FTP-Server erkennen und per Email (als Anhang) versenden an feste Emailadresse (Blat, AutoIT, WatchFTP ?)

Hallo,

ich bräuchte ein bißchen Hilfe bei der Umsetzung folgender Aufgabe und würde mich über Denkanstöße oder Hilfe sehr freuen:

Auf einem Windows-PC soll - alle 15min wiederkehrend im Hintergrund - folgendes Skript ablaufen:
1) Ein bestimmtes (externes) FTP-Verzeichnis soll regelmäßig z.B. alle 15min per FTP auf neue Dateien überprüft werden. Nur neue Dateien, keine neuen Ordner... Dateityp egal...
2) Falls neue Dateien (Typ, Größe egal) vorhanden sind, sollen diese per FTP runtergeladen werden in einen lokalen Ordner auf dem Windows-PC und dann
3) als Emailanhang verschickt werden, jede Datei einzeln, mit einem mehrzeiligen Standardtext ("Sehr geeehrte... anbei erhalten Sie... mit freundlichen Grüßen"), Betreff jeweils mit dem variablen Dateinamen "Anbei DATEINAME zur Information". To: und CC: wären immer gleich. Versand erfolgt über SMTP-Server vom Provider (auth). Keinr Verbindung zu Outlook o.ä. nötig und auch nicht gewünscht.
4) Anschließend soll die Datei in einen Ordner verschoben werden "verschickte Dateien" o.ä.

Ich dachte an BLAT oder ggf. auch AutoIT.

Hab mich auch schon eingelesen in die beiden Threads hier:
PDF Dateien aus Verzeichnis automatisch per Email versenden und
Hilfe!! Script erstellen, das 2 Dateien prüfen soll und dann als email zb. mit blat versenden.
habe mich bisher für die BLAT-Variante entschieden, ich denke das reicht und sollte funktionieren, aber leider bin ich bei den Skriptkenntnissen nicht so auf der Höhe.

Habe mir auch WatchFTP http://www.watchftp.de/ angesehen, aber ich möchte nicht nur eine Email bekommen, DASS sich etwas auf dem FTP-Server geändert hat, sondern genau diese Datei gleich an die Email anhängen. Soweit ich das erkennen konnte, wird dies von WatchFTP nicht unterstützt. Schlanker und mir lieber wäre eigentlich auch BLAT, denke ich.

Vielen Dank vorab
Andere Vorschläge (freeware, shareware oder dergl. die das kann auch gerne willkommen)

Tilo

Content-Key: 263860

Url: https://administrator.de/contentid/263860

Printed on: April 24, 2024 at 15:04 o'clock

Mitglied: 114757
Solution 114757 Feb 21, 2015 updated at 16:50:08 (UTC)
Goto Top
Moin Moin,
AutoIT All-In-One ohne externe Tools, das ganze alle 15 Minuten in den Taskplaner, feddich.
Viel Erfolg...
#NoTrayicon
; Includes -----
#include <FTPEx.au3>
#include <Array.au3>
#Include <file.au3>
#Include <WinAPI.au3>
; ---- FTP Settings ----
Const $SERVER = "ftp.domain.de"  
Const $USERNAME = "USERNAME"  
Const $PASSWORD = "PASSWORD"  
Const $FTPDIR = "/data/ordner"  
; ---------
; ---- MAIL-SETTINGS -----
Const $FROMEMAIL = "from@domain.de"  
Const $TOEMAIL = "to@domain.de"  
Const $CCEMAIL = "cc@domain.de"  
Const $FROMNAME = "ABSENDERNAME"  
Const $MAILSERVER = "smtp.domain.de"  
Const $SMTPUSER = "user@domain.de"  
Const $SMTPPASS = "PASSWORD"  
Const $SMTPPORT = "25"  
Const $SMTPUSESSL = 1
; -----------------
Global $downloadDir = @TempDir
Global $archiveDir = @ScriptDir & "\archive"  
Global $syncFile = @ScriptDir & "\files.txt"  
Global $newFiles[1]
;----------------
; Archivverzeichnis erstellen wenn es nicht existiert
if not FileExists($archiveDir) then DirCreate($archiveDir)
$ftp = _FTP_Open("myagent")  
; Verbinde mit FTP-Server
$conn = _FTP_Connect($ftp,$SERVER,$USERNAME,$PASSWORD)
if $conn = 0 then
	msgbox(48,"Verbindungs-Fehler","Die Verbindung zum FTP-Server konnte nicht hergestellt werden",2)  
	_FTP_Close($ftp)
	exit 1
EndIf
; navigiere in FTP Ordner
if _FTP_DirSetCurrent($conn,$FTPDIR) = 0 then
	msgbox(48,"Verzeichnisfehler","In das FTP-Verzeichnis konnte nicht gewechselt werden!",2)  
	_FTP_Close($ftp)
	exit 1
endif
; hole Liste aller aktuellen Dateien im Verzeichnis
$arrCurrentFiles = _Ftp_ListToArray($conn,2)
; Vergleiche Dateiliste wenn vorhanden
if FileExists($syncFile) then
	$fOpen = FileOpen($syncFile)
	$arrLastFiles = StringSplit(FileReadLine($fOpen,1),"|")  
	FileClose($fOpen)
	for $i = 1 to $arrCurrentFiles
		if _ArraySearch($arrLastFiles,BinaryToString($arrCurrentFiles[$i],4)) = -1 then
			_ArrayAdd($newFiles,$arrCurrentFiles[$i])
		Endif
	Next
Else
	; Speichere initial alle aktuellen Dateinamen im FTP-Verzeichnis
	$strFiles = BinaryToString(_ArrayToString($arrCurrentFiles,"|",1),4)  
	$fSave = FileOpen($syncFile,138)
	FileWrite($fSave,$strFiles)
	FileClose($fSave)
	msgbox(64,"Erstsynchronisierung erfolgt", "Die Dateiliste wurde erstmalig erstellt, nachfolgend hinzukommende Dateien werden bei erneutem Aufruf versendet",4)  
Else
EndIf
; Alle neuen Dateien lokal speichern, per Mail verschicken und Archivieren
if UBound($newFiles) > 1 then
	local $sentFiles
	for $i = 1 to UBound($newFiles)-1
		;Download File
		$UTF8Name = BinaryToString($newFiles[$i],4)
		if _FTP_FileGet($conn,$newFiles[$i],$downloadDir & "\" & $UTF8Name ) = 1 then  
			;Send Mail with Attachment
			local $mBody = "Sehr geehrte Damen und Herren," & @CRLF & "anbei erhalten sie eine neue Datei." & @CRLF & "Mit freundlichen Grüßen" & @CRLF & "Ihre Firma XYZ"  
			local $mSubject = "Anbei " & $UTF8Name & " zur Information."  
			_INetSmtpMailCom($MAILSERVER,$FROMNAME,$FROMEMAIL,$TOEMAIL,$mSubject,$mBody,$downloadDir & "\" & $UTF8Name,$CCEMAIL,"","Normal",$SMTPUSER,$SMTPPASS,$SMTPPORT,$SMTPUSESSL)  
			; Wenn Datei erfolgreich veschickt wurde => Datei archivieren
			if not @error then
				FileMove($downloadDir & "\" & $UTF8Name,$archiveDir,1)  
				$sentfiles = $sentFiles & "|" & $UTF8Name  
			endif
		Endif
	Next
	$fSave = FileOpen($syncFile,129)
	FileWrite($fSave,$sentFiles)
	FileClose($fSave)
	msgbox(64,"Erfolg", "Folgende Dateien wurden erfolgreich versendet: " & @CRLF & @CRLF & $sentFiles,2)  
Else
	msgbox(64,"Keine neuen Dateien","Es wurden keine neuen Dateien gefunden!",1)  
Endif

;Trenne FTP Verbindung
_FTP_Close($ftp)
Exit 0


; Functions ------------------------------

; Variables for Function _INetSmtpMailCom
;##################################
;~ $SmtpServer = ""                            ; address for the smtp-server to use - REQUIRED  
;~ $FromName = "User"                          ; name from who the email was sent  
;~ $FromAddress = ""                           ; address from where the mail should come  
;~ $ToAddress = ""                             ; destination address of the email - REQUIRED  
;~ $Subject = "testsubject"                       ; subject from the email - can be anything you want it to be  
;~ $Body = "This Is The 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 = ""                             ; address for cc - leave blank if not needed  
;~ $BccAddress = ""                            ; 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                                   ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS


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
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            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)
    EndIf
    $objEmail=""  
EndFunc   ;==>_INetSmtpMailCom
Gruß jodel32