Skip to content

Commit

Permalink
Check SMTP SIZE #779
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Dec 18, 2022
1 parent 2793dae commit f5beeb9
Showing 1 changed file with 94 additions and 110 deletions.
204 changes: 94 additions & 110 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,138 +781,126 @@ private function smtpSendMessage(Account $oAccount, \MailSo\Mime\Message $oMessa
/*resource*/ &$rMessageStream, int &$iMessageStreamSize, bool $bDsn = false, bool $bAddHiddenRcpt = true)
{
$oRcpt = $oMessage->GetRcpt();
if ($oRcpt && 0 < $oRcpt->Count())
{
$this->Plugins()->RunHook('filter.smtp-message-stream',
array($oAccount, &$rMessageStream, &$iMessageStreamSize));
if (!$oRcpt || !$oRcpt->count()) {
throw new ClientException(Notifications::InvalidRecipients);
}

$this->Plugins()->RunHook('filter.message-rcpt', array($oAccount, $oRcpt));
$this->Plugins()->RunHook('filter.smtp-message-stream',
array($oAccount, &$rMessageStream, &$iMessageStreamSize));

try
{
$oFrom = $oMessage->GetFrom();
$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
{
$oFrom = $oMessage->GetFrom();
$sFrom = $oFrom instanceof \MailSo\Mime\Email ? $oFrom->GetEmail() : '';
$sFrom = empty($sFrom) ? $oAccount->Email() : $sFrom;

$aHiddenRcpt = array();
if ($bAddHiddenRcpt)
{
$this->Plugins()->RunHook('filter.smtp-hidden-rcpt', array($oAccount, $oMessage, &$aHiddenRcpt));
}
$this->Plugins()->RunHook('filter.smtp-from', array($oAccount, $oMessage, &$sFrom));

$oSmtpClient = new \MailSo\Smtp\SmtpClient();
$oSmtpClient->SetLogger($this->Logger());
$aHiddenRcpt = array();
if ($bAddHiddenRcpt)
{
$this->Plugins()->RunHook('filter.smtp-hidden-rcpt', array($oAccount, $oMessage, &$aHiddenRcpt));
}

$oSmtpClient = new \MailSo\Smtp\SmtpClient();
$oSmtpClient->SetLogger($this->Logger());

$bUsePhpMail = false;
$oAccount->SmtpConnectAndLoginHelper($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);
$bUsePhpMail = false;
$oAccount->SmtpConnectAndLoginHelper($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);

if ($bUsePhpMail)
if ($bUsePhpMail)
{
if (\MailSo\Base\Utils::FunctionCallable('mail'))
{
if (\MailSo\Base\Utils::FunctionCallable('mail'))
$aToCollection = $oMessage->GetTo();
if ($aToCollection && $oFrom)
{
$aToCollection = $oMessage->GetTo();
if ($aToCollection && $oFrom)
$sRawBody = \stream_get_contents($rMessageStream);
if (!empty($sRawBody))
{
$sRawBody = \stream_get_contents($rMessageStream);
if (!empty($sRawBody))
{
$sMailTo = \trim($aToCollection->ToString(true));
$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))
{
$sMailHeaders = \MailSo\Base\Utils::RemoveHeaderFromHeaders($sMailHeaders, array(
MimeEnumHeader::TO_,
MimeEnumHeader::SUBJECT
));
}
$sMailTo = \trim($aToCollection->ToString(true));
$sMailSubject = \trim($oMessage->GetSubject());
$sMailSubject = 0 === \strlen($sMailSubject) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue(
\MailSo\Base\Enumerations\Encoding::BASE64_SHORT, $sMailSubject);

$this->Logger()->WriteDump(array(
$sMailTo, $sMailSubject, $sMailBody, $sMailHeaders
), \LOG_DEBUG);
$sMailHeaders = $sMailBody = '';
list($sMailHeaders, $sMailBody) = \explode("\r\n\r\n", $sRawBody, 2);
unset($sRawBody);

$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);
}
if ($this->Config()->Get('labs', 'mail_func_clear_headers', true))
{
$sMailHeaders = \MailSo\Base\Utils::RemoveHeaderFromHeaders($sMailHeaders, array(
MimeEnumHeader::TO_,
MimeEnumHeader::SUBJECT
));
}
}
}
else
{
throw new ClientException(Notifications::CantSendMessage);
}
}
else if ($oSmtpClient->IsConnected())
{
if (!empty($sFrom))
{
$oSmtpClient->MailFrom($sFrom, '', $bDsn);
}

foreach ($oRcpt as /* @var $oEmail \MailSo\Mime\Email */ $oEmail)
{
$oSmtpClient->Rcpt($oEmail->GetEmail(), $bDsn);
}
$this->Logger()->WriteDump(array(
$sMailTo, $sMailSubject, $sMailBody, $sMailHeaders
), \LOG_DEBUG);

if ($bAddHiddenRcpt && \is_array($aHiddenRcpt) && \count($aHiddenRcpt))
{
foreach ($aHiddenRcpt as $sEmail)
{
if (\preg_match('/^[^@\s]+@[^@\s]+$/', $sEmail))
$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)
{
$oSmtpClient->Rcpt($sEmail);
throw new ClientException(Notifications::CantSendMessage);
}
}
}

$oSmtpClient->DataWithStream($rMessageStream);

$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);
throw new ClientException(Notifications::CantSendMessage);
}
}
catch (\MailSo\Smtp\Exceptions\LoginException $oException)
{
throw new ClientException(Notifications::AuthError, $oException);
}
catch (\Throwable $oException)
else if ($oSmtpClient->IsConnected())
{
if ($this->Config()->Get('labs', 'smtp_show_server_errors'))
{
throw new ClientException(Notifications::ClientViewError, $oException);
if ($iMessageStreamSize && $oSmtpClient->maxSize() && $iMessageStreamSize * 1.33 > $oSmtpClient->maxSize()) {
throw new ClientException(Notifications::ClientViewError, 'Message size '. ($iMessageStreamSize * 1.33) . ' bigger then max ' . $oSmtpClient->maxSize());
}
else
{
throw $oException;

if (!empty($sFrom)) {
$oSmtpClient->MailFrom($sFrom, '', $bDsn);
}

foreach ($oRcpt as /* @var $oEmail \MailSo\Mime\Email */ $oEmail) {
$oSmtpClient->Rcpt($oEmail->GetEmail(), $bDsn);
}

if ($bAddHiddenRcpt && \is_array($aHiddenRcpt) && \count($aHiddenRcpt)) {
foreach ($aHiddenRcpt as $sEmail) {
if (\preg_match('/^[^@\s]+@[^@\s]+$/', $sEmail)) {
$oSmtpClient->Rcpt($sEmail);
}
}
}

$oSmtpClient->DataWithStream($rMessageStream);

$oSmtpClient->Disconnect();
}
}
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;
}
}

Expand Down Expand Up @@ -981,8 +969,7 @@ private function buildReadReceiptMessage(Account $oAccount) : \MailSo\Mime\Messa

$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);
}

Expand All @@ -1000,20 +987,17 @@ private function buildReadReceiptMessage(Account $oAccount) : \MailSo\Mime\Messa
$oMessage->RegenerateMessageId($oFrom ? $oFrom->GetDomain() : '');

$sReplyTo = $oIdentity->ReplyTo();
if (!empty($sReplyTo))
{
if (!empty($sReplyTo)) {
$oReplyTo = new \MailSo\Mime\EmailCollection($sReplyTo);
if ($oReplyTo && $oReplyTo->Count())
{
if ($oReplyTo && $oReplyTo->count()) {
$oMessage->SetReplyTo($oReplyTo);
}
}

$oMessage->SetSubject($sSubject);

$oToEmails = new \MailSo\Mime\EmailCollection($sReadReceipt);
if ($oToEmails && $oToEmails->Count())
{
if ($oToEmails && $oToEmails->count()) {
$oMessage->SetTo($oToEmails);
}

Expand Down

0 comments on commit f5beeb9

Please sign in to comment.