* Mail: make sure to create a valid Message-ID header, even if SERVER_NAME is set to "_"

happens e.g. under Univention
This commit is contained in:
ralf 2024-06-29 17:51:21 +02:00
parent 7ccd1e4cf2
commit 0c6e9ac77f

View File

@ -517,8 +517,7 @@ class Mailer extends Horde_Mime_Mail
if (!($message_id = $this->getHeader('Message-ID')) && if (!($message_id = $this->getHeader('Message-ID')) &&
class_exists('Horde_Mime_Headers_MessageId')) // since 2.5.0 class_exists('Horde_Mime_Headers_MessageId')) // since 2.5.0
{ {
$message_id = Horde_Mime_Headers_MessageId::create('EGroupware'); self::checkSetRequiredHeaders($this->_headers);
$this->addHeader('Message-ID', $message_id);
} }
$body_sha1 = null; // skip sha1, it requires whole mail in memory, which we traing to avoid now $body_sha1 = null; // skip sha1, it requires whole mail in memory, which we traing to avoid now
@ -643,6 +642,33 @@ class Mailer extends Horde_Mime_Mail
if (isset($e)) throw $e; if (isset($e)) throw $e;
} }
public static function checkSetRequiredHeaders(\Horde_Mime_Headers $headers)
{
if (!isset($headers['Message-ID']))
{
// UCS e.g. has $_SERVER['SERVER_NAME'] === '_', which is used by Horde_Mime_Headers_MessageID::create()
if (!preg_match($domain_regexp='/^[a-z0-9][a-z0-9_-]+(\.[a-z0-9][a-z0-9_-]+)+$/i', $server = $_SERVER['SERVER_NAME']) &&
!preg_match($domain_regexp, $server = $_SERVER['HTTP_HOST']) &&
!preg_match($domain_regexp, $server = gethostname().'.user.egroupware.org'))
{
$server = 'user.egroupware.org';
}
$headers->addHeaderOb(
new Horde_Mime_Headers_MessageId('Message-ID', '<'.(string)(new \Horde_Support_Guid([
'prefix' => 'EGroupware',
'server' => $server,
])).'>'));
}
if (!isset($headers['User-Agent']))
{
$headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['api']);
}
if (!isset($headers['Date']))
{
$headers->addHeaderOb(Horde_Mime_Headers_Date::create());
}
}
/** /**
* Sends this message. * Sends this message.
* *
@ -668,19 +694,7 @@ class Mailer extends Horde_Mime_Mail
public function _send($mailer, $resend = false, $flowed = true, array $opts = array()) public function _send($mailer, $resend = false, $flowed = true, array $opts = array())
{ {
/* Add mandatory headers if missing. */ /* Add mandatory headers if missing. */
if (!$resend || !isset($this->_headers['Message-ID'])) { self::checkSetRequiredHeaders($this->_headers);
$this->_headers->addHeaderOb(
Horde_Mime_Headers_MessageId::create()
);
}
if (!isset($this->_headers['User-Agent'])) {
$this->_headers->addHeaderOb(
Horde_Mime_Headers_UserAgent::create()
);
}
if (!$resend || !isset($this->_headers['Date'])) {
$this->_headers->addHeaderOb(Horde_Mime_Headers_Date::create());
}
if (isset($this->_base)) { if (isset($this->_base)) {
$basepart = $this->_base; $basepart = $this->_base;
@ -784,6 +798,7 @@ class Mailer extends Horde_Mime_Mail
catch(Horde_Mail_Exception $e) catch(Horde_Mail_Exception $e)
{ {
unset($e); unset($e);
self::checkSetRequiredHeaders($this->_headers);
parent::send(new Horde_Mail_Transport_Null(), true); // true: keep Message-ID parent::send(new Horde_Mail_Transport_Null(), true); // true: keep Message-ID
} }
// code copied from Horde_Mime_Mail::getRaw(), as there is no way to inject charset in // code copied from Horde_Mime_Mail::getRaw(), as there is no way to inject charset in