Exemple #1
0
 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') {
                 if ($parent->get('inboundEmailId')) {
                     $inboundEmail = $this->getEntityManager()->getEntity('InboundEmail', $parent->get('inboundEmailId'));
                     if ($inboundEmail && $inboundEmail->get('replyToAddress')) {
                         $params['replyToAddress'] = $inboundEmail->get('replyToAddress');
                     }
                 }
             }
         }
     }
     $message = null;
     try {
         $emailSender->send($entity, $params, $message);
     } catch (\Exception $e) {
         $entity->set('status', 'Failed');
         $this->getEntityManager()->saveEntity($entity, array('silent' => true));
         throw new Error($e->getMessage(), $e->getCode());
     }
     if ($entity->get('from') && $message) {
         $emailAccount = $this->getEntityManager()->getRepository('EmailAccount')->where(array('storeSentEmails' => true, 'emailAddress' => $entity->get('from'), 'assignedUserId' => $this->getUser()->id))->findOne();
         if ($emailAccount) {
             try {
                 $emailAccountService = $this->getServiceFactory()->create('EmailAccount');
                 $emailAccountService->storeSentMessage($emailAccount, $message);
             } catch (\Exception $e) {
                 $GLOBALS['log']->error("Could not store sent email (Email Account {$emailAccount->id}): " . $e->getMessage());
             }
         }
     }
     if ($parent) {
         $this->getStreamService()->noteEmailSent($parent, $entity);
     }
     $entity->set('isJustSent', true);
     $this->getEntityManager()->saveEntity($entity);
 }
 public function matchBody(Email $email, $filterList = [])
 {
     foreach ($filterList as $filter) {
         if ($filter->get('bodyContains')) {
             $phraseList = $filter->get('bodyContains');
             $body = $email->get('body');
             $bodyPlain = $email->get('bodyPlain');
             foreach ($phraseList as $phrase) {
                 if (stripos($bodyPlain, $phrase) !== false) {
                     return true;
                 }
                 if (stripos($body, $phrase) !== false) {
                     return true;
                 }
             }
         }
     }
     return false;
 }
