Patch level : 12.0 nopatch

Files correlati     :
Commento        :

Modifiche invio mail tramite campo
This commit is contained in:
smen 2021-10-19 09:40:38 +02:00
parent becdf257ca
commit 35580c4a41

View File

@ -6,6 +6,7 @@
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/mimetype.h>
#include <wx/textfile.h>
#include <fstream>
static wxString GetMailParam(const char* key, const char* def = "")
@ -284,6 +285,30 @@ BOOLEAN xvt_mail_send(const char* to, const char* cc, const char* ccn,
return ok;
}
void buildPSArray(wxString& str)
{
if (str.Find(";") >= 0)
{
wxStringTokenizer str_tok(str, ";");
str = "";
while (str_tok.HasMoreTokens())
{
if (str_tok.GetPosition() > 0)
str << ",";
str << '\'' << str_tok.GetNextToken() << '\'';
}
}
else
{
wxString wrkstr(str);
str = "\'";
str << wrkstr << "\'";
}
}
BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
const char* subject, const char* msg,
const char* attach, short flags, const char* usr)
@ -328,9 +353,9 @@ BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
}
// Hard code and no play makes Tolla a dull programmer
file.Write("$Server = \""); file.Write(server); file.Write("\";\n");
file.Write("$Server = \""); file.Write(server); file.Write("\"\n");
file.Write("$Port = \""); file.Write(port_number); file.Write("\";\n");
file.Write("$Port = \""); file.Write(port_number); file.Write("\"\n");
file.Write("$SSL = ");
if(ssl)
@ -339,37 +364,59 @@ BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
file.Write("$false");
file.Write("\n");
file.Write("$Username = \""); file.Write(user); file.Write("\";\n");
file.Write("$Password = \""); file.Write(password); file.Write("\";\n");
file.Write("$From = \""); file.Write(from); file.Write("\";\n");
file.Write("$To = \""); file.Write(to); file.Write("\";\n");
file.Write("$Subject = \""); file.Write(subject); file.Write("\";\n");
file.Write("$Body = \""); file.Write(msg); file.Write("\";\n");
file.Write("$Username = \""); file.Write(user); file.Write("\"\n");
file.Write("$Password = \""); file.Write(password); file.Write("\"\n");
file.Write("$From = \""); file.Write(from); file.Write("\"\n");
wxString str_to(to);
buildPSArray(str_to);
file.Write("$To = "); file.Write(str_to); file.Write("\n");
file.Write("$Subject = \""); file.Write(subject); file.Write("\"\n");
file.Write("$Body = \""); file.Write(msg); file.Write("\"\n");
if (hascc)
{
file.Write("$CC = \""); file.Write(cc); file.Write("\";\n");
wxString str_cc(cc);
buildPSArray(str_cc);
file.Write("$CC = "); file.Write(str_cc); file.Write("\n");
}
if (hasccn)
{
file.Write("$Bcc = \""); file.Write(ccn); file.Write("\";\n");
wxString str_ccn(ccn);
buildPSArray(str_ccn);
file.Write("$Bcc = "); file.Write(str_ccn); file.Write("\n");
}
file.Write(
"$message = new-object Net.Mail.MailMessage;\n"
"$message = new-object Net.Mail.MailMessage\n"
"$message.From = $From\n"
"$message.To.Add($To);\n");
"$message.To.Add($To)\n");
//TODO same per from e to
//mettere un flag nella mask per decidere se mettere in CC o in CCn
if (hascc == true)
{
file.Write(
"foreach($i in $CC)\n"
"{ $message.CC.Add($i) }\n"
);
}
if (hascc == true)
file.Write("$message.CC.Add($CC);\n");
if (hasccn == true)
file.Write("$message.Bcc.Add($Bcc);\n");
{
file.Write(
"foreach($i in $Bcc)\n"
"{ $message.Bcc.Add($i)}"
);
}
file.Write(
"$message.Subject = $Subject;\n"
"$message.Body = $Body;\n"
"$message.Subject = $Subject\n"
"$message.Body = $Body\n"
);
if (flags & 0x2)
file.Write("$message.DeliveryNotificationOptions = 1;\n");
file.Write("$message.DeliveryNotificationOptions = 1\n");
// Aggiungo a schiena gli allegati
if (attach && *attach)
@ -381,7 +428,7 @@ BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
wxString attachmentLine;
attachmentLine << "$attach" << ++i << "= New-Object Net.Mail.Attachment(\""
<< tokAttach.GetNextToken() << "\");\n" << "$message.Attachments.Add("
<< "$attach" << i << ");\n";
<< "$attach" << i << ")\n";
file.Write(attachmentLine);
}
}
@ -394,21 +441,46 @@ BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
"try\n"
"{\n"
" $smtp.send($message);\n"
" write-host \"E-Mail sent\" ; \n"
" exit 1\n"
" write-host \"E-Mail sent\" \n"
" $exitCode = 1 \n"
" Out-File -FilePath \"C:\\Users\\smen\\Desktop\\Pmail\\ExitCode.txt\" -InputObject $exitCode \n"
" exit $exitCode \n"
"}\n"
"catch\n"
"{\n"
" write-host \"E-Mail not sent\" ; \n"
" $exitCode = 0\n"
" $exitCode = 0 \n"
" Out-File -FilePath \".\ExitCode.txt\" -InputObject $exitCode \n"
" exit $exitCode \n"
"}\n"
"$host.SetShouldExit($exitCode);\n"
);
file.Close();
wxString command;
command << R"(PowerShell -ExecutionPolicy ByPass -NonInteractive -NoProfile -Command "{)" << powerFile << R"(; exit $LastExitCode }")";
wxString command("powershell.exe -File ");
// command << R"(PowerShell -ExecutionPolicy ByPass -NonInteractive -NoProfile -Command "{)" << powerFile << R"(; exit $LastExitCode }")";
//una volta scritto lo scritp ps1 cos'è che lo runna? Dove vinene aperta la PowerShell?
// al momento lo script generato da campo funziona se runnato direttamente da powershell
command << powerFile;
int exit = xvt_sys_execute(command, true, true);
//nt exit = system( + powerFile);
//wxString exitFile;
//wxString exitFileName = "ExitCode.txt";
//xvt_fsys_build_pathname(exitFile.GetWriteBuf(_MAX_PATH), NULL, userTemp, exitFileName, NULL , NULL);
//exitFile.UngetWriteBuf();
// Creo il file di log
//wxTextFile logFile;
//logFile.Open(exitFile);
//wxString logStr = logFile.GetFirstLine();
//int exitCode;
//exitCode = wxAtoi(logStr);
//system("powershell.exe $lastexitcode");
/*
BOOLEAN sys_command = (BOOLEAN)system(command);
if (!sys_command)
{
@ -422,5 +494,8 @@ BOOLEAN xvt_powermail_send(const char* to, const char* cc, const char* ccn,
fout.close();
}
}
return sys_command;
*/
//sys.command;
return exit
;
}