program pdf2smtp;
{$APPTYPE CONSOLE}
uses
WinTypes, WinProcs, SysUtils, IniFiles;
var
{Variablendeklaration, z.T. mit Defaultbelegung, falls keine
anderen Werte aus dem INI-File gelesen werden koennen}
PSFILE, BODYFILE, PDFFILE, TEMP :
String;
REDMON_USER, REDMON_DOCNAME, REDMON_JOB, REDMON_MACHINE, REDMON_PRINTER :
String;
REDMON_PORT, REDMON_FILENAME, REDMON_SESSIONID :
String;
result : boolean;
pdf2smtpini : String='
pdf2smtp.ini';
inifilepath : String='
c:\programme\gs\redmon';
pdf2smtplog : String='
c:\programme\gs\redmon\pdf2smtp.log';
pdf2smtpgs : String='
c:\programme\gs\gs8.14\bin\gswin32c.exe';
pdf2smtpblat : String='
c:\programme\gs\redmon\blat.exe';
maildomain : String='
firma.de';
mailserver : String='
mail.firma.de';
sender : String='
pdf2smtp';
pdfprotect : String='
64';
pdfpassword : String='
';
outputsettings : String='
/default';
compatibility : String='
1.4';
F, LOG, BODY, PDF : TextFile;
FileMode : Byte = 2;
{ die Funktion ExecAndWait entliehen aus: [url]http://www.delphi-fundgrube.de/faq08.htm[/url]
16bit Teil entfernt; WindowState ist SW_normal, SW_minimized oder SW_maximized}
{ WindowState is one of the SW_xxx constants.
Look up ShowWindow in the API help for a list.}
function ExecAndWait(
const Filename, Params:
string; WindowState: word): boolean;
var
SUInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine:
string;
begin
{ Enclose filename in quotes to take care of
long filenames with spaces. }
CmdLine := '
"' + Filename + '
" ' + Params;
FillChar(SUInfo, SizeOf(SUInfo), #0);
with SUInfo
do begin
cb := SizeOf(SUInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WindowState;
end;
Result := CreateProcess(
NIL, PChar(CmdLine),
NIL,
NIL, FALSE,
CREATE_NEW_CONSOLE
or
NORMAL_PRIORITY_CLASS,
NIL,
PChar(ExtractFilePath(Filename)),
SUInfo, ProcInfo);
{ Wait for it to finish. }
if Result
then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;
procedure ExecuteFile(
const AFilename:
String;
AParameter, ACurrentDir:
String; AWait: Boolean);
var
si : TStartupInfo;
pi : TProcessInformation;
begin
if Length(ACurrentDir) = 0
then begin
ACurrentDir := ExtractFilePath(AFilename);
if ACurrentDir[Length(ACurrentDir)] = '
'
then
Delete(ACurrentDir, Length(ACurrentDir), 1);
end;
FillChar(si, SizeOf(si), 0);
with si
do begin
cb := SizeOf(si);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_NORMAL;
end;
FillChar(pi, SizeOf(pi), 0);
if Length(AParameter) = 0
then
AParameter := Format('
"%s"', [AFilename])
else
AParameter := Format('
"%s" "%s"', [AFilename, AParameter]);
if CreateProcess(
Nil, PChar(AParameter),
Nil,
Nil, False,
CREATE_DEFAULT_ERROR_MODE
or CREATE_NEW_CONSOLE
or
NORMAL_PRIORITY_CLASS,
Nil, PChar(ACurrentDir), si, pi)
then
begin
try
if AWait
then WaitForSingleObject(pi.hProcess, INFINITE);
finally
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
end;
end;
{Auslesen aller Werte aus dem Ini-File in Variablen}
procedure ReadIniFile;
var
IniFile : TIniFile;
begin
inifilepath := ExtractFilePath(ParamStr(0));
IniFile := TIniFile.Create(inifilepath+pdf2smtpini);
with IniFile
do
begin
pdf2smtplog := IniFile.ReadString('
Path','
pdf2smtplog',pdf2smtplog);
pdf2smtpgs := IniFile.ReadString('
Path','
pdf2smtpgs',pdf2smtpgs);
pdf2smtpblat := IniFile.ReadString('
Path','
pdf2smtpblat',pdf2smtpblat);
maildomain := IniFile.ReadString('
Mail','
maildomain',maildomain);
mailserver := IniFile.ReadString('
Mail','
mailserver',mailserver);
sender := IniFile.ReadString('
Mail','
sender',sender);
pdfprotect := IniFile.ReadString('
Pdf','
pdfprotect',pdfprotect);
pdfpassword := IniFile.ReadString('
Pdf','
pdfpassword',pdfpassword);
outputsettings := IniFile.ReadString('
Pdf','
outputsettings',outputsettings);
compatibility := IniFile.ReadString('
Pdf','
compatibility',compatibility);
end;
IniFile.Free;
end;
begin
{ TODO -oUser -cConsole Main : Insert code here }
{Erzeuge LOGFILE; anhaengen falls existiert, sonst neu anlegen}
AssignFile(LOG, pdf2smtplog);
try
Append(LOG);
except
Rewrite(LOG);
end;
Writeln(LOG, '
---------------------------------------------------');
Writeln(LOG, '
Logfile erstellt von PDF2SMTP Version 1.4 am '+DateToStr(Date));
Writeln(LOG, '
---------------------------------------------------');
CloseFile(LOG);
{Werte aus dem inifile lesen}
readinifile;
{Umgebungsvariablen lesen}
TEMP := GetEnvironmentVariable('
TEMP');
REDMON_USER := GetEnvironmentVariable('
REDMON_USER');
REDMON_DOCNAME := GetEnvironmentVariable('
REDMON_DOCNAME');
REDMON_JOB := GetEnvironmentVariable('
REDMON_JOB');
REDMON_MACHINE := GetEnvironmentVariable('
REDMON_MACHINE');
REDMON_PRINTER := GetEnvironmentVariable('
REDMON_PRINTER');
REDMON_PORT := GetEnvironmentVariable('
REDMON_PORT');
REDMON_FILENAME :=GetEnvironmentVariable('
REDMON_FILENAME');
REDMON_SESSIONID :=GetEnvironmentVariable('
REDMON_SESSIONID');
{Logfile schreiben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Umgebungsvariablen --------------------------------');
writeln(LOG, DateToStr(Date)+ '
REDMON_PORT '+REDMON_PORT );
writeln(LOG, DateToStr(Date)+ '
REDMON_JOB '+REDMON_JOB );
writeln(LOG, DateToStr(Date)+ '
REDMON_PRINTER '+REDMON_PRINTER );
writeln(LOG, DateToStr(Date)+ '
REDMON_MACHINE '+REDMON_MACHINE );
writeln(LOG, DateToStr(Date)+ '
REDMON_USER '+REDMON_USER );
writeln(LOG, DateToStr(Date)+ '
REDMON_DOCNAME '+REDMON_DOCNAME );
writeln(LOG, DateToStr(Date)+ '
REDMON_FILENAME '+REDMON_FILENAME );
writeln(LOG, DateToStr(Date)+ '
REDMON_SESSIONID '+REDMON_SESSIONID );
writeln(LOG, DateToStr(Date)+ '
Inputfile '+ParamStr(1) );
writeln(LOG, DateToStr(Date)+ '
TEMP-Pfad '+TEMP );
writeln(LOG, DateToStr(Date)+ '
GS-Pfad '+pdf2smtpgs );
writeln(LOG, DateToStr(Date)+ '
Blat-Pfad '+pdf2smtpblat );
writeln(LOG, DateToStr(Date)+ '
IniFile-Pfad '+inifilepath+pdf2smtpini );
CloseFile(LOG);
{Behandlung der Sonderzeichen}
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
\','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
/','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
:','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
?','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
<','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
>','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
|','
_',[rfReplaceAll]);
REDMON_DOCNAME := stringreplace(REDMON_DOCNAME,'
"','
_',[rfReplaceAll]);
{Dateinamen erzeugen}
BODYFILE := TEMP +'
\'+ REDMON_USER +'
-'+ REDMON_DOCNAME +'
-'+ REDMON_JOB +'
.txt';
PDFFILE := TEMP +'
\'+ REDMON_USER +'
-'+ REDMON_DOCNAME +'
-'+ REDMON_JOB +'
.pdf';
PSFILE := TEMP +'
\job-'+REDMON_JOB+'
.ps';
{Hier holen wir mal die Druckausgabe ab}
ExecuteFile('
c:\programme\gs\redmon\redfile.exe ','
"'+PSFILE+'
"','
c:\programme\gs\redmon\',False);
{Logfile schreiben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
temporäre Dateipfade --------------------------------');
writeln(LOG, DateToStr(Date)+ '
BODYFILE = '+BODYFILE);
writeln(LOG, DateToStr(Date)+ '
PDFFILE = '+PDFFILE);
writeln(LOG, DateToStr(Date)+ '
PSFILE = '+PSFILE);
writeln(LOG, DateToStr(Date)+ '
REDMON_DOCNAME neu = '+REDMON_DOCNAME);
Writeln(LOG, '
-----------------------------------------------------------------------');
CloseFile(LOG);
{Ghostscript Konvertierung}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
GS Konvertierung startet Input '+PSFile);
//writeln(LOG, DateToStr(Date)+ ' GS Konvertierung startet Input '+ParamStr(1));
writeln(LOG, DateToStr(Date)+ '
GS Konvertierung startet Output '+PDFFILE);
CloseFile(LOG);
//result := ExecAndWait(pdf2smtpgs, '-dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS='+outputsettings+' -dCompatibilityLevel='+compatibility+' -dPermissions='+pdfprotect+' -sOwnerPassword='+pdfpassword+' -sOutputFile="'+PDFFILE+'" '+ParamStr(1), SW_normal);
result := ExecAndWait(pdf2smtpgs, '
-dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS='+outputsettings+'
-dCompatibilityLevel='+compatibility+'
-dPermissions='+pdfprotect+'
-sOwnerPassword='+pdfpassword+'
-sOutputFile="'+PDFFILE+'
" '+PSFILE, SW_normal);
{Logfile schreiben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
GS Konvertierung beendet');
CloseFile(LOG);
{wenn die Umwandlung fehlschlaegt, dann das Postscriptfile sichern,
und bei Erfolg/Misserfolg entsprechende Meldungen ins Log schreiben
--- diese Funktion bisher nicht getestet ---}
if result=false
then
begin
result := ExecAndWait('
copy', '
"'+ParamStr(1)+ '
error-'+Paramstr(1)+'
.ps', SW_normal);
if result = true
then
begin
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Fehler bei der Konvertierung aufgetreten !!');
writeln(LOG, DateToStr(Date)+ '
PostscriptInput gesichert als'+ParamStr(1));
CloseFile(LOG);
end else
begin
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Fehler bei der Konvertierung aufgetreten !!');
writeln(LOG, DateToStr(Date)+ '
PostscriptInput wurde nicht gesichert !!!');
CloseFile(LOG);
end;
{Den Mailbody erzeugen, wenn Fehler bei der Konvertierung auftrat...}
AssignFile(F, BODYFILE);
Rewrite(F);
Writeln(F, '
!!! FEHLER bei der Konvertierung !!!');
Writeln(F, '
---------------------------------------------------');
Writeln(F, '
Ausdruck erzeugt von: '+REDMON_USER);
Writeln(F, '
Computer : '+REDMON_MACHINE);
Writeln(F, '
Drucker : '+REDMON_PRINTER);
Writeln(F, '
Dokumentname : '+REDMON_DOCNAME);
Writeln(F, '
Jobnummer : '+REDMON_JOB);
Writeln(F, '
---------------------------------------------------');
Writeln(F, '
Erstellt von PDF2SMTP Version 1.4 am '+DateToStr(Date));
CloseFile(F);
{...und dann versenden}
result := ExecAndWait(pdf2smtpblat, '
"'+BODYFILE+'
"'+'
-server "'+mailserver+'
" -f "'+sender+'
@'+maildomain+'
" -t "'+REDMON_USER+'
@'+maildomain+'
" -s "PDF2SMTP Fehler mit '+REDMON_DOCNAME+'
" -log '+pdf2smtplog, SW_normal);
{Logfile schreiben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Exitcode von blat: '+BoolToStr(result, FALSE));
writeln(LOG, DateToStr(Date)+ '
Beendet --------------------------------------');
CloseFile(LOG);
{Programm beenden mit Errorcode 1}
Halt(1);
end;
{Logfile schreiben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Sende Anlage per Mail');
writeln(LOG, DateToStr(Date)+ '
Empfaenger: '+REDMON_USER+'
@'+maildomain);
writeln(LOG, DateToStr(Date)+ '
Erzeuge Mailbody');
CloseFile(LOG);
{Den Mailbody erzeugen, wenn Konvertierung erfolgreich...}
AssignFile(F, BODYFILE);
Rewrite(F);
Writeln(F, '
Ihr Konvertierungsauftrag wurde ausgefuehrt');
Writeln(F, '
Sie finden die PDF-Datei als Anlage an diese Mail');
Writeln(F, '
---------------------------------------------------');
Writeln(F, '
Ausdruck erzeugt von: '+REDMON_USER);
Writeln(F, '
Computer : '+REDMON_MACHINE);
Writeln(F, '
Drucker : '+REDMON_PRINTER);
Writeln(F, '
Dokumentname : '+REDMON_DOCNAME);
Writeln(F, '
Jobnummer : '+REDMON_JOB);
Writeln(F, '
---------------------------------------------------');
Writeln(F, '
Erstellt von PDF2SMTP Version 1.4 am '+DateToStr(Date));
CloseFile(F);
{Logfile schreiben...}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Sende SMTP-Mail');
CloseFile(LOG);
{... und Mail versenden mit blat}
//Eckenroth result := ExecAndWait(pdf2smtpblat, '"'+BODYFILE+'"'+' -server "'+mailserver+'" -f "pdf2smtp@'+maildomain+'" -t "'+REDMON_USER+'@'+maildomain+'" -s "'+REDMON_DOCNAME+'" -log '+pdf2smtplog+' -attach '+'"'+PDFFILE+'"', SW_normal);
{Logfile schriben}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Exitcode von blat: '+BoolToStr(result, FALSE));
writeln(LOG, DateToStr(Date)+ '
Cleanup');
CloseFile(LOG);
{aufraemen}
AssignFile(BODY, BODYFILE);
Reset(BODY);
Close(BODY);
//ERASE(BODY);
AssignFile(PDF, PDFFILE);
Reset(PDF);
Close(PDF);
//ERASE(PDF);
{Ende im Logfile festhalten}
AssignFile(LOG, pdf2smtplog);
Append(LOG);
writeln(LOG, DateToStr(Date)+ '
Beendet --------------------------------------');
CloseFile(LOG);
end.