HEX
Server: nginx/1.24.0
System: Linux 0c1023d6c65e 6.8.0-137-generic #137-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 17 20:28:23 UTC 2026 x86_64
User: root (0)
PHP: 8.3.33
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/mu-plugins/ethnic-smtp-mailer.php
<?php
/**
 * Plugin Name: Ethnic SMTP Mailer
 * Description: Optional SMTP transport driven by environment variables for transactional mail.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Read a mailer environment variable.
 *
 * @param string $key Environment key.
 * @param string $default Default value.
 * @return string
 */
function ethnic_smtp_mailer_env( $key, $default = '' ) {
	$value = getenv( $key );

	if ( false === $value || null === $value || '' === $value ) {
		return $default;
	}

	return trim( (string) $value );
}

/**
 * Return whether SMTP configuration is available.
 *
 * @return bool
 */
function ethnic_smtp_mailer_is_configured() {
	return '' !== ethnic_smtp_mailer_env( 'ETHNIC_SMTP_HOST' );
}

/**
 * Normalize boolean-like environment values.
 *
 * @param string $value Raw value.
 * @param bool   $default Default fallback.
 * @return bool
 */
function ethnic_smtp_mailer_bool( $value, $default ) {
	if ( '' === $value ) {
		return $default;
	}

	return in_array( strtolower( $value ), array( '1', 'true', 'yes', 'on' ), true );
}

/**
 * Configure PHPMailer to use SMTP when environment variables are present.
 *
 * @param PHPMailer\PHPMailer\PHPMailer $phpmailer Mailer instance.
 * @return void
 */
function ethnic_smtp_mailer_configure( $phpmailer ) {
	if ( ! ethnic_smtp_mailer_is_configured() ) {
		return;
	}

	$host       = ethnic_smtp_mailer_env( 'ETHNIC_SMTP_HOST' );
	$port       = (int) ethnic_smtp_mailer_env( 'ETHNIC_SMTP_PORT', '587' );
	$username   = ethnic_smtp_mailer_env( 'ETHNIC_SMTP_USER' );
	$password   = ethnic_smtp_mailer_env( 'ETHNIC_SMTP_PASS' );
	$secure     = strtolower( ethnic_smtp_mailer_env( 'ETHNIC_SMTP_SECURE', 'tls' ) );
	$from_email = ethnic_smtp_mailer_env( 'ETHNIC_SMTP_FROM_EMAIL' );
	$from_name  = ethnic_smtp_mailer_env( 'ETHNIC_SMTP_FROM_NAME', get_bloginfo( 'name' ) );
	$auth       = ethnic_smtp_mailer_bool( ethnic_smtp_mailer_env( 'ETHNIC_SMTP_AUTH' ), ( '' !== $username || '' !== $password ) );
	$auto_tls   = ethnic_smtp_mailer_bool( ethnic_smtp_mailer_env( 'ETHNIC_SMTP_AUTO_TLS' ), true );

	$phpmailer->isSMTP();
	$phpmailer->Host       = $host;
	$phpmailer->Port       = $port > 0 ? $port : 587;
	$phpmailer->SMTPAuth   = $auth;
	$phpmailer->SMTPAutoTLS = $auto_tls;
	$phpmailer->Timeout    = 20;

	if ( in_array( $secure, array( 'ssl', 'tls' ), true ) ) {
		$phpmailer->SMTPSecure = $secure;
	}

	if ( $auth ) {
		$phpmailer->Username = $username;
		$phpmailer->Password = $password;
	}

	if ( $from_email ) {
		$phpmailer->From   = $from_email;
		$phpmailer->Sender = $from_email;
	}

	if ( $from_name ) {
		$phpmailer->FromName = $from_name;
	}
}
add_action( 'phpmailer_init', 'ethnic_smtp_mailer_configure', 20 );

/**
 * Override WordPress mail from email when SMTP config provides one.
 *
 * @param string $from_email Current from email.
 * @return string
 */
function ethnic_smtp_mailer_from_email( $from_email ) {
	if ( ! ethnic_smtp_mailer_is_configured() ) {
		return $from_email;
	}

	return ethnic_smtp_mailer_env( 'ETHNIC_SMTP_FROM_EMAIL', $from_email );
}
add_filter( 'wp_mail_from', 'ethnic_smtp_mailer_from_email', 20 );

/**
 * Override WordPress mail from name when SMTP config provides one.
 *
 * @param string $from_name Current from name.
 * @return string
 */
function ethnic_smtp_mailer_from_name( $from_name ) {
	if ( ! ethnic_smtp_mailer_is_configured() ) {
		return $from_name;
	}

	return ethnic_smtp_mailer_env( 'ETHNIC_SMTP_FROM_NAME', $from_name );
}
add_filter( 'wp_mail_from_name', 'ethnic_smtp_mailer_from_name', 20 );