Exemple #3
0
 public function send(Email $email)
 {
     $message = new Message();
     $config = $this->config;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($this->params['fromName'])) {
             $fromName = $this->params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($this->params['fromAddress'])) {
             $fromAddress = $this->params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($this->params['fromName'])) {
             $fromName = $this->params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $parts = array();
     if ($email->get('isHtml')) {
         $bodyPart = new MimePart($email->getBodyForSending());
         $bodyPart->type = 'text/html';
         $bodyPart->charset = 'utf-8';
     } else {
         if ($email->get('bodyPlain')) {
             $bodyPart = new MimePart($email->get('bodyPlain'));
         } else {
             $bodyPart = new MimePart($email->get('body'));
         }
         $bodyPart->type = 'text/plain';
         $bodyPart->charset = 'utf-8';
     }
     $parts[] = $bodyPart;
     $aCollection = $email->get('attachments');
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $aCollection = $email->getInlineAttachments();
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $body->setParts($parts);
     $message->setBody($body);
     try {
         $this->transport->send($message);
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }
 protected function importPartDataToEmail(\Espo\Entities\Email $email, $part, &$inlineIds = array())
 {
     try {
         $type = strtok($part->contentType, ';');
         $encoding = null;
         switch ($type) {
             case 'text/plain':
                 $content = $this->getContentFromPart($part);
                 if (!$email->get('body')) {
                     $email->set('body', $content);
                 }
                 $email->set('bodyPlain', $content);
                 break;
             case 'text/html':
                 $content = $this->getContentFromPart($part);
                 $email->set('body', $content);
                 $email->set('isHtml', true);
                 break;
             default:
                 $content = $part->getContent();
                 $disposition = null;
                 $fileName = null;
                 $contentId = null;
                 if (isset($part->ContentDisposition)) {
                     if (strpos($part->ContentDisposition, 'attachment') === 0) {
                         if (preg_match('/filename="?([^"]+)"?/i', $part->ContentDisposition, $m)) {
                             $fileName = $m[1];
                             $disposition = 'attachment';
                         }
                     } else {
                         if (strpos($part->ContentDisposition, 'inline') === 0) {
                             $contentId = trim($part->contentID, '<>');
                             $fileName = $contentId;
                             $disposition = 'inline';
                         }
                     }
                 }
                 if (isset($part->contentTransferEncoding)) {
                     $encoding = strtolower($part->getHeader('Content-Transfer-Encoding')->getTransferEncoding());
                 }
                 $attachment = $this->getEntityManager()->getEntity('Attachment');
                 $attachment->set('name', $fileName);
                 $attachment->set('type', $type);
                 $this->getEntityManager()->saveEntity($attachment);
                 $path = 'data/upload/' . $attachment->id;
                 if ($encoding == 'base64') {
                     $content = base64_decode($content);
                 }
                 $this->getFileManager()->putContents($path, $content);
                 if ($disposition == 'attachment') {
                     $attachmentsIds = $email->get('attachmentsIds');
                     $attachmentsIds[] = $attachment->id;
                     $email->set('attachmentsIds', $attachmentsIds);
                 } else {
                     if ($disposition == 'inline') {
                         $inlineIds[$contentId] = $attachment->id;
                     }
                 }
         }
     } catch (\Exception $e) {
         // TODO log
     }
 }
Exemple #5
0
 protected function importPartDataToEmail(\Espo\Entities\Email $email, $part, &$inlineIds = array(), $defaultContentType = null)
 {
     try {
         $type = null;
         if ($part->getHeaders() && isset($part->contentType)) {
             $type = strtok($part->contentType, ';');
         }
         $contentDisposition = false;
         if (isset($part->ContentDisposition)) {
             if (strpos(strtolower($part->ContentDisposition), 'attachment') === 0) {
                 $contentDisposition = 'attachment';
             } else {
                 if (strpos(strtolower($part->ContentDisposition), 'inline') === 0) {
                     $contentDisposition = 'inline';
                 }
             }
         }
         if (empty($type)) {
             if (!empty($defaultContentType)) {
                 $type = $defaultContentType;
             } else {
                 return;
             }
         }
         $encoding = null;
         $isAttachment = true;
         if ($type == 'text/plain' || $type == 'text/html') {
             if ($contentDisposition !== 'attachment') {
                 $isAttachment = false;
                 $content = $this->getContentFromPart($part);
                 if ($type == 'text/plain') {
                     $bodyPlain = '';
                     if ($email->get('bodyPlain')) {
                         $bodyPlain .= $email->get('bodyPlain') . "\n";
                     }
                     $bodyPlain .= $content;
                     $email->set('bodyPlain', $bodyPlain);
                 } else {
                     if ($type == 'text/html') {
                         $body = '';
                         if ($email->get('body')) {
                             $body .= $email->get('body') . "<br>";
                         }
                         $body .= $content;
                         $email->set('isHtml', true);
                         $email->set('body', $body);
                     }
                 }
             }
         }
         if ($isAttachment) {
             $content = $part->getContent();
             $disposition = null;
             $fileName = null;
             $contentId = null;
             if ($contentDisposition) {
                 if ($contentDisposition === 'attachment') {
                     $fileName = $this->fetchFileNameFromContentDisposition($part->ContentDisposition);
                     if ($fileName) {
                         $disposition = 'attachment';
                     }
                 } else {
                     if ($contentDisposition === 'inline') {
                         if (isset($part->contentID)) {
                             $contentId = trim($part->contentID, '<>');
                             $fileName = $contentId;
                             $disposition = 'inline';
                         } else {
                             // for iOS attachments
                             if (empty($fileName)) {
                                 $fileName = $this->fetchFileNameFromContentDisposition($part->ContentDisposition);
                                 if ($fileName) {
                                     $disposition = 'attachment';
                                 }
                             }
                         }
                     }
                 }
             }
             if (isset($part->contentTransferEncoding)) {
                 $encoding = strtolower($this->normilizeHeader($part->getHeader('Content-Transfer-Encoding'))->getTransferEncoding());
             }
             $attachment = $this->getEntityManager()->getEntity('Attachment');
             $attachment->set('name', $fileName);
             $attachment->set('type', $type);
             if ($disposition == 'inline') {
                 $attachment->set('role', 'Inline Attachment');
             } else {
                 $attachment->set('role', 'Attachment');
             }
             if ($encoding == 'base64') {
                 $content = base64_decode($content);
             }
             $attachment->set('size', strlen($content));
             $this->getEntityManager()->saveEntity($attachment);
             $path = 'data/upload/' . $attachment->id;
             $this->getFileManager()->putContents($path, $content);
             if ($disposition == 'attachment') {
                 $attachmentsIds = $email->get('attachmentsIds');
                 $attachmentsIds[] = $attachment->id;
                 $email->set('attachmentsIds', $attachmentsIds);
             } else {
                 if ($disposition == 'inline') {
                     $inlineIds[$contentId] = $attachment->id;
                 }
             }
         }
     } catch (\Exception $e) {
     }
 }
Exemple #6
0
 protected function emailToCase(\Espo\Entities\Email $email, array $params = array())
 {
     $case = $this->getEntityManager()->getEntity('Case');
     $case->populateDefaults();
     $case->set('name', $email->get('name'));
     $userId = $this->getUser()->id;
     if (!empty($params['userId'])) {
         $userId = $params['userId'];
     }
     $case->set('assignedUserId', $userId);
     $teamId = false;
     if (!empty($params['teamId'])) {
         $teamId = $params['teamId'];
     }
     if ($teamId) {
         $case->set('teamsIds', array($teamId));
     }
     $caseDistribution = 'Direct-Assignment';
     if (!empty($params['caseDistribution'])) {
         $caseDistribution = $params['caseDistribution'];
     }
     $case->set('status', 'Assigned');
     switch ($caseDistribution) {
         case 'Round-Robin':
             if ($teamId) {
                 $team = $this->getEntityManager()->getEntity('Team', $teamId);
                 if ($team) {
                     $this->assignRoundRobin($case, $team);
                 }
             }
             break;
         case 'Least-Busy':
             if ($teamId) {
                 $team = $this->getEntityManager()->getEntity('Team', $teamId);
                 if ($team) {
                     $this->assignLeastBusy($case, $team);
                 }
             }
             break;
     }
     $email->set('assignedUserId', $case->get('assignedUserId'));
     $contact = $this->getEntityManager()->getRepository('Contact')->where(array('EmailAddress.id' => $email->get('fromEmailAddressId')))->findOne();
     if ($contact) {
         $case->set('contactId', $contact->id);
         if ($contact->get('accountId')) {
             $case->set('accountId', $contact->get('accountId'));
         }
     }
     $this->getEntityManager()->saveEntity($case);
     $email->set('parentType', 'Case');
     $email->set('parentId', $case->id);
     $this->getEntityManager()->saveEntity($email);
     $case = $this->getEntityManager()->getEntity('Case', $case->id);
     return $case;
 }
Exemple #7
0
 public function send(Email $email, $params = array(), &$message = null, $attachmetList = [])
 {
     if (!$message) {
         $message = new Message();
     }
     $config = $this->config;
     $params = $this->params + $params;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($params['fromAddress'])) {
             $fromAddress = $params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     if (!$email->get('from')) {
         $email->set('from', $fromAddress);
     }
     if (!empty($params['replyToAddress'])) {
         $replyToName = null;
         if (!empty($params['replyToName'])) {
             $replyToName = $params['replyToName'];
         }
         $message->setReplyTo($params['replyToAddress'], $replyToName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $value = $email->get('replyTo');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addReplyTo(trim($address));
             }
         }
     }
     $attachmentPartList = array();
     $attachmentCollection = $email->get('attachments');
     $attachmentInlineCollection = $email->getInlineAttachments();
     foreach ($attachmetList as $attachment) {
         $attachmentCollection[] = $attachment;
     }
     if (!empty($attachmentCollection)) {
         foreach ($attachmentCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     if (!empty($attachmentInlineCollection)) {
         foreach ($attachmentInlineCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $textPart = new MimePart($email->getBodyPlainForSending());
     $textPart->type = 'text/plain';
     $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $textPart->charset = 'utf-8';
     if ($email->get('isHtml')) {
         $htmlPart = new MimePart($email->getBodyForSending());
         $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
         $htmlPart->type = 'text/html';
         $htmlPart->charset = 'utf-8';
     }
     if (!empty($attachmentPartList)) {
         $messageType = 'multipart/related';
         if ($email->get('isHtml')) {
             $content = new MimeMessage();
             $content->addPart($textPart);
             $content->addPart($htmlPart);
             $messageType = 'multipart/mixed';
             $contentPart = new MimePart($content->generateMessage());
             $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"';
             $body->addPart($contentPart);
         } else {
             $body->addPart($textPart);
         }
         foreach ($attachmentPartList as $attachmentPart) {
             $body->addPart($attachmentPart);
         }
     } else {
         if ($email->get('isHtml')) {
             $body->setParts(array($textPart, $htmlPart));
             $messageType = 'multipart/alternative';
         } else {
             $body = $email->getBodyPlainForSending();
             $messageType = 'text/plain';
         }
     }
     $message->setBody($body);
     if ($messageType == 'text/plain') {
         if ($message->getHeaders()->has('content-type')) {
             $message->getHeaders()->removeHeader('content-type');
         }
         $message->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
     } else {
         if (!$message->getHeaders()->has('content-type')) {
             $contentTypeHeader = new \Zend\Mail\Header\ContentType();
             $message->getHeaders()->addHeader($contentTypeHeader);
         }
         $message->getHeaders()->get('content-type')->setType($messageType);
     }
     $message->setEncoding('UTF-8');
     try {
         $rand = mt_rand(1000, 9999);
         if ($email->get('parentType') && $email->get('parentId')) {
             $messageId = '' . $email->get('parentType') . '/' . $email->get('parentId') . '/' . time() . '/' . $rand . '@espo';
         } else {
             $messageId = '' . md5($email->get('name')) . '/' . time() . '/' . $rand . '@espo';
         }
         if ($email->get('isSystem')) {
             $messageId .= '-system';
         }
         $messageIdHeader = new \Zend\Mail\Header\MessageId();
         $messageIdHeader->setId($messageId);
         $message->getHeaders()->addHeader($messageIdHeader);
         $this->transport->send($message);
         $email->set('messageId', '<' . $messageId . '>');
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }