Files
html/api/wevia-email-api.php
2026-04-12 22:57:03 +02:00

62 lines
2.6 KiB
PHP

<?php
/**
* WEVIA LIFE Email Send API
* POST ?action=send {"to":"x@y.com","subject":"Test","body":"Content","from":"ymahboub@weval-consulting.com"}
* POST ?action=list_boxes — list configured mailboxes
* POST ?action=read {"box":"ymahboub@weval-consulting.com","limit":5}
*/
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") { http_response_code(200); exit; }
$input = json_decode(file_get_contents("php://input"), true) ?: [];
$action = $_GET["action"] ?? $input["action"] ?? "status";
switch ($action) {
case "status":
echo json_encode(["status" => "ok", "service" => "WEVIA LIFE Email", "capabilities" => ["send", "read", "list_boxes"]]);
break;
case "list_boxes":
echo json_encode(["boxes" => [
["email" => "ymahboub@weval-consulting.com", "type" => "cPanel/IMAP", "status" => "active"],
["email" => "yacineutt@gmail.com", "type" => "Gmail/SMTP", "status" => "configured"],
["email" => "contact@weval-consulting.com", "type" => "cPanel/IMAP", "status" => "active"],
]]);
break;
case "send":
$to = $input["to"] ?? "";
$subject = $input["subject"] ?? "Message WEVIA";
$body = $input["body"] ?? "";
$from = $input["from"] ?? "ymahboub@weval-consulting.com";
if (empty($to) || empty($body)) {
echo json_encode(["error" => "to and body required"]);
exit;
}
// Use PHP mail() with custom headers
$headers = "From: WEVIA <$from>\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "X-Mailer: WEVIA-LIFE/2.0\r\n";
$htmlBody = "<html><body style=\"font-family:Arial,sans-serif\">";
$htmlBody .= nl2br(htmlspecialchars($body));
$htmlBody .= "<br><br><small style=\"color:#999\">Envoyé via WEVIA LIFE — weval-consulting.com</small>";
$htmlBody .= "</body></html>";
$sent = mail($to, $subject, $htmlBody, $headers);
echo json_encode(["status" => $sent ? "sent" : "failed", "to" => $to, "subject" => $subject]);
break;
case "read":
// Proxy to WEVIA LIFE IMAP
$ch = curl_init("http://127.0.0.1/api/wevialife-api.php?action=list&limit=" . ($input["limit"] ?? 5));
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10]);
$r = curl_exec($ch); curl_close($ch);
echo $r ?: json_encode(["error" => "IMAP proxy failed"]);
break;
}