dependencies[] = 'mailSender'; $this->dependencies[] = 'preferences'; $this->dependencies[] = 'fileManager'; $this->dependencies[] = 'crypt'; $this->dependencies[] = 'serviceFactory'; } private $streamService = null; protected $getEntityBeforeUpdate = true; protected function getFileManager() { return $this->getInjection('fileManager'); } protected function getMailSender() { return $this->injections['mailSender']; } protected function getPreferences() { return $this->injections['preferences']; } protected function getCrypt() { return $this->injections['crypt']; } protected function getServiceFactory() { return $this->injections['serviceFactory']; } protected function send(Entities\Email $entity) { $emailSender = $this->getMailSender(); if (strtolower($this->getUser()->get('emailAddress')) == strtolower($entity->get('from'))) { $smtpParams = $this->getPreferences()->getSmtpParams(); if (array_key_exists('password', $smtpParams)) { $smtpParams['password'] = $this->getCrypt()->decrypt($smtpParams['password']); } if ($smtpParams) { $smtpParams['fromName'] = $this->getUser()->get('name'); $emailSender->useSmtp($smtpParams); } } else { if (!$this->getConfig()->get('outboundEmailIsShared')) { throw new Error('Can not use system smtp. outboundEmailIsShared is false.'); } $emailSender->setParams(array( 'fromName' => $this->getUser()->get('name') )); } $params = array(); if ($entity->get('parentType') && $entity->get('parentId')) { $parent = $this->getEntityManager()->getEntity($entity->get('parentType'), $entity->get('parentId')); if ($parent) { if ($entity->get('parentType') == 'Case' && $parent->get('inboundEmailId')) { $inboundEmail = $this->getEntityManager()->getEntity('InboundEmail', $parent->get('inboundEmailId')); if ($inboundEmail && $inboundEmail->get('replyToAddress')) { $params['replyToAddress'] = $inboundEmail->get('replyToAddress'); } $this->getStreamService()->noteEmailSent($parent, $entity); } } } $emailSender->send($entity, $params); $this->getEntityManager()->saveEntity($entity); } protected function getStreamService() { if (empty($this->streamService)) { $this->streamService = $this->getServiceFactory()->create('Stream'); } return $this->streamService; } public function createEntity($data) { $entity = parent::createEntity($data); if ($entity && $entity->get('status') == 'Sending') { $this->send($entity); } return $entity; } public function getEntity($id = null) { $entity = parent::getEntity($id); if (!empty($entity) && !empty($id)) { if ($entity->get('fromEmailAddressName')) { $entity->set('from', $entity->get('fromEmailAddressName')); } $entity->loadLinkMultipleField('toEmailAddresses'); $entity->loadLinkMultipleField('ccEmailAddresses'); $entity->loadLinkMultipleField('bccEmailAddresses'); $names = $entity->get('toEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } $entity->set('to', implode(';', $arr)); } $names = $entity->get('ccEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } $entity->set('cc', implode(';', $arr)); } $names = $entity->get('bccEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } $entity->set('bcc', implode(';', $arr)); } $this->loadNameHash($entity); $this->loadAttachmentsTypes($entity); } return $entity; } protected function loadAttachmentsTypes(Entity $entity) { $types = new \stdClass(); $attachmentsIds = $entity->get('attachmentsIds'); if (!empty($attachmentsIds)) { foreach ($attachmentsIds as $id) { $attachment = $this->getEntityManager()->getEntity('Attachment', $id); if ($attachment) { $types->$id = $attachment->get('type'); } } } $entity->set('attachmentsTypes', $types); } public function loadNameHash(Entity $entity) { $addressList = array(); if ($entity->get('from')) { $addressList[] = $entity->get('from'); } $arr = explode(';', $entity->get('to')); foreach ($arr as $address) { if (!in_array($address, $addressList)) { $addressList[] = $address; } } $arr = explode(';', $entity->get('cc')); foreach ($arr as $address) { if (!in_array($address, $addressList)) { $addressList[] = $address; } } $nameHash = array(); $typeHash = array(); $idHash = array(); foreach ($addressList as $address) { $p = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($address); if ($p) { $nameHash[$address] = $p->get('name'); $typeHash[$address] = $p->getEntityName(); $idHash[$address] = $p->id; } } $entity->set('nameHash', $nameHash); $entity->set('typeHash', $typeHash); $entity->set('idHash', $idHash); } public function findEntities($params) { $searchByEmailAddress = false; if (!empty($params['where']) && is_array($params['where'])) { foreach ($params['where'] as $i => $p) { if (!empty($p['field']) && $p['field'] == 'emailAddress') { $searchByEmailAddress = true; $emailAddress = $p['value']; unset($params['where'][$i]); } } } $selectManager = $this->getSelectManager($this->entityName); $selectParams = $selectManager->getSelectParams($params, true); if ($searchByEmailAddress) { $selectManager->whereEmailAddress($emailAddress, $selectParams); } $collection = $this->getRepository()->find($selectParams); foreach ($collection as $e) { $this->loadParentNameFields($e); } return array( 'total' => $this->getRepository()->count($selectParams), 'collection' => $collection, ); } public function copyAttachments($emailId, $parentType, $parentId) { return $this->getCopiedAttachments($emailId, $parentType, $parentId); } public function getCopiedAttachments($id, $parentType = null, $parentId = null) { $ids = array(); $names = new \stdClass(); if (!empty($id)) { $email = $this->getEntityManager()->getEntity('Email', $id); if ($email && $this->getAcl()->check($email, 'read')) { $email->loadLinkMultipleField('attachments'); $attachmentsIds = $email->get('attachmentsIds'); foreach ($attachmentsIds as $attachmentId) { $source = $this->getEntityManager()->getEntity('Attachment', $attachmentId); if ($source) { $attachment = $this->getEntityManager()->getEntity('Attachment'); $attachment->set('role', 'Attachment'); $attachment->set('type', $source->get('type')); $attachment->set('size', $source->get('size')); $attachment->set('global', $source->get('global')); $attachment->set('name', $source->get('name')); if (!empty($parentType) && !empty($parentId)) { $attachment->set('parentType', $parentType); $attachment->set('parentId', $parentId); } $this->getEntityManager()->saveEntity($attachment); $contents = $this->getFileManager()->getContents('data/upload/' . $source->id); if (!empty($contents)) { $this->getFileManager()->putContents('data/upload/' . $attachment->id, $contents); $ids[] = $attachment->id; $names->{$attachment->id} = $attachment->get('name'); } } } } } return array( 'ids' => $ids, 'names' => $names ); } public function sendTestEmail($data) { $email = $this->getEntityManager()->getEntity('Email'); $email->set(array( 'subject' => 'EspoCRM: Test Email', 'isHtml' => false, 'to' => $data['emailAddress'] )); $emailSender = $this->getMailSender(); $emailSender->useSmtp($data)->send($email); return true; } }