diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index af180a20b8..a43d4d1fe9 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -191,12 +191,17 @@ class Sender $body = new MimeMessage; $parts = array(); - - $bodyPart = new MimePart($email->get('body')); + if ($email->get('isHtml')) { + $bodyPart = new MimePart($email->get('body')); $bodyPart->type = 'text/html'; - } else { + } else { + if ($email->get('bodyPlain')) { + $bodyPart = new MimePart($email->get('bodyPlain')); + } else { + $bodyPart = new MimePart($email->get('body')); + } $bodyPart->type = 'text/plain'; } diff --git a/application/Espo/Entities/Preferences.php b/application/Espo/Entities/Preferences.php index cd79b5165f..0afa6d4049 100644 --- a/application/Espo/Entities/Preferences.php +++ b/application/Espo/Entities/Preferences.php @@ -18,7 +18,7 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Entities; @@ -27,11 +27,11 @@ class Preferences extends \Espo\Core\ORM\Entity public function getSmtpParams() { - $smtpParams = array(); + $smtpParams = array(); $smtpParams['server'] = $this->get('smtpServer'); if ($smtpParams['server']) { $smtpParams['port'] = $this->get('smtpPort'); - $smtpParams['server'] = $this->get('smtpServer'); + $smtpParams['server'] = $this->get('smtpServer'); $smtpParams['auth'] = $this->get('smtpAuth'); $smtpParams['security'] = $this->get('smtpSecurity'); $smtpParams['username'] = $this->get('smtpUsername'); @@ -40,7 +40,6 @@ class Preferences extends \Espo\Core\ORM\Entity } else { return false; } - } - + } } diff --git a/application/Espo/Modules/Crm/Controllers/Call.php b/application/Espo/Modules/Crm/Controllers/Call.php index e83b989507..d664c225e6 100644 --- a/application/Espo/Modules/Crm/Controllers/Call.php +++ b/application/Espo/Modules/Crm/Controllers/Call.php @@ -22,7 +22,30 @@ namespace Espo\Modules\Crm\Controllers; +use \Espo\Core\Exceptions\Forbidden; +use \Espo\Core\Exceptions\BadRequest; +use \Espo\Core\Exceptions\NotFound; + class Call extends \Espo\Core\Controllers\Record { + public function actionSendInvitations($params, $data) + { + if (empty($data['id'])) { + throw new BadRequest(); + } + + $entity = $this->getRecordService()->getEntity($data['id']); + + if (!$entity) { + throw new NotFound(); + } + + if (!$this->getAcl()->check($entity, 'edit')) { + throw new Forbidden(); + } + + return $this->getRecordService()->sendInvitations($entity); + } + } diff --git a/application/Espo/Modules/Crm/Controllers/Meeting.php b/application/Espo/Modules/Crm/Controllers/Meeting.php index 49ba17da59..d3aa4ce1aa 100644 --- a/application/Espo/Modules/Crm/Controllers/Meeting.php +++ b/application/Espo/Modules/Crm/Controllers/Meeting.php @@ -22,7 +22,30 @@ namespace Espo\Modules\Crm\Controllers; +use \Espo\Core\Exceptions\Forbidden; +use \Espo\Core\Exceptions\BadRequest; +use \Espo\Core\Exceptions\NotFound; + class Meeting extends \Espo\Core\Controllers\Record -{ +{ + + public function actionSendInvitations($params, $data) + { + if (empty($data['id'])) { + throw new BadRequest(); + } + + $entity = $this->getRecordService()->getEntity($data['id']); + + if (!$entity) { + throw new NotFound(); + } + + if (!$this->getAcl()->check($entity, 'edit')) { + throw new Forbidden(); + } + + return $this->getRecordService()->sendInvitations($entity); + } } diff --git a/application/Espo/Modules/Crm/Resources/templates/InvitationBody.tpl b/application/Espo/Modules/Crm/Resources/templates/InvitationBody.tpl new file mode 100644 index 0000000000..5c6bf8f329 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/templates/InvitationBody.tpl @@ -0,0 +1,5 @@ +

Subject: {name}

+

Date & time: {dateStart}

+

+Accept, Decline +

diff --git a/application/Espo/Modules/Crm/Resources/templates/InvitationSubject.tpl b/application/Espo/Modules/Crm/Resources/templates/InvitationSubject.tpl new file mode 100644 index 0000000000..5355701f83 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/templates/InvitationSubject.tpl @@ -0,0 +1 @@ +Invitation to the {activityType} '{name}' diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index ab81405909..242e95a35f 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -29,6 +29,86 @@ use \Espo\Core\Exceptions\Forbidden; class Meeting extends \Espo\Services\Record { + protected function init() + { + $this->dependencies[] = 'mailSender'; + $this->dependencies[] = 'preferences'; + $this->dependencies[] = 'i18n'; + } + + protected function getMailSender() + { + return $this->injections['mailSender']; + } + + protected function getPreferences() + { + return $this->injections['preferences']; + } + + protected function getI18n() + { + return $this->injections['i18n']; + } + + + protected function parseInvitationTemplate($contents, $entity, $invitee = null) + { + $contents = str_replace('{name}', $entity->get('name'), $contents); + $contents = str_replace('{activityType}', strtolower($this->getI18n()->translate($entity->getEntityName(), 'scopeNames')), $contents); + $contents = str_replace('{dateStart}', $entity->get('dateStart'), $contents); + return $contents; + } + + protected function sendInvitation(Entity $entity, Entity $invitee) + { + $email = $this->getEntityManager()->getEntity('Email'); + $email->set('to', $invitee->get('emailAddress')); + + $subjectTplFileName = 'custom/Espo/Custom/Resources/templates/InvitationSubject.tpl'; + if (!file_exists($subjectTplFileName)) { + $subjectTplFileName = 'application/Espo/Modules/Crm/Resources/templates/InvitationSubject.tpl'; + } + $subjectTpl = file_get_contents($subjectTplFileName); + + $bodyTplFileName = 'custom/Espo/Custom/Resources/templates/InvitationBody.tpl'; + if (!file_exists($bodyTplFileName)) { + $bodyTplFileName = 'application/Espo/Modules/Crm/Resources/templates/InvitationBody.tpl'; + } + $bodyTpl = file_get_contents($bodyTplFileName); + + $subject = $this->parseInvitationTemplate($subjectTpl, $entity, $invitee); + $body = $this->parseInvitationTemplate($bodyTpl, $entity, $invitee); + + $email->set('subject', $subject); + $email->set('body', $body); + $email->set('isHtml', true); + + $emailSender = $this->getMailSender(); + + $emailSender->send($email); + } + + public function sendInvitations(Entity $entity) + { + $users = $entity->get('users'); + foreach ($users as $user) { + $this->sendInvitation($entity, $user); + } + + $contacts = $entity->get('contacts'); + foreach ($contacts as $contact) { + $this->sendInvitation($entity, $contact); + } + + $leads = $entity->get('leads'); + foreach ($leads as $lead) { + $this->sendInvitation($entity, $lead); + } + + return true; + } + protected function storeEntity(Entity $entity) { $assignedUserId = $entity->get('assignedUserId'); diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index 8cb127a5b1..2c6cdb8701 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -18,7 +18,7 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Services; @@ -28,90 +28,90 @@ use \Espo\Core\Exceptions\Error; class Email extends Record { - + protected function init() { $this->dependencies[] = 'mailSender'; $this->dependencies[] = 'preferences'; } - + protected function getMailSender() { return $this->injections['mailSender']; } - + protected function getPreferences() { return $this->injections['preferences']; - } - + } + public function createEntity($data) { $entity = parent::createEntity($data); - + if ($entity && $entity->get('status') == 'Sending') { $emailSender = $this->getMailSender(); - + if (strtolower($this->getUser()->get('emailAddress')) == strtolower($entity->get('from'))) { - $smtpParams = $this->getPreferences()->getSmtpParams(); + $smtpParams = $this->getPreferences()->getSmtpParams(); if ($smtpParams) { - $smtpParams['fromName'] = $this->getUser()->get('name'); + $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.'); + throw new Error('Can not use system smtp. outboundEmailIsShared is false.'); } - } - - $emailSender->send($entity); - + } + + $emailSender->send($entity); + $this->getEntityManager()->saveEntity($entity); } - + return $entity; } - + public function getEntity($id = null) { $entity = parent::getEntity($id); if (!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'); + + $names = $entity->get('toEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } - $entity->set('to', implode('; ', $arr)); + $entity->set('to', implode('; ', $arr)); } - - $names = $entity->get('ccEmailAddressesNames'); + + $names = $entity->get('ccEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } - $entity->set('cc', implode('; ', $arr)); + $entity->set('cc', implode('; ', $arr)); } - - $names = $entity->get('bccEmailAddressesNames'); + + $names = $entity->get('bccEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } - $entity->set('bcc', implode('; ', $arr)); - } - + $entity->set('bcc', implode('; ', $arr)); + } + } return $entity; }