Check SMTP SIZE #779

This commit is contained in:
the-djmaze 2022-12-18 13:55:22 +01:00
parent 2793dae547
commit f5beeb9ca1

View file

@ -781,138 +781,126 @@ trait Messages
/*resource*/ &$rMessageStream, int &$iMessageStreamSize, bool $bDsn = false, bool $bAddHiddenRcpt = true) /*resource*/ &$rMessageStream, int &$iMessageStreamSize, bool $bDsn = false, bool $bAddHiddenRcpt = true)
{ {
$oRcpt = $oMessage->GetRcpt(); $oRcpt = $oMessage->GetRcpt();
if ($oRcpt && 0 < $oRcpt->Count()) if (!$oRcpt || !$oRcpt->count()) {
throw new ClientException(Notifications::InvalidRecipients);
}
$this->Plugins()->RunHook('filter.smtp-message-stream',
array($oAccount, &$rMessageStream, &$iMessageStreamSize));
$this->Plugins()->RunHook('filter.message-rcpt', array($oAccount, $oRcpt));
try
{ {
$this->Plugins()->RunHook('filter.smtp-message-stream', $oFrom = $oMessage->GetFrom();
array($oAccount, &$rMessageStream, &$iMessageStreamSize)); $sFrom = $oFrom instanceof \MailSo\Mime\Email ? $oFrom->GetEmail() : '';
$sFrom = empty($sFrom) ? $oAccount->Email() : $sFrom;
$this->Plugins()->RunHook('filter.message-rcpt', array($oAccount, $oRcpt)); $this->Plugins()->RunHook('filter.smtp-from', array($oAccount, $oMessage, &$sFrom));
try $aHiddenRcpt = array();
if ($bAddHiddenRcpt)
{ {
$oFrom = $oMessage->GetFrom(); $this->Plugins()->RunHook('filter.smtp-hidden-rcpt', array($oAccount, $oMessage, &$aHiddenRcpt));
$sFrom = $oFrom instanceof \MailSo\Mime\Email ? $oFrom->GetEmail() : ''; }
$sFrom = empty($sFrom) ? $oAccount->Email() : $sFrom;
$this->Plugins()->RunHook('filter.smtp-from', array($oAccount, $oMessage, &$sFrom)); $oSmtpClient = new \MailSo\Smtp\SmtpClient();
$oSmtpClient->SetLogger($this->Logger());
$aHiddenRcpt = array(); $bUsePhpMail = false;
if ($bAddHiddenRcpt) $oAccount->SmtpConnectAndLoginHelper($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);
if ($bUsePhpMail)
{
if (\MailSo\Base\Utils::FunctionCallable('mail'))
{ {
$this->Plugins()->RunHook('filter.smtp-hidden-rcpt', array($oAccount, $oMessage, &$aHiddenRcpt)); $aToCollection = $oMessage->GetTo();
} if ($aToCollection && $oFrom)
$oSmtpClient = new \MailSo\Smtp\SmtpClient();
$oSmtpClient->SetLogger($this->Logger());
$bUsePhpMail = false;
$oAccount->SmtpConnectAndLoginHelper($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);
if ($bUsePhpMail)
{
if (\MailSo\Base\Utils::FunctionCallable('mail'))
{ {
$aToCollection = $oMessage->GetTo(); $sRawBody = \stream_get_contents($rMessageStream);
if ($aToCollection && $oFrom) if (!empty($sRawBody))
{ {
$sRawBody = \stream_get_contents($rMessageStream); $sMailTo = \trim($aToCollection->ToString(true));
if (!empty($sRawBody)) $sMailSubject = \trim($oMessage->GetSubject());
$sMailSubject = 0 === \strlen($sMailSubject) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue(
\MailSo\Base\Enumerations\Encoding::BASE64_SHORT, $sMailSubject);
$sMailHeaders = $sMailBody = '';
list($sMailHeaders, $sMailBody) = \explode("\r\n\r\n", $sRawBody, 2);
unset($sRawBody);
if ($this->Config()->Get('labs', 'mail_func_clear_headers', true))
{ {
$sMailTo = \trim($aToCollection->ToString(true)); $sMailHeaders = \MailSo\Base\Utils::RemoveHeaderFromHeaders($sMailHeaders, array(
$sMailSubject = \trim($oMessage->GetSubject()); MimeEnumHeader::TO_,
$sMailSubject = 0 === \strlen($sMailSubject) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue( MimeEnumHeader::SUBJECT
\MailSo\Base\Enumerations\Encoding::BASE64_SHORT, $sMailSubject); ));
}
$sMailHeaders = $sMailBody = ''; $this->Logger()->WriteDump(array(
list($sMailHeaders, $sMailBody) = \explode("\r\n\r\n", $sRawBody, 2); $sMailTo, $sMailSubject, $sMailBody, $sMailHeaders
unset($sRawBody); ), \LOG_DEBUG);
if ($this->Config()->Get('labs', 'mail_func_clear_headers', true)) $bR = $this->Config()->Get('labs', 'mail_func_additional_parameters', false) ?
{ \mail($sMailTo, $sMailSubject, $sMailBody, $sMailHeaders, '-f'.$oFrom->GetEmail()) :
$sMailHeaders = \MailSo\Base\Utils::RemoveHeaderFromHeaders($sMailHeaders, array( \mail($sMailTo, $sMailSubject, $sMailBody, $sMailHeaders);
MimeEnumHeader::TO_,
MimeEnumHeader::SUBJECT
));
}
$this->Logger()->WriteDump(array( if (!$bR)
$sMailTo, $sMailSubject, $sMailBody, $sMailHeaders {
), \LOG_DEBUG); throw new ClientException(Notifications::CantSendMessage);
$bR = $this->Config()->Get('labs', 'mail_func_additional_parameters', false) ?
\mail($sMailTo, $sMailSubject, $sMailBody, $sMailHeaders, '-f'.$oFrom->GetEmail()) :
\mail($sMailTo, $sMailSubject, $sMailBody, $sMailHeaders);
if (!$bR)
{
throw new ClientException(Notifications::CantSendMessage);
}
} }
} }
} }
else
{
throw new ClientException(Notifications::CantSendMessage);
}
} }
else if ($oSmtpClient->IsConnected()) else
{ {
if (!empty($sFrom)) throw new ClientException(Notifications::CantSendMessage);
{ }
$oSmtpClient->MailFrom($sFrom, '', $bDsn); }
} else if ($oSmtpClient->IsConnected())
{
if ($iMessageStreamSize && $oSmtpClient->maxSize() && $iMessageStreamSize * 1.33 > $oSmtpClient->maxSize()) {
throw new ClientException(Notifications::ClientViewError, 'Message size '. ($iMessageStreamSize * 1.33) . ' bigger then max ' . $oSmtpClient->maxSize());
}
foreach ($oRcpt as /* @var $oEmail \MailSo\Mime\Email */ $oEmail) if (!empty($sFrom)) {
{ $oSmtpClient->MailFrom($sFrom, '', $bDsn);
$oSmtpClient->Rcpt($oEmail->GetEmail(), $bDsn); }
}
if ($bAddHiddenRcpt && \is_array($aHiddenRcpt) && \count($aHiddenRcpt)) foreach ($oRcpt as /* @var $oEmail \MailSo\Mime\Email */ $oEmail) {
{ $oSmtpClient->Rcpt($oEmail->GetEmail(), $bDsn);
foreach ($aHiddenRcpt as $sEmail) }
{
if (\preg_match('/^[^@\s]+@[^@\s]+$/', $sEmail)) if ($bAddHiddenRcpt && \is_array($aHiddenRcpt) && \count($aHiddenRcpt)) {
{ foreach ($aHiddenRcpt as $sEmail) {
$oSmtpClient->Rcpt($sEmail); if (\preg_match('/^[^@\s]+@[^@\s]+$/', $sEmail)) {
} $oSmtpClient->Rcpt($sEmail);
} }
} }
}
$oSmtpClient->DataWithStream($rMessageStream); $oSmtpClient->DataWithStream($rMessageStream);
$oSmtpClient->Disconnect(); $oSmtpClient->Disconnect();
}
}
catch (\MailSo\Net\Exceptions\ConnectionException $oException)
{
if ($this->Config()->Get('labs', 'smtp_show_server_errors'))
{
throw new ClientException(Notifications::ClientViewError, $oException);
}
else
{
throw new ClientException(Notifications::ConnectionError, $oException);
}
}
catch (\MailSo\Smtp\Exceptions\LoginException $oException)
{
throw new ClientException(Notifications::AuthError, $oException);
}
catch (\Throwable $oException)
{
if ($this->Config()->Get('labs', 'smtp_show_server_errors'))
{
throw new ClientException(Notifications::ClientViewError, $oException);
}
else
{
throw $oException;
}
} }
} }
else catch (\MailSo\Net\Exceptions\ConnectionException $oException)
{ {
throw new ClientException(Notifications::InvalidRecipients); if ($this->Config()->Get('labs', 'smtp_show_server_errors')) {
throw new ClientException(Notifications::ClientViewError, $oException);
}
throw new ClientException(Notifications::ConnectionError, $oException);
}
catch (\MailSo\Smtp\Exceptions\LoginException $oException)
{
throw new ClientException(Notifications::AuthError, $oException);
}
catch (\Throwable $oException)
{
if ($this->Config()->Get('labs', 'smtp_show_server_errors')) {
throw new ClientException(Notifications::ClientViewError, $oException);
}
throw $oException;
} }
} }
@ -981,8 +969,7 @@ trait Messages
$oIdentity = $this->GetIdentityByID($oAccount, '', true); $oIdentity = $this->GetIdentityByID($oAccount, '', true);
if (empty($sReadReceipt) || empty($sSubject) || empty($sText) || !$oIdentity) if (empty($sReadReceipt) || empty($sSubject) || empty($sText) || !$oIdentity) {
{
throw new ClientException(Notifications::UnknownError); throw new ClientException(Notifications::UnknownError);
} }
@ -1000,11 +987,9 @@ trait Messages
$oMessage->RegenerateMessageId($oFrom ? $oFrom->GetDomain() : ''); $oMessage->RegenerateMessageId($oFrom ? $oFrom->GetDomain() : '');
$sReplyTo = $oIdentity->ReplyTo(); $sReplyTo = $oIdentity->ReplyTo();
if (!empty($sReplyTo)) if (!empty($sReplyTo)) {
{
$oReplyTo = new \MailSo\Mime\EmailCollection($sReplyTo); $oReplyTo = new \MailSo\Mime\EmailCollection($sReplyTo);
if ($oReplyTo && $oReplyTo->Count()) if ($oReplyTo && $oReplyTo->count()) {
{
$oMessage->SetReplyTo($oReplyTo); $oMessage->SetReplyTo($oReplyTo);
} }
} }
@ -1012,8 +997,7 @@ trait Messages
$oMessage->SetSubject($sSubject); $oMessage->SetSubject($sSubject);
$oToEmails = new \MailSo\Mime\EmailCollection($sReadReceipt); $oToEmails = new \MailSo\Mime\EmailCollection($sReadReceipt);
if ($oToEmails && $oToEmails->Count()) if ($oToEmails && $oToEmails->count()) {
{
$oMessage->SetTo($oToEmails); $oMessage->SetTo($oToEmails);
} }