Files
wevads-platform/scripts/scripts_brain_send_production.php
2026-02-26 04:53:11 +01:00

60 lines
3.3 KiB
PHP
Executable File

<?php
/**
* BRAIN SEND v4 - Uses RICH creatives from affiliate.creatives
*/
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$toEmail = $argv[3] ?? null;
$ispTarget = strtoupper($argv[2] ?? 'GMAIL');
$requestedOffer = $argv[1] ?? null;
$winner = $pdo->query("SELECT c.*, w.inbox_rate FROM admin.brain_configs c JOIN admin.brain_winners w ON w.config_id = c.id WHERE w.is_active = true ORDER BY w.inbox_rate DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC);
if (!$winner) die("❌ No winner\n");
// Get offer with RICH creative + sponsor link
$sql = $requestedOffer
? "SELECT o.id, o.name, l.value as sponsor_url, c.value as creative_b64, c.name as creative_name
FROM affiliate.offers o
JOIN affiliate.links l ON l.offer_id=o.id AND l.type='preview' AND l.value LIKE 'http%'
JOIN affiliate.creatives c ON c.offer_id=o.id AND c.status='Activated' AND length(c.value) > 100
WHERE o.id = $requestedOffer ORDER BY RANDOM() LIMIT 1"
: "SELECT o.id, o.name, l.value as sponsor_url, c.value as creative_b64, c.name as creative_name
FROM affiliate.offers o
JOIN affiliate.links l ON l.offer_id=o.id AND l.type='preview' AND l.value LIKE 'http%'
JOIN affiliate.creatives c ON c.offer_id=o.id AND c.status='Activated' AND length(c.value) > 100
WHERE o.status = 'Activated' ORDER BY RANDOM() LIMIT 1";
$offer = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
if (!$offer) die("❌ No offer with rich creative\n");
// Get from_name and subject
$fn = $pdo->query("SELECT value FROM affiliate.from_names WHERE offer_id = {$offer['id']} ORDER BY RANDOM() LIMIT 1")->fetch(PDO::FETCH_ASSOC);
$sj = $pdo->query("SELECT value FROM affiliate.subjects WHERE offer_id = {$offer['id']} ORDER BY RANDOM() LIMIT 1")->fetch(PDO::FETCH_ASSOC);
$fromName = $fn['value'] ?? 'Customer Support';
$subject = $sj['value'] ?? 'Special offer';
$domain = $winner['domain_used'] ?: 'wevup.app';
$fromEmail = "hello@$domain";
$trackBase = "https://wevup.app";
$clickUrl = "$trackBase/click.php?url=" . base64_encode($offer['sponsor_url']) . "&oid={$offer['id']}&e=" . urlencode($toEmail);
$openPixel = "$trackBase/tracking.php?act=op&oid={$offer['id']}&e=" . urlencode($toEmail);
$unsubUrl = "$trackBase/click.php?act=unsub&e=" . urlencode($toEmail);
// Decode rich creative HTML and replace placeholders
$body = base64_decode($offer['creative_b64']);
$body = str_replace(['[url]', '[open]', '[unsub]'], [$clickUrl, $openPixel, $unsubUrl], $body);
echo "🚀 BRAIN v4 (RICH)\n";
echo " Offer: #{$offer['id']} - " . substr($offer['name'],0,45) . "\n";
echo " Creative: {$offer['creative_name']}\n";
echo " From: $fromName <$fromEmail>\n";
echo " Subject: $subject\n";
echo " To: $toEmail\n";
echo " HTML: " . strlen($body) . " bytes\n\n";
$jsonFile = tempnam("/tmp","brain_v4_");
file_put_contents($jsonFile, json_encode(["to"=>$toEmail,"from_email"=>$fromEmail,"from_name"=>$fromName,"subject"=>$subject,"body"=>$body,"domain"=>$domain]));
$response = trim(shell_exec("python3 /opt/wevads/scripts/bcg_local.py ".escapeshellarg($jsonFile)." 2>&1"));
@unlink($jsonFile);
$pdo->exec("UPDATE admin.brain_winners SET times_used = times_used + 1, last_used_at = NOW() WHERE config_id = {$winner['id']}");
echo strpos($response,'OK')!==false ? "✅ SENT!\n" : "❌ FAIL: $response\n";