ref
This commit is contained in:
@@ -33,35 +33,32 @@ use Espo\Core\Job\JobDataLess;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
use Espo\Core\Utils\DateTime;
|
||||
use Espo\Core\Utils\Log;
|
||||
|
||||
use Espo\Modules\Crm\Entities\MassEmail;
|
||||
use Espo\Modules\Crm\Tools\MassEmail\QueueCreator;
|
||||
use Espo\Modules\Crm\Tools\MassEmail\SendingProcessor;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class ProcessMassEmail implements JobDataLess
|
||||
{
|
||||
private SendingProcessor $processor;
|
||||
private QueueCreator $queue;
|
||||
private EntityManager $entityManager;
|
||||
private Log $log;
|
||||
|
||||
public function __construct(
|
||||
SendingProcessor $processor,
|
||||
QueueCreator $queue,
|
||||
EntityManager $entityManager,
|
||||
Log $log
|
||||
) {
|
||||
$this->processor = $processor;
|
||||
$this->queue = $queue;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->log = $log;
|
||||
}
|
||||
private SendingProcessor $processor,
|
||||
private QueueCreator $queue,
|
||||
private EntityManager $entityManager,
|
||||
private Log $log
|
||||
) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$pendingMassEmailList = $this->entityManager
|
||||
$this->processCreateQueue();
|
||||
$this->processSend();
|
||||
}
|
||||
|
||||
private function processCreateQueue(): void
|
||||
{
|
||||
$pendingMassEmails = $this->entityManager
|
||||
->getRDBRepositoryByClass(MassEmail::class)
|
||||
->where([
|
||||
'status' => MassEmail::STATUS_PENDING,
|
||||
@@ -69,32 +66,37 @@ class ProcessMassEmail implements JobDataLess
|
||||
])
|
||||
->find();
|
||||
|
||||
foreach ($pendingMassEmailList as $massEmail) {
|
||||
foreach ($pendingMassEmails as $massEmail) {
|
||||
try {
|
||||
$this->queue->create($massEmail);
|
||||
} catch (Throwable $e) {
|
||||
$this->log->error(
|
||||
'Job ProcessMassEmail#createQueue ' . $massEmail->getId() . ': [' . $e->getCode() . '] ' .
|
||||
$e->getMessage()
|
||||
);
|
||||
$this->log->error("Create queue error. {id}. {message}", [
|
||||
'id' => $massEmail->getId(),
|
||||
'message' => $e->getMessage(),
|
||||
'exception' => $e,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$massEmailList = $this->entityManager
|
||||
private function processSend(): void
|
||||
{
|
||||
$inProcessMassEmails = $this->entityManager
|
||||
->getRDBRepositoryByClass(MassEmail::class)
|
||||
->where([
|
||||
'status' => MassEmail::STATUS_IN_PROCESS,
|
||||
])
|
||||
->find();
|
||||
|
||||
foreach ($massEmailList as $massEmail) {
|
||||
foreach ($inProcessMassEmails as $massEmail) {
|
||||
try {
|
||||
$this->processor->process($massEmail);
|
||||
} catch (Throwable $e) {
|
||||
$this->log->error(
|
||||
'Job ProcessMassEmail#processSending '. $massEmail->getId() . ': [' . $e->getCode() . '] ' .
|
||||
$e->getMessage()
|
||||
);
|
||||
$this->log->error("Sending mass email error. {id}. {message}", [
|
||||
'id' => $massEmail->getId(),
|
||||
'message' => $e->getMessage(),
|
||||
'exception' => $e,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
namespace Espo\Modules\Crm\Tools\MassEmail;
|
||||
|
||||
use Espo\ORM\EntityCollection;
|
||||
use Laminas\Mail\Message;
|
||||
|
||||
use Espo\Core\Name\Field;
|
||||
@@ -109,14 +110,14 @@ class SendingProcessor
|
||||
|
||||
foreach ($queueItemList as $queueItem) {
|
||||
$this->sendQueueItem(
|
||||
$queueItem,
|
||||
$massEmail,
|
||||
$emailTemplate,
|
||||
$attachmentList,
|
||||
$campaign,
|
||||
$isTest,
|
||||
$smtpParams,
|
||||
$senderParams
|
||||
queueItem: $queueItem,
|
||||
massEmail: $massEmail,
|
||||
emailTemplate: $emailTemplate,
|
||||
attachmentList: $attachmentList,
|
||||
campaign: $campaign,
|
||||
isTest: $isTest,
|
||||
smtpParams: $smtpParams,
|
||||
senderParams: $senderParams,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -142,7 +143,7 @@ class SendingProcessor
|
||||
iterable $trackingUrlList = []
|
||||
): ?Email {
|
||||
|
||||
$emailAddress = $target->get('emailAddress');
|
||||
$emailAddress = $target->get(Field::EMAIL_ADDRESS);
|
||||
|
||||
if (!$emailAddress) {
|
||||
return null;
|
||||
@@ -159,7 +160,6 @@ class SendingProcessor
|
||||
|
||||
$body = $this->prepareBody($emailData, $queueItem, $trackingUrlList, $massEmail);
|
||||
|
||||
/** @var Email $email */
|
||||
$email = $this->entityManager
|
||||
->getRDBRepositoryByClass(Email::class)
|
||||
->getNew();
|
||||
@@ -229,13 +229,13 @@ class SendingProcessor
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<Attachment> $attachmentList
|
||||
* @param EntityCollection<Attachment> $attachmentList
|
||||
*/
|
||||
private function sendQueueItem(
|
||||
EmailQueueItem $queueItem,
|
||||
MassEmail $massEmail,
|
||||
EmailTemplate $emailTemplate,
|
||||
$attachmentList,
|
||||
EntityCollection $attachmentList,
|
||||
?Campaign $campaign,
|
||||
bool $isTest,
|
||||
?SmtpParams $smtpParams,
|
||||
@@ -250,7 +250,7 @@ class SendingProcessor
|
||||
|
||||
$target = $this->entityManager->getEntityById($queueItem->getTargetType(), $queueItem->getTargetId());
|
||||
|
||||
$emailAddress = $target?->get('emailAddress');
|
||||
$emailAddress = $target?->get(Field::EMAIL_ADDRESS);
|
||||
|
||||
if (
|
||||
!$target ||
|
||||
|
||||
Reference in New Issue
Block a user