<?php
function getEnvEmails()
{
    $envPath = __DIR__ . '/.env';
    if (!file_exists($envPath)) return [];

    $lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $emails = [];

    foreach ($lines as $line) {
        if (strpos($line, '#') === 0 || strpos($line, '=') === false) continue;
        list($key, $value) = explode('=', $line, 2);
        $key = trim($key);
        $value = trim($value, '"');

        if (preg_match('/^(.*)_EMAIL$/', $key, $matches)) {
            $prefix = $matches[1];
            $passKey = $prefix . '_PASS';
            $pass = null;

            foreach ($lines as $line2) {
                if (strpos($line2, $passKey . '=') === 0) {
                    $pass = trim(explode('=', $line2, 2)[1], '"');
                    break;
                }
            }

            if ($pass) $emails[$value] = $pass;
        }
    }

    return $emails;
}
