Skip to content

EspoCRM – Open Source CRM Application

Reset email account fetch data

Yuriif0e9f42ec9fd
diff --git a/application/Espo/Classes/Jobs/CheckEmailAccounts.php b/application/Espo/Classes/Jobs/CheckEmailAccounts.php
index 4b1c751f65..c80f16ecbd 100644
--- a/application/Espo/Classes/Jobs/CheckEmailAccounts.php
+++ b/application/Espo/Classes/Jobs/CheckEmailAccounts.php
@@ -52,7 +52,7 @@ class CheckEmailAccounts implements Job
try {
$this->service->fetch($targetId);
} catch (Throwable $e) {
- throw new RuntimeException("CheckEmailAccounts job failed, $targetId; {$e->getMessage()}", 0, $e);
+ throw new RuntimeException("CheckEmailAccounts job failed, $targetId.", 0, $e);
}
}
}
diff --git a/application/Espo/Classes/Jobs/CheckInboundEmails.php b/application/Espo/Classes/Jobs/CheckInboundEmails.php
index 616818bb8e..711ef01bf8 100644
--- a/application/Espo/Classes/Jobs/CheckInboundEmails.php
+++ b/application/Espo/Classes/Jobs/CheckInboundEmails.php
@@ -52,7 +52,7 @@ class CheckInboundEmails implements Job
try {
$this->service->fetch($targetId);
} catch (Throwable $e) {
- throw new RuntimeException("CheckInboundEmails job failed, $targetId; {$e->getMessage()}", 0, $e);
+ throw new RuntimeException("CheckInboundEmails job failed, $targetId.", 0, $e);
}
}
}
diff --git a/application/Espo/Core/Mail/Account/FetchData.php b/application/Espo/Core/Mail/Account/FetchData.php
index 2137e00f21..f4460b89de 100644
--- a/application/Espo/Core/Mail/Account/FetchData.php
+++ b/application/Espo/Core/Mail/Account/FetchData.php
@@ -42,14 +42,16 @@ class FetchData
/**
* @internal
*/
- public function __construct(?stdClass $data = null)
- {
+ public function __construct(
+ ?stdClass $data = null,
+ private int $validityNumber = 0,
+ ) {
$this->data = ObjectUtil::clone($data ?? (object) []);
}
- public static function fromRaw(stdClass $data): self
+ public static function fromRaw(stdClass $data, int $fetchNumber): self
{
- return new self($data);
+ return new self($data, $fetchNumber);
}
public function getRaw(): stdClass
@@ -146,4 +148,12 @@ class FetchData
$this->data->byDate->$folder = $forceByDate;
}
+
+ /**
+ * @since 10.0.0
+ */
+ public function getValidityNumber(): int
+ {
+ return $this->validityNumber;
+ }
}
diff --git a/application/Espo/Core/Mail/Account/GroupAccount/Account.php b/application/Espo/Core/Mail/Account/GroupAccount/Account.php
index b29574aced..ab35cbcac6 100644
--- a/application/Espo/Core/Mail/Account/GroupAccount/Account.php
+++ b/application/Espo/Core/Mail/Account/GroupAccount/Account.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Mail\Account\GroupAccount;
+use Espo\Core\Exceptions\Error;
use Espo\Core\Field\Date;
use Espo\Core\Field\DateTime;
use Espo\Core\Field\Link;
@@ -39,6 +40,7 @@ use Espo\Core\Mail\Smtp\HandlerProcessor;
use Espo\Core\Mail\SmtpParams;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Repository\Option\SaveOption;
+use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Crypt;
use Espo\Entities\GroupEmailFolder;
@@ -49,7 +51,9 @@ use Espo\ORM\EntityManager;
use Espo\Core\Mail\Account\Account as AccountInterface;
use Espo\Core\Mail\Account\FetchData;
+use Espo\ORM\Mapper\Util;
use Espo\ORM\Name\Attribute;
+use Espo\ORM\Query\UpdateBuilder;
use RuntimeException;
class Account implements AccountInterface
@@ -70,9 +74,24 @@ class Account implements AccountInterface
public function updateFetchData(FetchData $fetchData): void
{
- $this->entity->set('fetchData', $fetchData->getRaw());
+ $query = UpdateBuilder::create()
+ ->in(InboundEmail::ENTITY_TYPE)
+ ->where([
+ InboundEmail::FIELD_FETCH_VALIDITY_NUMBER => $fetchData->getValidityNumber(),
+ Attribute::ID => $this->entity->getId(),
+ Attribute::DELETED => false,
+ ])
+ ->set([
+ InboundEmail::FIELD_FETCH_DATA => Util::prepareValue(FieldType::JSON_OBJECT, $fetchData->getRaw()),
+ ])
+ ->build();
- $this->entityManager->saveEntity($this->entity, [SaveOption::SILENT => true]);
+ $sth = $this->entityManager->getQueryExecutor()->execute($query);
+
+ if ($sth->rowCount() !== 0) {
+ $this->entity->setFetchData($fetchData);
+ $this->entity->setAsFetched();
+ }
}
public function updateConnectedAt(): void
@@ -226,7 +245,8 @@ class Account implements AccountInterface
public function getFetchData(): FetchData
{
return FetchData::fromRaw(
- $this->entity->getFetchData()
+ $this->entity->getFetchData(),
+ $this->entity->getFetchValidityNumber(),
);
}
diff --git a/application/Espo/Core/Mail/Account/PersonalAccount/Account.php b/application/Espo/Core/Mail/Account/PersonalAccount/Account.php
index 5de2268441..5acdd14bf8 100644
--- a/application/Espo/Core/Mail/Account/PersonalAccount/Account.php
+++ b/application/Espo/Core/Mail/Account/PersonalAccount/Account.php
@@ -41,15 +41,20 @@ use Espo\Core\Mail\Account\ImapParams;
use Espo\Core\Mail\Smtp\HandlerProcessor;
use Espo\Core\Mail\SmtpParams;
use Espo\Core\ORM\Repository\Option\SaveOption;
+use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Crypt;
use Espo\Entities\EmailAccount;
+use Espo\Entities\InboundEmail;
use Espo\Entities\User;
use Espo\Entities\Email;
use Espo\Core\Mail\Account\Account as AccountInterface;
use Espo\Core\Mail\Account\FetchData;
use Espo\ORM\EntityManager;
+use Espo\ORM\Mapper\Util;
+use Espo\ORM\Name\Attribute;
+use Espo\ORM\Query\UpdateBuilder;
use RuntimeException;
class Account implements AccountInterface
@@ -87,9 +92,24 @@ class Account implements AccountInterface
public function updateFetchData(FetchData $fetchData): void
{
- $this->entity->set('fetchData', $fetchData->getRaw());
-
- $this->entityManager->saveEntity($this->entity, [SaveOption::SILENT => true]);
+ $query = UpdateBuilder::create()
+ ->in(EmailAccount::ENTITY_TYPE)
+ ->where([
+ InboundEmail::FIELD_FETCH_VALIDITY_NUMBER => $fetchData->getValidityNumber(),
+ Attribute::ID => $this->entity->getId(),
+ Attribute::DELETED => false,
+ ])
+ ->set([
+ InboundEmail::FIELD_FETCH_DATA => Util::prepareValue(FieldType::JSON_OBJECT, $fetchData->getRaw()),
+ ])
+ ->build();
+
+ $sth = $this->entityManager->getQueryExecutor()->execute($query);
+
+ if ($sth->rowCount() !== 0) {
+ $this->entity->setFetchData($fetchData);
+ $this->entity->setAsFetched();
+ }
}
public function updateConnectedAt(): void
@@ -187,7 +207,8 @@ class Account implements AccountInterface
public function getFetchData(): FetchData
{
return FetchData::fromRaw(
- $this->entity->getFetchData()
+ $this->entity->getFetchData(),
+ $this->entity->getFetchValidityNumber(),
);
}
diff --git a/application/Espo/Entities/EmailAccount.php b/application/Espo/Entities/EmailAccount.php
index 3ab7a28aa9..316a930954 100644
--- a/application/Espo/Entities/EmailAccount.php
+++ b/application/Espo/Entities/EmailAccount.php
@@ -29,6 +29,7 @@
namespace Espo\Entities;
+use Espo\Core\Mail\Account\FetchData;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity;
@@ -86,6 +87,29 @@ class EmailAccount extends Entity
return (bool) $this->get('storeSentEmails');
}
+ /**
+ * @since 10.0.0
+ */
+ public function getFetchValidityNumber(): int
+ {
+ return $this->get(InboundEmail::FIELD_FETCH_VALIDITY_NUMBER);
+ }
+
+ public function setFetchSince(Date $fetchSince): self
+ {
+ return $this->setValueObject('fetchSince', $fetchSince);
+ }
+
+ public function setFetchData(FetchData $data): self
+ {
+ return $this->set('fetchData', $data->getRaw());
+ }
+
+ public function setFetchValidityNumber(int $number): self
+ {
+ return $this->set(InboundEmail::FIELD_FETCH_VALIDITY_NUMBER, $number);
+ }
+
public function getFetchData(): stdClass
{
$data = $this->get('fetchData') ?? (object) [];
diff --git a/application/Espo/Entities/InboundEmail.php b/application/Espo/Entities/InboundEmail.php
index a17bc4e665..a56c04c708 100644
--- a/application/Espo/Entities/InboundEmail.php
+++ b/application/Espo/Entities/InboundEmail.php
@@ -29,6 +29,7 @@
namespace Espo\Entities;
+use Espo\Core\Mail\Account\FetchData;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity;
@@ -48,6 +49,9 @@ class InboundEmail extends Entity
public const CASE_DISTRIBUTION_ROUND_ROBIN = 'Round-Robin';
public const CASE_DISTRIBUTION_LEAST_BUSY = 'Least-Busy';
+ public const string FIELD_FETCH_VALIDITY_NUMBER = 'fetchValidityNumber';
+ public const string FIELD_FETCH_DATA = 'fetchData';
+
public function isAvailableForFetching(): bool
{
return $this->isActive() && $this->get('useImap');
@@ -136,6 +140,29 @@ class InboundEmail extends Entity
return (bool) $this->get('keepFetchedEmailsUnread');
}
+ /**
+ * @since 10.0.0
+ */
+ public function getFetchValidityNumber(): int
+ {
+ return $this->get(InboundEmail::FIELD_FETCH_VALIDITY_NUMBER);
+ }
+
+ public function setFetchSince(Date $fetchSince): self
+ {
+ return $this->setValueObject('fetchSince', $fetchSince);
+ }
+
+ public function setFetchData(FetchData $data): self
+ {
+ return $this->set('fetchData', $data->getRaw());
+ }
+
+ public function setFetchValidityNumber(int $number): self
+ {
+ return $this->set(InboundEmail::FIELD_FETCH_VALIDITY_NUMBER, $number);
+ }
+
public function getFetchData(): stdClass
{
$data = $this->get('fetchData') ?? (object) [];
diff --git a/application/Espo/ORM/Mapper/BaseMapper.php b/application/Espo/ORM/Mapper/BaseMapper.php
index 637d171491..9ae9521629 100644
--- a/application/Espo/ORM/Mapper/BaseMapper.php
+++ b/application/Espo/ORM/Mapper/BaseMapper.php
@@ -1482,17 +1482,7 @@ class BaseMapper implements RDBMapper
private function prepareValueForInsert(?string $type, mixed $value): mixed
{
- if ($type == Entity::JSON_ARRAY && is_array($value)) {
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- } else if ($type == Entity::JSON_OBJECT && (is_array($value) || $value instanceof stdClass)) {
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- } else {
- if (is_array($value) || is_object($value)) {
- return null;
- }
- }
-
- return $value;
+ return Util::prepareValue($type, $value);
}
/**
diff --git a/application/Espo/ORM/Mapper/Util.php b/application/Espo/ORM/Mapper/Util.php
new file mode 100644
index 0000000000..da2c55f3cd
--- /dev/null
+++ b/application/Espo/ORM/Mapper/Util.php
@@ -0,0 +1,58 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\ORM\Mapper;
+
+use Espo\ORM\Entity;
+use stdClass;
+
+/**
+ * @since 10.0.0
+ * @internal
+ */
+class Util
+{
+ /**
+ * @internal
+ */
+ public static function prepareValue(?string $type, mixed $value): mixed
+ {
+ if ($type == Entity::JSON_ARRAY && is_array($value)) {
+ $value = json_encode($value, JSON_UNESCAPED_UNICODE);
+ } else if ($type == Entity::JSON_OBJECT && (is_array($value) || $value instanceof stdClass)) {
+ $value = json_encode($value, JSON_UNESCAPED_UNICODE);
+ } else {
+ if (is_array($value) || is_object($value)) {
+ return null;
+ }
+ }
+
+ return $value;
+ }
+}
diff --git a/application/Espo/Resources/i18n/en_US/EmailAccount.json b/application/Espo/Resources/i18n/en_US/EmailAccount.json
index 2c2ac2c337..09b003409d 100644
--- a/application/Espo/Resources/i18n/en_US/EmailAccount.json
+++ b/application/Espo/Resources/i18n/en_US/EmailAccount.json
@@ -61,6 +61,7 @@
"active": "Active"
},
"messages": {
+ "resetFetchData": "Emails will be re-fetched starting from the specified date. Existing emails will not be duplicated.",
"noFolder": "Mapped folder does not exist.",
"couldNotConnectToImap": "Could not connect to IMAP server",
"connectionIsOk": "Connection is Ok",
diff --git a/application/Espo/Resources/metadata/clientDefs/EmailAccount.json b/application/Espo/Resources/metadata/clientDefs/EmailAccount.json
index a3eee54a02..f580b1ee60 100644
--- a/application/Espo/Resources/metadata/clientDefs/EmailAccount.json
+++ b/application/Espo/Resources/metadata/clientDefs/EmailAccount.json
@@ -14,6 +14,15 @@
"name": "active"
}
],
+ "detailActionList": [
+ {
+ "name": "resetFetchData",
+ "label": "Reset",
+ "acl": "edit",
+ "handler": "handlers/email-account/reset-fetch-data-action",
+ "actionFunction": "process"
+ }
+ ],
"relationshipPanels": {
"filters": {
"select": false,
diff --git a/application/Espo/Resources/metadata/clientDefs/InboundEmail.json b/application/Espo/Resources/metadata/clientDefs/InboundEmail.json
index ec687614af..9ba42d4df5 100644
--- a/application/Espo/Resources/metadata/clientDefs/InboundEmail.json
+++ b/application/Espo/Resources/metadata/clientDefs/InboundEmail.json
@@ -6,6 +6,15 @@
},
"inlineEditDisabled": true,
"searchPanelDisabled": true,
+ "detailActionList": [
+ {
+ "name": "resetFetchData",
+ "label": "Reset",
+ "acl": "edit",
+ "handler": "handlers/email-account/reset-fetch-data-action",
+ "actionFunction": "process"
+ }
+ ],
"relationshipPanels": {
"filters": {
"select": false,
diff --git a/application/Espo/Resources/metadata/entityDefs/EmailAccount.json b/application/Espo/Resources/metadata/entityDefs/EmailAccount.json
index e5b3d305ed..1accdf89fd 100644
--- a/application/Espo/Resources/metadata/entityDefs/EmailAccount.json
+++ b/application/Espo/Resources/metadata/entityDefs/EmailAccount.json
@@ -71,6 +71,11 @@
"keepFetchedEmailsUnread": {
"type": "bool"
},
+ "fetchValidityNumber": {
+ "type": "int",
+ "readOnly": true,
+ "default": 0
+ },
"fetchSince": {
"type": "date",
"validatorClassNameList": [
diff --git a/application/Espo/Resources/metadata/entityDefs/InboundEmail.json b/application/Espo/Resources/metadata/entityDefs/InboundEmail.json
index 97235114f5..b168fb4bc3 100644
--- a/application/Espo/Resources/metadata/entityDefs/InboundEmail.json
+++ b/application/Espo/Resources/metadata/entityDefs/InboundEmail.json
@@ -51,6 +51,11 @@
"noEmptyString": true,
"duplicateIgnore": true
},
+ "fetchValidityNumber": {
+ "type": "int",
+ "readOnly": true,
+ "default": 0
+ },
"fetchSince": {
"type": "date",
"validatorClassNameList": [
diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json
index cbe5e24523..7db41b5678 100644
--- a/application/Espo/Resources/routes.json
+++ b/application/Espo/Resources/routes.json
@@ -492,6 +492,22 @@
"method": "put",
"actionClassName": "Espo\\Tools\\User\\Api\\PutTeamUserPosition"
},
+ {
+ "route": "EmailAccount/:id/resetFetchData",
+ "method": "post",
+ "actionClassName": "Espo\\Tools\\EmailAccount\\Api\\PostResetFetchData",
+ "params": {
+ "entityType": "EmailAccount"
+ }
+ },
+ {
+ "route": "InboundEmail/:id/resetFetchData",
+ "method": "post",
+ "actionClassName": "Espo\\Tools\\EmailAccount\\Api\\PostResetFetchData",
+ "params": {
+ "entityType": "InboundEmail"
+ }
+ },
{
"route": "/Oidc/authorizationData",
"method": "get",
diff --git a/application/Espo/Tools/EmailAccount/Api/PostResetFetchData.php b/application/Espo/Tools/EmailAccount/Api/PostResetFetchData.php
new file mode 100644
index 0000000000..7935f4746b
--- /dev/null
+++ b/application/Espo/Tools/EmailAccount/Api/PostResetFetchData.php
@@ -0,0 +1,95 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+declare(strict_types=1);
+
+namespace Espo\Tools\EmailAccount\Api;
+
+use Espo\Core\Acl;
+use Espo\Core\Api\Action;
+use Espo\Core\Api\Request;
+use Espo\Core\Api\Response;
+use Espo\Core\Api\ResponseComposer;
+use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Forbidden;
+use Espo\Core\Field\Date;
+use Espo\Core\Record\EntityProvider;
+use Espo\Core\Record\ServiceContainer;
+use Espo\Entities\EmailAccount;
+use Espo\Entities\InboundEmail;
+use Espo\Tools\EmailAccount\ResetService;
+use InvalidArgumentException;
+
+/**
+ * @noinspection PhpUnused
+ */
+class PostResetFetchData implements Action
+{
+ public function __construct(
+ private EntityProvider $entityProvider,
+ private Acl $acl,
+ private ResetService $service,
+ private ServiceContainer $serviceContainer,
+ ) {}
+
+ public function process(Request $request): Response
+ {
+ $entityType = $request->getRouteParam('entityType');
+ $id = $request->getRouteParam('id') ?? throw new BadRequest();
+ $fetchSinceRaw = $request->getParsedBody()->fetchSince ?? null;
+
+ if (!is_string($fetchSinceRaw)) {
+ throw new BadRequest("No or bad 'fetchSince'.");
+ }
+
+ try {
+ $fetchSince = Date::fromString($fetchSinceRaw);
+ } catch (InvalidArgumentException) {
+ throw new BadRequest("Bad date.");
+ }
+
+ if ($entityType === EmailAccount::ENTITY_TYPE) {
+ $entity = $this->entityProvider->getByClass(EmailAccount::class, $id);
+ } else if ($entityType === InboundEmail::ENTITY_TYPE) {
+ $entity = $this->entityProvider->getByClass(InboundEmail::class, $id);
+ } else {
+ throw new BadRequest();
+ }
+
+ if (!$this->acl->checkEntityEdit($entity)) {
+ throw new Forbidden("No edit access.");
+ }
+
+ $this->service->reset($entity, $fetchSince);
+
+ $this->serviceContainer->get($entityType)->prepareEntityForOutput($entity);
+
+ return ResponseComposer::json($entity->getValueMap());
+ }
+}
diff --git a/application/Espo/Tools/EmailAccount/ResetService.php b/application/Espo/Tools/EmailAccount/ResetService.php
new file mode 100644
index 0000000000..a645d2ab20
--- /dev/null
+++ b/application/Espo/Tools/EmailAccount/ResetService.php
@@ -0,0 +1,57 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+declare(strict_types=1);
+
+namespace Espo\Tools\EmailAccount;
+
+use Espo\Core\Field\Date;
+use Espo\Core\Mail\Account\FetchData;
+use Espo\Entities\EmailAccount;
+use Espo\Entities\InboundEmail;
+use Espo\ORM\EntityManager;
+
+class ResetService
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function reset(InboundEmail|EmailAccount $entity, Date $fetchSince): void
+ {
+ $number = $entity->getFetchValidityNumber();
+
+ $entity
+ ->setFetchSince($fetchSince)
+ ->setFetchData(new FetchData())
+ ->setFetchValidityNumber($number + 1);
+
+ $this->entityManager->saveEntity($entity);
+ }
+}
diff --git a/client/src/handlers/email-account/reset-fetch-data-action.js b/client/src/handlers/email-account/reset-fetch-data-action.js
new file mode 100644
index 0000000000..b2770b4779
--- /dev/null
+++ b/client/src/handlers/email-account/reset-fetch-data-action.js
@@ -0,0 +1,54 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+import ActionHandler from 'action-handler';
+import {inject} from 'di';
+import Language from 'language';
+import ResetFetchDataModalView from 'views/email-account/modals/reset-fetch-data';
+
+export default class ResetFetchDataActionHandler extends ActionHandler {
+
+ /**
+ * @private
+ * @type {Language}
+ */
+ @inject(Language)
+ language
+
+ async process() {
+ const model = this.view.model;
+
+ const view = new ResetFetchDataModalView({
+ model: model,
+ });
+
+ await this.view.assignView('dialog', view);
+ await view.render();
+ }
+}
+
diff --git a/client/src/views/email-account/modals/reset-fetch-data.ts b/client/src/views/email-account/modals/reset-fetch-data.ts
new file mode 100644
index 0000000000..066e698281
--- /dev/null
+++ b/client/src/views/email-account/modals/reset-fetch-data.ts
@@ -0,0 +1,128 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+import ModalView, {ModalViewOptions} from 'views/modal';
+import Ajax from 'ajax';
+import EditForModalRecordView from 'views/record/edit-for-modal';
+import Model from 'model';
+import DateTime from 'date-time';
+import {inject} from 'di';
+import DateFieldView from 'views/fields/date';
+import Ui from 'ui';
+
+export default class ResetFetchDataModalView extends ModalView<{
+ model: Model,
+ options: ModalViewOptions,
+}> {
+
+ templateContent = `
+ <div
+ class="alert alert-warning"
+ >{{translate 'resetFetchData' category='messages' scope='EmailAccount'}}</div>
+ <div class="record-container no-side-margin">{{{record}}}</div>
+ `
+ private recordView: EditForModalRecordView;
+
+ private formModel: Model<{
+ fetchSince: string | null;
+ }>;
+
+ @inject(DateTime)
+ private dateTime: DateTime
+
+ protected setup() {
+ this.buttonList = [
+ {
+ name: 'Reset',
+ style: 'danger',
+ onClick: () => this.processReset(),
+ },
+ {
+ name: 'Cancel',
+ onClick: () => this.close(),
+ },
+ ];
+
+ this.formModel = new Model({
+ fetchSince: this.dateTime.getToday(),
+ });
+
+ this.recordView = new EditForModalRecordView({
+ model: this.formModel,
+ detailLayout: [
+ {
+ rows: [
+ [
+ {
+ view: new DateFieldView({
+ name: 'fetchSince',
+ labelText: this.translate('fetchSince', 'fields', 'EmailAccount'),
+ params: {
+ required: true,
+ },
+ })
+ },
+ false,
+ ]
+ ]
+ }
+ ],
+ });
+
+ this.assignView('record', this.recordView);
+ }
+
+ private async processReset() {
+ if (this.recordView.validate()) {
+ return;
+ }
+
+ await this.confirm({message: this.translate('confirmation', 'messages')});
+
+ Ui.notifyWait();
+ this.disableButton('reset');
+
+ let response: Record<string, any>;
+
+ try {
+ response = await Ajax.postRequest(`${this.model.entityType!}/${this.model.id!}/resetFetchData`, {
+ fetchSince: this.formModel.attributes.fetchSince,
+ }) as Record<string, any>;
+ } catch (e) {
+ this.enableButton('reset');
+
+ return;
+ }
+
+ this.model.setMultiple(response, {sync: true});
+
+ Ui.success(this.translate('Done'));
+
+ this.close();
+ }
+}
diff --git a/tests/unit/Espo/Core/Mail/Account/FetchDataTest.php b/tests/unit/Espo/Core/Mail/Account/FetchDataTest.php
index 7164f16663..f71a1f5caf 100644
--- a/tests/unit/Espo/Core/Mail/Account/FetchDataTest.php
+++ b/tests/unit/Espo/Core/Mail/Account/FetchDataTest.php
@@ -46,7 +46,7 @@ class FetchDataTest extends \PHPUnit\Framework\TestCase
],
];
- $data = FetchData::fromRaw($raw);
+ $data = FetchData::fromRaw($raw, 0);
$this->assertEquals(10, $data->getLastUid('test'));
$this->assertEquals('2022-01-01 00:00:00', $data->getLastDate('test')->toString());