|
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="X-UA-Compatible" content="IE=9" />
- <title>MassMailer</title>
- <HTA:APPLICATION ID="MassMailer" APPLICATIONNAME="MassMailer" BORDER="thick" SCROLL="yes" SINGLEINSTANCE="yes" />
- <style type="text/css">
- body { margin: 0; font: 15px Segoe UI, Arial, sans-serif; color: #243145; background: #f4f6f8; }
- header { background: #17324e; color: #fff; padding: 22px 28px; }
- h1 { margin: 0; font-size: 24px; }
- header p { margin: 7px 0 0; color: #d2dfec; }
- main { padding: 24px 28px; }
- .notice { padding: 14px 17px; background: #fff7e3; border-left: 4px solid #ca8f13; margin-bottom: 22px; }
- .cards { display: table; width: 100%; border-spacing: 10px; margin-left: -10px; }
- .card { display: table-cell; width: 25%; background: white; padding: 18px; border: 1px solid #dbe1e7; }
- .number { display: block; margin-top: 8px; font-size: 28px; color: #17324e; }
- .details { background: white; padding: 18px; border: 1px solid #dbe1e7; line-height: 1.7; }
- .details strong { display: inline-block; width: 135px; }
- button { background: #176b9b; color: white; border: 0; padding: 9px 16px; cursor: pointer; margin-top: 17px; }
- button:disabled { background: #a7b3bd; cursor: default; }
- .panel { background: white; padding: 18px; border: 1px solid #dbe1e7; margin-top: 18px; }
- .panel h2 { margin: 0 0 12px; font-size: 19px; }
- .field { display: block; margin: 12px 0; }
- .field input, .field select { display: block; width: 95%; max-width: 640px; padding: 7px; margin-top: 4px; }
- table { border-collapse: collapse; width: 100%; margin-top: 15px; }
- th, td { border-bottom: 1px solid #e4e8ec; padding: 7px; text-align: left; }
- .bad { color: #9c2929; }
- .good { color: #235e37; }
- #error { color: #9c2929; white-space: pre-wrap; margin-top: 12px; }
- </style>
- <script language="JScript" src="src/contacts.js"></script>
- <script language="JScript" type="text/javascript">
- var pendingCsvText = null;
- var pendingPreview = null;
- var pendingSource = "";
- var pendingPath = "";
-
- function projectRoot() {
- var path = decodeURIComponent(window.location.pathname);
- if (path.charAt(0) === "/" && path.charAt(2) === ":") path = path.substring(1);
- path = path.replace(/\//g, "\\");
- return new ActiveXObject("Scripting.FileSystemObject").GetParentFolderName(path);
- }
- function readSettings(filePath) {
- var stream = new ActiveXObject("ADODB.Stream");
- stream.Type = 2;
- stream.Charset = "utf-8";
- stream.Open();
- stream.LoadFromFile(filePath);
- var data = stream.ReadText();
- stream.Close();
- var result = {};
- var lines = data.replace(/\r\n?/g, "\n").split("\n");
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i].replace(/^\s+|\s+$/g, "");
- if (!line || line.charAt(0) === "#" || line.charAt(0) === ";") continue;
- var equalAt = line.indexOf("=");
- if (equalAt > 0) result[line.substring(0, equalAt).replace(/\s/g, "").toUpperCase()] =
- line.substring(equalAt + 1).replace(/^\s+|\s+$/g, "");
- }
- return result;
- }
- function countRows(db, tableName) {
- var rows = db.Execute("SELECT COUNT(*) AS N FROM [" + tableName + "]");
- var result = rows.Fields("N").Value;
- rows.Close();
- return result;
- }
- function openDatabase() {
- var fs = new ActiveXObject("Scripting.FileSystemObject");
- var root = projectRoot();
- var config = readSettings(fs.BuildPath(root, "config.ini"));
- var dbPath = config.DB_PATH;
- if (fs.GetDriveName(dbPath) === "") dbPath = fs.BuildPath(root, dbPath);
- dbPath = fs.GetAbsolutePathName(dbPath);
- var db = new ActiveXObject("ADODB.Connection");
- db.Open("Provider=" + config.DB_PROVIDER + ";Data Source=" + dbPath + ";");
- return { db: db, config: config, path: dbPath };
- }
- function refreshStatus() {
- var db = null;
- try {
- var opened = openDatabase();
- db = opened.db;
- document.getElementById("contacts").innerText = countRows(db, "Contact");
- document.getElementById("suppressed").innerText = countRows(db, "Suppression");
- document.getElementById("templates").innerText = countRows(db, "TemplateVersion");
- document.getElementById("campaigns").innerText = countRows(db, "Campaign");
- document.getElementById("database").innerText = opened.path;
- document.getElementById("provider").innerText = opened.config.DB_PROVIDER;
- document.getElementById("interval").innerText = opened.config.QUEUE_INTERVAL + " seconds";
- document.getElementById("testTo").innerText = opened.config.SMTP_TEST_TO || "Not configured";
- document.getElementById("error").innerText = "";
- } catch (error) {
- document.getElementById("error").innerText = "Status refresh failed: " + error.message;
- } finally {
- if (db !== null && db.State === 1) db.Close();
- }
- }
- function readCsvFile(path) {
- var fs = new ActiveXObject("Scripting.FileSystemObject");
- if (!fs.FileExists(path)) throw new Error("CSV file not found: " + path);
- if (fs.GetFile(path).Size > 2097152) throw new Error("CSV exceeds the 2 MB import limit");
- var stream = new ActiveXObject("ADODB.Stream");
- stream.Type = 2;
- stream.Charset = "utf-8";
- stream.Open();
- stream.LoadFromFile(path);
- var text = stream.ReadText();
- stream.Close();
- return text;
- }
- function emailLookup(db, tableName) {
- var found = {};
- var rows = db.Execute("SELECT [EmailKey] FROM [" + tableName + "]");
- while (!rows.EOF) {
- found[String(rows.Fields("EmailKey").Value).toLowerCase()] = true;
- rows.MoveNext();
- }
- rows.Close();
- return found;
- }
- function renderPreview(preview) {
- document.getElementById("previewCount").innerText = preview.accepted.length +
- " ready, " + preview.rejected.length + " rejected";
- var body = document.getElementById("previewBody");
- while (body.firstChild) body.removeChild(body.firstChild);
- var items = preview.accepted.concat(preview.rejected);
- items.sort(function (a, b) { return a.line - b.line; });
- for (var i = 0; i < items.length; i++) {
- var tr = document.createElement("tr");
- var values = [items[i].line, items[i].email, items[i].businessName,
- items[i].source, items[i].reason || "Ready"];
- for (var j = 0; j < values.length; j++) {
- var td = document.createElement("td");
- td.innerText = values[j];
- if (j === 4) td.className = items[i].reason ? "bad" : "good";
- tr.appendChild(td);
- }
- body.appendChild(tr);
- }
- document.getElementById("commitImport").disabled = preview.accepted.length === 0;
- }
- function previewContacts() {
- var db = null;
- document.getElementById("importMessage").innerText = "";
- document.getElementById("commitImport").disabled = true;
- try {
- var path = document.getElementById("csvPath").value;
- pendingPath = path;
- pendingSource = document.getElementById("sourceFallback").value.replace(/^\s+|\s+$/g, "");
- pendingCsvText = readCsvFile(path);
- var opened = openDatabase();
- db = opened.db;
- pendingPreview = MassMailerContacts.preview(pendingCsvText, pendingSource,
- emailLookup(db, "Contact"), emailLookup(db, "Suppression"));
- renderPreview(pendingPreview);
- } catch (error) {
- pendingPreview = null;
- document.getElementById("importMessage").innerText = error.message;
- } finally {
- if (db !== null && db.State === 1) db.Close();
- }
- }
- function utcNowSql() {
- return "DateAdd('n'," + new Date().getTimezoneOffset() + ",Now())";
- }
- function importOne(db, row) {
- var dateSql = utcNowSql();
- if (row.acquiredAt) {
- dateSql = "DateSerial(" + Number(row.acquiredAt.substring(0, 4)) + "," +
- Number(row.acquiredAt.substring(5, 7)) + "," +
- Number(row.acquiredAt.substring(8, 10)) + ")";
- }
- var cmd = new ActiveXObject("ADODB.Command");
- cmd.ActiveConnection = db;
- cmd.CommandText = "INSERT INTO [Contact] ([EmailOriginal],[EmailKey],[BusinessName]," +
- "[SourceReference],[AcquiredAtUtc],[Country],[EligibilityBasis],[CreatedAtUtc]) " +
- "VALUES (?,?,?,? ," + dateSql + ",'US','public_listing'," + utcNowSql() + ")";
- cmd.Parameters.Append(cmd.CreateParameter("email", 202, 1, 254, row.email));
- cmd.Parameters.Append(cmd.CreateParameter("key", 202, 1, 254, row.emailKey));
- cmd.Parameters.Append(cmd.CreateParameter("business", 202, 1, 200, row.businessName));
- cmd.Parameters.Append(cmd.CreateParameter("source", 203, 1, Math.max(1, row.source.length), row.source));
- cmd.Execute();
- }
- function commitContacts() {
- if (!pendingPreview || !pendingPreview.accepted.length) return;
- var db = null, inTransaction = false, succeeded = false;
- try {
- if (document.getElementById("csvPath").value !== pendingPath)
- throw new Error("CSV file selection changed. Preview it again.");
- if (document.getElementById("sourceFallback").value.replace(/^\s+|\s+$/g, "") !== pendingSource)
- throw new Error("Source reference changed. Preview the CSV again.");
- var opened = openDatabase();
- db = opened.db;
- var current = MassMailerContacts.preview(pendingCsvText, pendingSource,
- emailLookup(db, "Contact"), emailLookup(db, "Suppression"));
- if (current.accepted.length !== pendingPreview.accepted.length ||
- current.rejected.length !== pendingPreview.rejected.length)
- throw new Error("Contacts or suppressions changed. Preview the CSV again.");
- for (var check = 0; check < current.accepted.length; check++) {
- if (current.accepted[check].emailKey !== pendingPreview.accepted[check].emailKey)
- throw new Error("Contacts changed. Preview the CSV again.");
- }
- db.BeginTrans();
- inTransaction = true;
- for (var i = 0; i < current.accepted.length; i++) importOne(db, current.accepted[i]);
- db.CommitTrans();
- inTransaction = false;
- document.getElementById("importMessage").innerText =
- "Imported " + current.accepted.length + " contacts. Rejected rows were not saved.";
- pendingPreview = null;
- document.getElementById("commitImport").disabled = true;
- succeeded = true;
- } catch (error) {
- if (inTransaction) db.RollbackTrans();
- document.getElementById("importMessage").innerText = "Import failed: " + error.message;
- } finally {
- if (db !== null && db.State === 1) db.Close();
- if (succeeded) refreshStatus();
- }
- }
- function addSuppression() {
- var db = null, succeeded = false;
- try {
- var email = document.getElementById("suppressEmail").value.replace(/^\s+|\s+$/g, "");
- if (!MassMailerContacts.isEmail(email)) throw new Error("Enter a valid email address");
- var key = MassMailerContacts.emailKey(email);
- var reason = document.getElementById("suppressReason").value;
- var evidence = document.getElementById("suppressEvidence").value.replace(/^\s+|\s+$/g, "");
- var opened = openDatabase();
- db = opened.db;
- if (emailLookup(db, "Suppression")[key]) throw new Error("Address is already suppressed");
- var cmd = new ActiveXObject("ADODB.Command");
- cmd.ActiveConnection = db;
- cmd.CommandText = "INSERT INTO [Suppression] ([EmailKey],[Reason],[RecordedAtUtc]," +
- "[EvidenceReference]) VALUES (?, ?, " + utcNowSql() + ", ?)";
- cmd.Parameters.Append(cmd.CreateParameter("email", 202, 1, 254, key));
- cmd.Parameters.Append(cmd.CreateParameter("reason", 202, 1, 40, reason));
- cmd.Parameters.Append(cmd.CreateParameter("evidence", 203, 1, Math.max(1, evidence.length), evidence));
- cmd.Execute();
- document.getElementById("suppressMessage").innerText = "Suppressed " + key;
- document.getElementById("suppressEmail").value = "";
- succeeded = true;
- } catch (error) {
- document.getElementById("suppressMessage").innerText = "Suppression failed: " + error.message;
- } finally {
- if (db !== null && db.State === 1) db.Close();
- if (succeeded) refreshStatus();
- }
- }
- function powerShellHost() {
- var fs = new ActiveXObject("Scripting.FileSystemObject");
- var shell = new ActiveXObject("WScript.Shell");
- var root = shell.ExpandEnvironmentStrings("%SystemRoot%");
- var settings = readSettings(fs.BuildPath(projectRoot(), "config.ini"));
- var folder = (settings.DB_BITNESS || "").toLowerCase() === "x64" ? "System32" :
- (fs.FolderExists(root + "\\SysWOW64") ? "SysWOW64" : "System32");
- return root + "\\" + folder + "\\WindowsPowerShell\\v1.0\\powershell.exe";
- }
- function quoted(value) { return '"' + value + '"'; }
- function configurePassword() {
- try {
- var fs = new ActiveXObject("Scripting.FileSystemObject");
- var script = fs.BuildPath(projectRoot(), "tools\\set-smtp-password.ps1");
- var command = quoted(powerShellHost()) + " -NoProfile -ExecutionPolicy Bypass -File " + quoted(script);
- var status = new ActiveXObject("WScript.Shell").Run(command, 1, true);
- document.getElementById("transportMessage").innerText = status === 0 ?
- "SMTP password setup completed." : "SMTP password setup failed. Review the console error.";
- } catch (error) {
- document.getElementById("transportMessage").innerText = "Setup failed: " + error.message;
- }
- }
- function sendInternalTest() {
- try {
- var fs = new ActiveXObject("Scripting.FileSystemObject");
- var root = projectRoot();
- var settings = readSettings(fs.BuildPath(root, "config.ini"));
- if (!settings.SMTP_TEST_TO) throw new Error("Set SMTP_TEST_TO in config.ini first");
- if (!window.confirm("Send one internal SMTP test to " + settings.SMTP_TEST_TO + "?")) return;
- var script = fs.BuildPath(root, "tools\\test-smtp-message.ps1");
- var command = quoted(powerShellHost()) + " -NoProfile -ExecutionPolicy Bypass -File " +
- quoted(script) + " -Send";
- var process = new ActiveXObject("WScript.Shell").Exec(command);
- var output = process.StdOut.ReadAll();
- var errors = process.StdErr.ReadAll();
- document.getElementById("transportMessage").innerText =
- (process.ExitCode === 0 ? "" : "Test failed. ") + output + errors;
- } catch (error) {
- document.getElementById("transportMessage").innerText = "Test failed: " + error.message;
- }
- }
- </script>
- </head>
- <body onload="refreshStatus()">
- <header><h1>MassMailer</h1><p>9 x 12 postcard marketing campaign workspace</p></header>
- <main>
- <div class="notice"><strong>Campaign sending is disabled in this build.</strong> The SMTP test sends one message only when you press its button.</div>
- <div class="cards">
- <div class="card">Contacts<span class="number" id="contacts">—</span></div>
- <div class="card">Suppressed<span class="number" id="suppressed">—</span></div>
- <div class="card">Templates<span class="number" id="templates">—</span></div>
- <div class="card">Campaigns<span class="number" id="campaigns">—</span></div>
- </div>
- <div class="details">
- <div><strong>Database</strong><span id="database">Checking…</span></div>
- <div><strong>Provider</strong><span id="provider">Checking…</span></div>
- <div><strong>Send interval</strong><span id="interval">Checking…</span></div>
- <div><strong>Internal test to</strong><span id="testTo">Checking…</span></div>
- <div><strong>Send state</strong>Disabled</div>
- <button onclick="refreshStatus()">Refresh status</button>
- <div id="error"></div>
- </div>
- <div class="panel">
- <h2>SMTP setup and internal test</h2>
- <p>The password entry opens a local console and stores a protected value in the MDB. The test sends one HTML message with an inline image to the configured test inbox.</p>
- <button onclick="configurePassword()">Set or rotate SMTP password</button>
- <button onclick="sendInternalTest()">Send one internal test</button>
- <div id="transportMessage"></div>
- </div>
- <div class="panel">
- <h2>Import business contacts</h2>
- <p>UTF-8 CSV needs an <code>email</code> header. Optional headers: <code>business_name</code>, <code>source_reference</code>, and <code>acquired_at</code> (YYYY-MM-DD). Every accepted row needs a source reference.</p>
- <label class="field">CSV file <input id="csvPath" type="file" accept=".csv" /></label>
- <label class="field">Source URL or reference for rows without one
- <input id="sourceFallback" type="text" maxlength="2048" /></label>
- <button onclick="previewContacts()">Preview import</button>
- <button id="commitImport" onclick="commitContacts()" disabled="disabled">Import ready contacts</button>
- <div id="importMessage"></div>
- <p id="previewCount"></p>
- <table><thead><tr><th>CSV row</th><th>Email</th><th>Business</th><th>Source</th><th>Result</th></tr></thead>
- <tbody id="previewBody"></tbody></table>
- </div>
- <div class="panel">
- <h2>Suppress an address</h2>
- <p>Add unsubscribe replies here before the next send. Suppression applies across all campaigns.</p>
- <label class="field">Email address <input id="suppressEmail" type="text" maxlength="254" /></label>
- <label class="field">Reason <select id="suppressReason"><option value="opt-out">Opt-out reply</option><option value="manual">Manual</option><option value="bounce">Hard bounce</option><option value="complaint">Complaint</option></select></label>
- <label class="field">Evidence or note <input id="suppressEvidence" type="text" maxlength="2048" /></label>
- <button onclick="addSuppression()">Suppress address</button>
- <div id="suppressMessage"></div>
- </div>
- </main>
- </body>
- </html>
|