strtr($addr, ['@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ']), 'hex' => Conversion::toHtml($addr, true), default => $addr, }; } // endregion // region outgoing-mail helpers /** * Check if a given mail address is valid. * * @param string $email the address to check * @return bool true if address is valid */ public static function isValid(string $email): bool { return \EmailAddressValidator::checkEmailAddress($email, true); } /** * RFC 2045 quoted-printable encoding. * * @param string $sText * @param int $maxlen * @param bool $bEmulate_imap_8bit * @return string * @author umu * @link http://php.net/manual/en/function.imap-8bit.php#61216 * */ public static function quotedPrintableEncode( string $sText, int $maxlen = 74, bool $bEmulate_imap_8bit = true ): string { // split text into lines $aLines = preg_split("/(?:\r\n|\r|\n)/", $sText); $cnt = count($aLines); for ($i = 0; $i < $cnt; $i++) { $sLine =& $aLines[$i]; if ($sLine === '') continue; // do nothing, if empty $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; // imap_8bit encodes x09 everywhere, not only at lineends, // for EBCDIC safeness encode !"#$@[\]^`{|}~, // for complete safeness encode every character :) if ($bEmulate_imap_8bit) $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/'; $sLine = preg_replace_callback( $sRegExp, static fn(array $matches): string => sprintf("=%02X", ord($matches[0])), $sLine ); // encode x09,x20 at lineends $iLength = strlen($sLine); $iLastChar = ord($sLine[$iLength - 1]); // imap_8_bit does not encode x20 at the very end of a text, // here is, where I don't agree with imap_8_bit, // please correct me, if I'm wrong, // or comment next line for RFC2045 conformance, if you like if (!($bEmulate_imap_8bit && ($i == count($aLines) - 1))) { if (($iLastChar == 0x09) || ($iLastChar == 0x20)) { $sLine[$iLength - 1] = '='; $sLine .= ($iLastChar == 0x09) ? '09' : '20'; } } // imap_8bit encodes x20 before chr(13), too // although IMHO not requested by RFC2045, why not do it safer :) // and why not encode any x20 around chr(10) or chr(13) if ($bEmulate_imap_8bit) { $sLine = str_replace(' =0D', '=20=0D', $sLine); //$sLine=str_replace(' =0A','=20=0A',$sLine); //$sLine=str_replace('=0D ','=0D=20',$sLine); //$sLine=str_replace('=0A ','=0A=20',$sLine); } // finally split into softlines no longer than $maxlen chars, // for even more safeness one could encode x09,x20 // at the very first character of the line // and after soft linebreaks, as well, // but this wouldn't be caught by such an easy RegExp if ($maxlen) { preg_match_all('/.{1,' . ($maxlen - 2) . '}([^=]{0,2})?/', $sLine, $aMatch); $sLine = implode('=' . MAILHEADER_EOL, $aMatch[0]); // add soft crlf's } } // join lines into text return implode(MAILHEADER_EOL, $aLines); } // endregion }