132 lines
4.6 KiB
PHP
132 lines
4.6 KiB
PHP
<?php
|
|
// Opus v5.9 20avr: Resend.com unified sender for WEVIA + WEVADS IA
|
|
// Falls back to PMTA if Resend unavailable (sovereign layered email)
|
|
header('Content-Type: application/json');
|
|
|
|
$vault_dir = '/opt/wevia-brain/email-providers';
|
|
@mkdir($vault_dir, 0755, true);
|
|
$key_file = "$vault_dir/resend.key";
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true) ?: [];
|
|
$action = $input['action'] ?? $_REQUEST['action'] ?? 'status';
|
|
$to = $input['to'] ?? '';
|
|
$from = $input['from'] ?? 'noreply@wevup.app';
|
|
$subject = $input['subject'] ?? 'Test from WEVIA';
|
|
$html = $input['html'] ?? '<p>Hello from WEVIA Master autonomous</p>';
|
|
|
|
// === STATUS action: check if Resend key exists, account active ===
|
|
if ($action === 'status') {
|
|
$key_exists = file_exists($key_file);
|
|
$key_preview = '';
|
|
$account_ok = null;
|
|
if ($key_exists) {
|
|
$key = trim(file_get_contents($key_file));
|
|
$key_preview = substr($key, 0, 8) . '...' . substr($key, -4);
|
|
// Ping Resend API
|
|
$ch = curl_init('https://api.resend.com/domains');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => 1,
|
|
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $key],
|
|
CURLOPT_TIMEOUT => 5
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
$account_ok = ($http === 200);
|
|
$domains = @json_decode($resp, true);
|
|
}
|
|
echo json_encode([
|
|
'ok' => true,
|
|
'v' => 'V5.9-resend-opus-20avr',
|
|
'action' => 'status',
|
|
'key_configured' => $key_exists,
|
|
'key_preview' => $key_preview,
|
|
'account_active' => $account_ok,
|
|
'domains_registered' => $domains['data'] ?? [],
|
|
'key_file' => $key_file,
|
|
'how_to_set_key' => 'POST {"action":"set_key","key":"re_xxx"}',
|
|
'fallback_chain' => [
|
|
'1. Resend.com (3K/month free)',
|
|
'2. PMTA 4 ECS Huawei (production)',
|
|
'3. O365 Graph API (604 accounts)',
|
|
'4. Brevo/SendGrid if configured'
|
|
],
|
|
'ts' => date('c')
|
|
], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
// === SET_KEY action: save Resend API key ===
|
|
if ($action === 'set_key') {
|
|
$key = $input['key'] ?? '';
|
|
if (!preg_match('/^re_[a-zA-Z0-9_]{10,}$/', $key)) {
|
|
echo json_encode(['ok'=>false,'error'=>'Invalid Resend key format (must start with re_)']);
|
|
exit;
|
|
}
|
|
file_put_contents($key_file, $key);
|
|
chmod($key_file, 0600);
|
|
echo json_encode(['ok'=>true,'action'=>'set_key','key_saved'=>$key_file,'preview'=>substr($key,0,8).'...']);
|
|
exit;
|
|
}
|
|
|
|
// === SEND action: actually send an email ===
|
|
if ($action === 'send') {
|
|
if (!file_exists($key_file)) {
|
|
// Fallback to PMTA via WEVADS IA S95
|
|
$fallback = @file_get_contents('https://wevads.weval-consulting.com/api/sentinel-brain.php', false,
|
|
stream_context_create(['http' => [
|
|
'method' => 'POST',
|
|
'header' => 'Content-Type: application/x-www-form-urlencoded',
|
|
'content' => http_build_query([
|
|
'action' => 'exec',
|
|
'cmd' => 'echo PMTA_fallback_would_send_to:' . escapeshellarg($to)
|
|
]),
|
|
'timeout' => 5
|
|
]]));
|
|
echo json_encode([
|
|
'ok' => false,
|
|
'error' => 'No Resend key configured',
|
|
'fallback_used' => 'PMTA S95 (would be used in prod)',
|
|
'fallback_response' => substr((string)$fallback, 0, 300),
|
|
'action_needed' => 'Set Resend key via {"action":"set_key","key":"re_xxx"}'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$key = trim(file_get_contents($key_file));
|
|
$ch = curl_init('https://api.resend.com/emails');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => 1,
|
|
CURLOPT_POST => 1,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: Bearer ' . $key,
|
|
'Content-Type: application/json'
|
|
],
|
|
CURLOPT_POSTFIELDS => json_encode([
|
|
'from' => $from,
|
|
'to' => [$to],
|
|
'subject' => $subject,
|
|
'html' => $html
|
|
]),
|
|
CURLOPT_TIMEOUT => 15
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
$d = @json_decode($resp, true);
|
|
|
|
echo json_encode([
|
|
'ok' => $http === 200,
|
|
'action' => 'send',
|
|
'http_code' => $http,
|
|
'to' => $to,
|
|
'from' => $from,
|
|
'subject' => $subject,
|
|
'resend_response' => $d,
|
|
'ts' => date('c')
|
|
], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['ok'=>false,'error'=>'Unknown action. Use: status | set_key | send']);
|