php-simple-mail icon indicating copy to clipboard operation
php-simple-mail copied to clipboard

PHP 8.1 - Deprecated: Constant FILTER_SANITIZE_STRING is deprecated

Open MircoBabin opened this issue 7 months ago • 0 comments

Php 8.1 issues a deprecation warning when sending an email. This is because of FILTER_SANITIZE_STRING in filterName().

    // OLD
    public function filterName($name)
    {
        $rule = array(
            "\r" => '',
            "\n" => '',
            "\t" => '',
            '"'  => "'",
            '<'  => '[',
            '>'  => ']',
        );
        $filtered = filter_var(
            $name,
            FILTER_SANITIZE_STRING,
            FILTER_FLAG_NO_ENCODE_QUOTES
        );
        return trim(strtr($filtered, $rule));
    }

Changing this function to below solves this:

    // CHANGED
    public function filterName($name)
    {
        $rule = array(
            "\r" => '',
            "\n" => '',
            "\t" => '',
            '"'  => "'",
            '<'  => '[',
            '>'  => ']',
        );
        $filtered = filter_var(
            $name,
            FILTER_UNSAFE_RAW,
            FILTER_FLAG_STRIP_LOW
        );
        return trim(strtr($filtered, $rule));
    }

P.S. Sorry, I am unable to provide a PR.

MircoBabin avatar Apr 09 '25 14:04 MircoBabin