<?php

class DevMailer {
    public static function init(): void {

        $host = parse_url(home_url(), PHP_URL_HOST);

        $is_local =
            in_array($host, ['localhost', '127.0.0.1', 'wordpress.local'], true) ||
            (function_exists('str_ends_with') &&
                (str_ends_with($host, '.test') || str_ends_with($host, '.local'))
            );

        if (!$is_local) {
            return;
        }

        add_action('phpmailer_init', [self::class, 'configure']);
    }

    public static function configure($phpmailer): void {
        $phpmailer->isSMTP();
        $phpmailer->Host       = '127.0.0.1';
        $phpmailer->Port       = 1025;
        $phpmailer->SMTPAuth   = false;
        $phpmailer->SMTPSecure = false;
        $phpmailer->SMTPAutoTLS = false;
    }
}