You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
final class MailSenderIdentity
|
|
{
|
|
private const FALLBACK_NAME = 'Site';
|
|
private const MAX_DISPLAY_NAME_LENGTH = 128;
|
|
|
|
public static function displayName(): string
|
|
{
|
|
return self::sanitizeDisplayName(SiteName::defaultName());
|
|
}
|
|
|
|
public static function mailbox($configuredSender): string
|
|
{
|
|
$sender = str_replace(["\r", "\n"], '', trim((string) $configuredSender));
|
|
if (preg_match('/<\s*([^<>]+)\s*>\s*$/D', $sender, $matches) === 1) {
|
|
$sender = trim($matches[1]);
|
|
}
|
|
|
|
if (filter_var($sender, FILTER_VALIDATE_EMAIL) === false) {
|
|
throw new InvalidArgumentException('The configured sender must contain a valid email address.');
|
|
}
|
|
|
|
return $sender;
|
|
}
|
|
|
|
public static function header($configuredSender): string
|
|
{
|
|
$displayName = addcslashes(self::displayName(), "\\\"");
|
|
|
|
return '"' . $displayName . '" <' . self::mailbox($configuredSender) . '>';
|
|
}
|
|
|
|
public static function sanitizeDisplayName($name): string
|
|
{
|
|
$name = str_replace(["\r", "\n"], ' ', (string) $name);
|
|
$name = preg_replace('/[\x00-\x1F\x7F]+/', ' ', $name);
|
|
$name = preg_replace('/\s+/', ' ', (string) $name);
|
|
$name = trim((string) $name);
|
|
|
|
if ($name === '') {
|
|
return self::FALLBACK_NAME;
|
|
}
|
|
|
|
if (function_exists('mb_substr')) {
|
|
return (string) mb_substr($name, 0, self::MAX_DISPLAY_NAME_LENGTH, 'UTF-8');
|
|
}
|
|
|
|
return substr($name, 0, self::MAX_DISPLAY_NAME_LENGTH);
|
|
}
|
|
}
|