<?php

if (!function_exists('get_env_var')) {
    function get_env_var($key, $default = null) {
        if (function_exists('mail_get_env_var')) {
            return mail_get_env_var($key, $default);
        }
        
        if (isset($_ENV[$key])) return $_ENV[$key];
        $val = getenv($key);
        return $val === false ? $default : $val;
    }
}

if (!function_exists('mail_get_env_var')) {

    function mail_get_env_var($key, $default = null) {

        if (isset($_ENV[$key])) return $_ENV[$key];

        $val = getenv($key);

        return $val === false ? $default : $val;

    }

}



if (!function_exists('mail_load_dot_env')) {

    function mail_load_dot_env($path) {

        if (!file_exists($path) || !is_readable($path)) return;

        $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        foreach ($lines as $line) {

            if (strpos(trim($line), '#') === 0) continue;

            list($key, $val) = explode('=', $line, 2) + [null, null];

            if ($key === null) continue;

            $key = trim($key);

            $val = trim($val);

            if (preg_match('/^"(.*)"$/', $val, $matches)) $val = $matches[1];

            elseif (preg_match("/'(.*)'/ ", $val, $matches)) $val = $matches[1];

            if (!array_key_exists($key, $_SERVER) && !array_key_exists($key, $_ENV)) {

                putenv("$key=$val");

                $_ENV[$key] = $val;

            }

        }

    }

}



$dotenv_path = __DIR__ . '/../.env';

if (file_exists($dotenv_path)) {

    mail_load_dot_env($dotenv_path);

}



return [

    'transport'     => mail_get_env_var('MAIL_TRANSPORT', 'smtp'),

    'smtp_host'     => mail_get_env_var('SMTP_HOST'),

    'smtp_port'     => mail_get_env_var('SMTP_PORT', 587),

    'smtp_secure'   => mail_get_env_var('SMTP_SECURE', 'tls'), // tls or ssl

    'smtp_user'     => mail_get_env_var('SMTP_USER'),

    'smtp_pass'     => mail_get_env_var('SMTP_PASS'),

    'from_email'    => mail_get_env_var('MAIL_FROM'),

    'from_name'     => mail_get_env_var('MAIL_FROM_NAME'),

    'reply_to'      => mail_get_env_var('MAIL_REPLY_TO'),



    // Optional DKIM signing

    'dkim_domain'   => mail_get_env_var('DKIM_DOMAIN', ''),

    'dkim_selector' => mail_get_env_var('DKIM_SELECTOR', 'default'),

    'dkim_private_key_path' => mail_get_env_var('DKIM_PRIVATE_KEY_PATH', ''), // Path to the private key file

];
