54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
// Mail configuration sourced from environment variables.
|
|
// Updated to prioritize local .env in workspace root.
|
|
|
|
function env_val(string $key, $default = null) {
|
|
$v = getenv($key);
|
|
return ($v === false || $v === null || $v === '') ? $default : $v;
|
|
}
|
|
|
|
// Loads .env files (either executor/.env or local .env)
|
|
function load_env(): void {
|
|
$paths = [__DIR__ . '/../../.env', __DIR__ . '/../.env']; // executor/.env and workspace/.env
|
|
foreach ($paths as $envPath) {
|
|
$envPath = realpath($envPath);
|
|
if ($envPath && is_readable($envPath)) {
|
|
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
|
|
foreach ($lines as $line) {
|
|
if ($line[0] === '#' || trim($line) === '') continue;
|
|
if (!str_contains($line, '=')) continue;
|
|
[$k, $v] = array_map('trim', explode('=', $line, 2));
|
|
$v = trim($v, "' ");
|
|
// Set env if not set
|
|
if ($k !== '' && (getenv($k) === false || getenv($k) === '')) {
|
|
putenv("{$k}={$v}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
load_env();
|
|
|
|
$transport = env_val('MAIL_TRANSPORT', 'smtp');
|
|
$smtp_host = env_val('SMTP_HOST');
|
|
$smtp_port = (int) env_val('SMTP_PORT', 587);
|
|
$smtp_secure = env_val('SMTP_SECURE', 'tls');
|
|
$smtp_user = env_val('SMTP_USER');
|
|
$smtp_pass = env_val('SMTP_PASS');
|
|
|
|
$from_email = env_val('MAIL_FROM', 'no-reply@localhost');
|
|
$from_name = env_val('MAIL_FROM_NAME', 'App');
|
|
$reply_to = env_val('MAIL_REPLY_TO');
|
|
|
|
return [
|
|
'transport' => $transport,
|
|
'smtp_host' => $smtp_host,
|
|
'smtp_port' => $smtp_port,
|
|
'smtp_secure' => $smtp_secure,
|
|
'smtp_user' => $smtp_user,
|
|
'smtp_pass' => $smtp_pass,
|
|
'from_email' => $from_email,
|
|
'from_name' => $from_name,
|
|
'reply_to' => $reply_to,
|
|
]; |