public function process(Entity $entity) { if ($entity->get('status') !== 'Archived' && $entity->get('status') !== 'Sent') { return; } if ($entity->get('isJustSent')) { $previousUserIdList = []; } else { $previousUserIdList = $entity->getFetched('usersIds'); if (!is_array($previousUserIdList)) { $previousUserIdList = []; } } $emailUserIdList = $entity->get('usersIds'); if (is_null($emailUserIdList) || !is_array($emailUserIdList)) { return; } $userIdList = []; foreach ($emailUserIdList as $userId) { if (!in_array($userId, $userIdList) && !in_array($userId, $previousUserIdList) && $userId != $this->getUser()->id) { $userIdList[] = $userId; } } $data = array('emailId' => $entity->id, 'emailName' => $entity->get('name')); if (!$entity->has('from')) { $this->getEntityManager()->getRepository('Email')->loadFromField($entity); } $from = $entity->get('from'); if ($from) { $person = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($from, null, ['User', 'Contact', 'Lead']); if ($person) { $data['personEntityType'] = $person->getEntityType(); $data['personEntityName'] = $person->get('name'); $data['personEntityId'] = $person->id; } } $userIdFrom = null; if ($person && $person->getEntityType() == 'User') { $userIdFrom = $person->id; } if (empty($data['personEntityId'])) { $data['fromString'] = \Espo\Services\Email::parseFromName($entity->get('fromString')); if (empty($data['fromString']) && $from) { $data['fromString'] = $from; } } $parent = null; if ($entity->get('parentId') && $entity->get('parentType')) { $parent = $this->getEntityManager()->getEntity($entity->get('parentType'), $entity->get('parentId')); } $account = null; if ($entity->get('accountId')) { $account = $this->getEntityManager()->getEntity('Account', $entity->get('accountId')); } foreach ($userIdList as $userId) { if ($userIdFrom == $userId) { continue; } if ($entity->get('status') == 'Archived') { if ($parent) { if ($this->getStreamService()->checkIsFollowed($parent, $userId)) { continue; } } if ($account) { if ($this->getStreamService()->checkIsFollowed($account, $userId)) { continue; } } } $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array('type' => 'EmailReceived', 'userId' => $userId, 'data' => $data)); $this->getEntityManager()->saveEntity($notification); } }
protected function autoReply($inboundEmail, $email, $case = null, $user = null) { if (!$email->get('from')) { return false; } $d = new \DateTime(); $d->modify('-3 hours'); $threshold = $d->format('Y-m-d H:i:s'); $emailAddress = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($email->get('from')); $sent = $this->getEntityManager()->getRepository('Email')->where(array('toEmailAddresses.id' => $emailAddress->id, 'dateSent>' => $threshold, 'status' => 'Sent'))->join('toEmailAddresses')->findOne(); if ($sent) { return false; } try { $replyEmailTemplateId = $inboundEmail->get('replyEmailTemplateId'); if ($replyEmailTemplateId) { $entityHash = array(); if ($case) { $entityHash['Case'] = $case; if ($case->get('contactId')) { $contact = $this->getEntityManager()->getEntity('Contact', $case->get('contactId')); } } if (empty($contact)) { $contact = $this->getEntityManager()->getEntity('Contact'); $fromName = \Espo\Services\Email::parseFromName($email->get('fromString')); if (!empty($fromName)) { $contact->set('name', $fromName); } } $entityHash['Person'] = $contact; $entityHash['Contact'] = $contact; if ($user) { $entityHash['User'] = $user; } $emailTemplateService = $this->getServiceFactory()->create('EmailTemplate'); $replyData = $emailTemplateService->parse($replyEmailTemplateId, array('entityHash' => $entityHash), true); $subject = $replyData['subject']; if ($case) { $subject = '[#' . $case->get('number') . '] ' . $subject; } $reply = $this->getEntityManager()->getEntity('Email'); $reply->set('to', $email->get('from')); $reply->set('subject', $subject); $reply->set('body', $replyData['body']); $reply->set('isHtml', $replyData['isHtml']); $reply->set('attachmentsIds', $replyData['attachmentsIds']); if ($email->has('teamsIds')) { $reply->set('teamsIds', $email->get('teamsIds')); } if ($email->get('parentId') && $email->get('parentType')) { $reply->set('parentId', $email->get('parentId')); $reply->set('parentType', $email->get('parentType')); } $this->getEntityManager()->saveEntity($reply); $sender = $this->getMailSender()->useGlobal(); $senderParams = array(); if ($inboundEmail->get('replyFromAddress')) { $senderParams['fromAddress'] = $inboundEmail->get('replyFromAddress'); } if ($inboundEmail->get('replyFromName')) { $senderParams['fromName'] = $inboundEmail->get('replyFromName'); } if ($inboundEmail->get('replyToAddress')) { $senderParams['replyToAddress'] = $inboundEmail->get('replyToAddress'); } $sender->send($reply, $senderParams); $this->getEntityManager()->saveEntity($reply); return true; } } catch (\Exception $e) { } }