#include "wxinc.h" #include "xvt.h" #include "email.h" #include #include static wxString GetMailParam(const char* key, const char* def = "") { static wxString ini; if (ini.IsEmpty()) { wxString cu = "ADMIN"; for (int i = __argc-1; i > 1; i--) { wxString u = __argv[i]; u.MakeUpper(); if (u.StartsWith("/U") || u.StartsWith("-U")) { cu = u.Mid(2); break; } } char study[_MAX_PATH]; xvt_sys_get_profile_string(NULL, "Main", "Study", "", study, sizeof(study)); ini = study; if (!wxEndsWithPathSeparator(ini)) ini += wxFILE_SEP_PATH; ini += "config\\"; ini += cu; ini += ".ini"; } wxString val; const size_t sz = _MAX_PATH; xvt_sys_get_profile_string(ini, "Mail", key, def, val.GetWriteBuf(sz), sz); val.UngetWriteBuf(); return val; } static bool GetMailParams(wxString& smtp, wxString& port, wxString& user, wxString& pass, wxString& from) { smtp = GetMailParam("Server", "MAPI"); port = GetMailParam("Port"); user = GetMailParam("User"); pass = GetMailParam("Password"); wxString f = user; f += "@"; f += smtp.AfterFirst('.'); from = GetMailParam("From", f); return !smtp.IsEmpty() && !pass.IsEmpty(); } short xvt_mail_installed() { short bInstalled = 0; if (::GetProfileInt(_T("MAIL"), _T("MAPI"), 0) != 0 && SearchPath(NULL, _T("MAPI32.DLL"), NULL, 0, NULL, NULL) != 0) bInstalled |= 0x1; if (xvt_fsys_file_exists("servers/mailsend.exe")) { wxString smtp, port, user, pass, from; GetMailParams(smtp, port, user, pass, from); if (!pass.IsEmpty() && smtp != "MAPI") bInstalled |= 0x2; } return bInstalled; } static void AppendQuotedString(wxString& cmd, const char* key, const wxString& value) { if (!value.IsEmpty()) { cmd += " -"; cmd += key; cmd += " \""; cmd += value; cmd += "\""; } } BOOLEAN xvt_mail_send(const char* to, const char* cc, const char* ccn, const char* subject, const char* msg, const char* attach, short flags) { bool ui = (flags & 0x1) != 0; wxStringTokenizer tokTo(to, _T(";")); wxMailMessage Msg(subject, tokTo.GetNextToken(), msg); if (flags & 0x2) Msg.m_query_receipt = true; while (tokTo.HasMoreTokens()) Msg.AddTo(tokTo.GetNextToken()); if (Msg.m_to[0].IsEmpty()) { Msg.m_to[0] = " "; // Il destinatario "" fa piantare MAPI con errore 25 ui = true; // Forza user interface in assenza di recipient } if (attach && *attach) { wxStringTokenizer tokAttach(attach, _T(";")); while (tokAttach.HasMoreTokens()) Msg.AddAttachment(tokAttach.GetNextToken()); } if (cc && *cc) { wxStringTokenizer Tok(cc, _T(";")); while (Tok.HasMoreTokens()) Msg.AddCc(Tok.GetNextToken()); } if (ccn && *ccn) { wxStringTokenizer Tok(ccn, _T(";")); while (Tok.HasMoreTokens()) Msg.AddBcc(Tok.GetNextToken()); } const short mail_inst = xvt_mail_installed(); BOOLEAN ok = FALSE; wxString server, port, user, password, from; if ((mail_inst & 0x2) && GetMailParams(server, port, user, password, from)) { wxString cmd = "servers/mailsend.exe"; AppendQuotedString(cmd, "to", Msg.m_to[0]); AppendQuotedString(cmd, "from", from); if (Msg.m_cc.IsEmpty()) cmd += " +cc"; else AppendQuotedString(cmd, "cc", Msg.m_cc[0]); if (Msg.m_bcc.IsEmpty()) cmd += " +bc"; else AppendQuotedString(cmd, "bc", Msg.m_bcc[0]); AppendQuotedString(cmd, "smtp", server); if (!port.IsEmpty()) { cmd += " -port "; cmd += port; } AppendQuotedString(cmd, "sub", Msg.m_subject); AppendQuotedString(cmd, "user", user); AppendQuotedString(cmd, "pass", password); if (!Msg.m_attachments.IsEmpty()) { for (size_t a = 0; a < Msg.m_attachments.size(); a++) AppendQuotedString(cmd, "attach", Msg.m_attachments[a]); } wxArrayString output, error; wxExecute(cmd, output, error, wxEXEC_SYNC); ok = FALSE; for (size_t i = 0; !ok && i < error.size(); i++) ok = error[i].find("uccess") > 0; } else if (mail_inst & 1) { xvt_fsys_save_dir(); wxEmail Mail; ok = Mail.Send(Msg, wxEmptyString, ui); xvt_fsys_restore_dir(); } return ok; }