******************************************************************************
// add: <cMitt> come parametro opzionale - 30/03/2019
function Spedisci(oWnd,aAddr,cOggetto,cText,cHtm,aAllega,lSilent,aCC,aCCn,nPort,cMitt,cPwd)
local lRet, nTime := 0
local oMail
local cServer, cUser, lReceipt
if SETUP->(fieldpos("SMTPSERV")) > 0 // ctrl se esiste campo su Setup
default cMitt to trim(SETUP->MailMitte) // mittente
cServer := trim(SETUP->SmtpServ) //
cUser := trim(SETUP->SmtpUser) //
default cPwd := trim(SETUP->SmtpPass) // mod: default - 14/03/2025
endif
if SETUP->(fieldpos("SMTPPORT")) > 0 // ctrl se esiste campo su Setup
default nPort := val(alltrim(SETUP->SmtpPort)) // porta Smtp
endif
// valori di default
cMitt := if(!empty(cMitt),cMitt,"support@mydomain.it") // mittente
cServer := if(!empty(cServer),cServer,"smtp.mydomain.it") // mailserver
cUser := if(!empty(cUser),cUser,"support@mydomain.it") // username x autenticazione
cPwd := if(!empty(cPwd),cPwd,"mypassword") // password
nPort := if(!empty(nPort), nPort, 25 ) // add: nPort - 30/03/2018
default aAddr := {} // add: 09/10/2021
default aCC := {} //
default aCCn := {} //
aEval(aAddr,{|c,n| aAddr[n] := alltrim(c) }) // add: 09/10/2021
aEval(aCC, {|c,n| aCC[n] := alltrim(c) }) //
aEval(aCCn, {|c,n| aCCn[n] := alltrim(c) }) //
if oApp:lDemo .and. !lSilent
msginfo("Impossibile inviare mail in modalità demo.")
return (.f.)
endif
oMail := TSmtp():New(cServer, nPort, .t., cUser, cPwd) // .t. = autenticazione
//tracelog(cMitt,cServer,cUser,cPwd,oMail:cIpServer)
oMail:bConnected := {|| piede("Connesso, invio in corso..."+aAddr[1]) }
oMail:bConnecting := {|| piede("In connessione..."), cursorwait() }
oMail:bDone := {|oSmtp| /*WSACleanup(), */ piede("messaggio inviato."), lRet := .t. } // add: WSACleanup() 27/02/2026
#ifdef CGI
oMail:bFailure := {|oSmtp,cErr,cLastReply| tracelogNew( {cErr," Last reply: "+cLastReply}, "SendMail.log" ), lRet := .f. }
#else
#ifdef FW2512
// mod: 27/02/2026
oMail:bFailure := {|oSmtp,nWSAErr,cLastReply| alert("WSA Error Code: "+AllTrim(Str(nWSAErr))+" Last reply: "+cLastReply,,,,,8),;
tracelogNew({nWSAErr," Last reply: "+cLastReply}, "SendMail.log"),;
lRet := .f. }
#else
oMail:bFailure := {|oSmtp,cErr,cLastReply| alert(cErr+" Last reply: "+cLastReply,,,,,8),;
tracelogNew({cErr," Last reply: "+cLastReply}, "SendMail.log"),;
lRet := .f. }
#endif
#endif
oMail:nDelay := 5 // era 1.5 - 19/03/2009 - 10/06/2009 - 5=05/09/2014
oMail:SendMail(cMitt,aAddr,cText,cOggetto,aAllega,aCC,aCCn,lReceipt,cHtm)
while lRet == nil .and. nTime++ < 60 // 1 min max per ogni mail
cursorwait()
sysrefresh()
sysWait(1)
end
piede()
cursorArrow()
default lRet to ( oMail:end(), .f. ) // add: oMail:end() - 05/04/2018
tracelogNew( {if( lRet,"","Non ")+"Spedita => "+cServer, cMitt, aAddr, aCC, aCCn,;
"last reply: "+cvalToChar(atail(oMail:acReply)),;
"last error: "+oMail:cError, "Status: "+alltrim(str(oMail:nStatus)),;
nTime, cOggetto, cText, cHtm, aAllega }, "SendMail.log" )
#ifdef FW2512
syswait(0.2) // microsleep - 05/03/2026
#endif
return (lRet)