grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026getLastError();
if (null !== $err) {
LoggerManager::getLogger()->error(
'Unhandled non gmail sent folder hander error: ' . $err,
self::UNHANDLER_ERROR
);
}
}
/**
*
*/
public function clearLastError()
{
if (null !== $this->lastError) {
LoggerManager::getLogger()->fatal(
'Clear an unused Last Error of NonGmailSentFolderHandler: ' . $this->lastError
);
}
$this->lastError = null;
}
/**
*
* @return int
*/
public function getLastError()
{
$ret = $this->lastError;
$this->lastError = null;
return $ret;
}
/**
*
* @param int $err
*/
public function setLastError($err)
{
if (!is_int($err)) {
throw new InvalidArgumentException('Error code should be an integer.', self::ERR_SHOULD_BE_INT);
}
if (null !== $this->lastError) {
LoggerManager::getLogger()->error('Last Error already set for NonGmailSentFolderHandler but never checked: ' . $this->lastError);
}
$this->lastError = $err;
}
/**
*
* @param InboundEmail $ie
* @return bool
* @param SugarPHPMailer $mail
*/
public function storeInSentFolder(InboundEmail $ie, SugarPHPMailer $mail, $options = "\\Seen")
{
$ret = false;
$ok = !$this->lastError;
$ieIdFound = isset($ie->id) && $ie->id;
if ($ok && !$ieIdFound) {
$this->setLastError(self::ERR_NO_IE_FOUND);
$ok = false;
}
$isPop3Protocol = $ie->isPop3Protocol();
if ($ok && $isPop3Protocol) {
$this->setLastError(self::ERR_IS_POP3);
$ok = false;
}
$isGmail = $mail->oe->mail_smtptype == 'gmail';
if ($ok && $isGmail) {
$this->setLastError(self::ERR_IS_GMAIL);
$ok = false;
}
if ($ok) {
$ret = $this->storeInNonGmailSentMailBox($ie, $mail, $options);
} else {
LoggerManager::getLogger()->error($this->getProblemOfStoringInNonGmailSentFolder($ie, $mail));
}
return $ret;
}
/**
*
* @param InboundEmail $ie
* @param SugarPHPMailer $mail
* @return bool
* @throws EmailException
*/
protected function storeInNonGmailSentMailBox(InboundEmail $ie, SugarPHPMailer $mail, $options = "\\Seen")
{
$ret = false;
$this->clearLastError();
$sentFolder = $ie->get_stored_options("sentFolder");
if (!empty($sentFolder)) {
$ret = $this->connectToNonGmailServer($ie, $mail, $sentFolder, $options);
} else {
if (!$ie->mailbox) {
LoggerManager::getLogger()->warn("could not copy email to sent folder, mailbox is not set or empty");
$this->setLastError(self::ERR_EMPTY_MAILBOX);
} else {
LoggerManager::getLogger()->warn("could not copy email to {$ie->mailbox} sent folder as its empty");
$this->setLastError(self::ERR_NO_STORED_SENT_FOLDER);
}
}
return $ret;
}
/**
*
* @param InboundEmail $ie
* @param SugarPHPMailer $mail
* @return string
*/
protected function getProblemOfStoringInNonGmailSentFolder(InboundEmail $ie, SugarPHPMailer $mail)
{
$msg = '';
if (!isset($ie->id)) {
$msg .= 'IE ID is not set.';
} elseif (!$ie->id) {
$msg .= 'IE ID is set but no value.';
}
if ($ie->isPop3Protocol()) {
$msg .= 'It is a pop3 protocoll.';
}
if ($mail->oe->mail_smtptype == 'gmail') {
$msg .= 'It is a gmail.';
}
return $msg;
}
/**
*
* @param InboundEmail $ie
* @param SugarPHPMailer $mail
* @param string $sentFolder
* @return bool
*/
protected function connectToNonGmailServer(InboundEmail $ie, SugarPHPMailer $mail, $sentFolder, $options = "\\Seen")
{
if (!is_string($sentFolder) || !$sentFolder) {
throw new InvalidArgumentException('Sent folder should be a valid folder name string.', self::ERR_SHOULD_BE_STRING);
}
$msg = $ie->connectMailserver();
$ret = $msg == 'true';
if ($ret) {
$ret = $this->copyToNonGmailSentFolder($ie, $mail, $sentFolder, $options);
return $ret;
}
LoggerManager::getLogger()->warn(
"could not connect to mail serve for folder {$ie->mailbox} for {$ie->name} error message: $msg"
);
return false;
}
/**
*
* @param InboundEmail $ie
* @param SugarPHPMailer $mail
* @param string $sentFolder
* @return @return bool TRUE on success or FALSE on failure.
*/
protected function copyToNonGmailSentFolder(InboundEmail $ie, SugarPHPMailer $mail, $sentFolder, $options = "\\Seen")
{
// Call CreateBody() before CreateHeader() as that is where boundary IDs are generated.
$emailbody = $mail->CreateBody();
$emailheader = $mail->CreateHeader();
$data = $emailheader . "\r\n" . $emailbody . "\r\n";
$ie->mailbox = $sentFolder;
$connectString = $ie->getConnectString($ie->getServiceString(), $ie->mailbox);
$returnData = $ie->getImap()->append($connectString, $data, $options);
if (!$returnData) {
$this->setLastError(self::ERR_COULDNT_COPY_TO_SENT);
LoggerManager::getLogger()->warn("could not copy email to {$ie->mailbox} for {$ie->name}");
}
return $returnData;
}
}