Limit user to send emails from specific account only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theBuzzyCoder
    Senior Member
    • Feb 2018
    • 102

    Limit user to send emails from specific account only

    Hi tanya, yuri ,

    I am trying to achieve this.

    When a support team person click reply he should be able to send email from support@123.com email (group email) and when sales team person clicks reply it should be sales@123.com (group email).

    His/Her email which is specified in user should not show up at all when replying to email. Only group emails should show up. How to achieve this? It should be based on their default team.
    Last edited by theBuzzyCoder; 05-31-2018, 01:43 PM.
  • theBuzzyCoder
    Senior Member
    • Feb 2018
    • 102

    #2
    tanya

    Any updates on this query?

    Comment

    • tanya
      Senior Member
      • Jun 2014
      • 4308

      #3
      No changes, but you can do it.
      EspoCRM – Open Source CRM Application. Contribute to espocrm/espocrm development by creating an account on GitHub.

      EspoCRM – Open Source CRM Application. Contribute to espocrm/espocrm development by creating an account on GitHub.

      Comment

      • theBuzzyCoder
        Senior Member
        • Feb 2018
        • 102

        #4
        Hi tanya ,

        That works. Thanks.

        How can I get emails from group email account based on default teams only in these javascript files.

        Comment

        • theBuzzyCoder
          Senior Member
          • Feb 2018
          • 102

          #5
          Hi tanya

          This is what I want, see comments in code


          Code:
          Espo.define('custom:views/email/fields/compose-from-address', 'views/email/fields/compose-from-address', function (Dep) {
          
              return Dep.extend({
          
                  editTemplate: 'email/fields/compose-from-address/edit',
          
                  data: function () {
                      return _.extend({
                          list: this.list,
                          noSmtpMessage: this.translate('noSmtpSetup', 'messages', 'Email').replace('{link}', '<a href="#Preferences">'+this.translate('Preferences')+'</a>')
                      }, Dep.prototype.data.call(this));
                  },
          
                  setup: function () {
                      Dep.prototype.setup.call(this);
                      this.list = [];
          
                      var primaryEmailAddress = this.getUser().get('emailAddress');
          
                      var emailAddressList = this.getUser().get('emailAddressList') || [];
                      var defaultTeamId = this.getUser().get('defaultTeamId');
                      [B]// Load group email associated with default team ID[/B]
          
                      emailAddressList.forEach(function (item) {
                          if (item != primaryEmailAddress) {
                              this.list.push(item);
                          }
                      }, this);
          
                      this.list = _.uniq(this.list);
          
                      if (this.getConfig().get('outboundEmailIsShared') && this.getConfig().get('outboundEmailFromAddress')) {
                          var address = this.getConfig().get('outboundEmailFromAddress');
                          if (!~this.list.indexOf(address)) {
                              this.list.push(this.getConfig().get('outboundEmailFromAddress'));
                          }
                      }
                  },
              });
          
          });

          Comment

          • theBuzzyCoder
            Senior Member
            • Feb 2018
            • 102

            #6
            I was able to solve it by making an ajax request to InboundEmails. Let me know if there is a better way to do this.

            espocrm/client/custom/src/views/email/fields/compose-from-address.js


            PHP Code:
            Espo.define('custom:views/email/fields/compose-from-address', 'views/email/fields/compose-from-address', function (Dep) {
            
                return Dep.extend({
            
                    editTemplate: 'email/fields/compose-from-address/edit',
            
                    data: function () {
                        return _.extend({
                            list: this.list,
                            noSmtpMessage: this.translate('noSmtpSetup', 'messages', 'Email').replace('{link}', '<a href="#Preferences">'+this.translate('Preferences')+'</a>')
                        }, Dep.prototype.data.call(this));
                    },
            
                    setup: function () {
                        Dep.prototype.setup.call(this);
                        this.list = [];
            
                        var currentUser = this.getUser();
                        var primaryEmailAddress = currentUser.get('emailAddress');
            
                        // Below code is to prioritise the group Email Address that belongs to default Team of the user.
                        this.getPrimaryEmail();
            
                        var emailAddressList = currentUser.get('emailAddressList') || [];
                        emailAddressList.forEach(function (item) {
                            if (item != primaryEmailAddress) {
                                // Avoid including primary address.
                                this.list.push(item);
                            }
                        }, this);
            
                        this.list = _.uniq(this.list);
            
                        if (this.getConfig().get('outboundEmailIsShared') && this.getConfig().get('outboundEmailFromAddress')) {
                            var address = this.getConfig().get('outboundEmailFromAddress');
                            if (!~this.list.indexOf(address)) {
                                this.list.push(this.getConfig().get('outboundEmailFromAddress'));
                            }
                        }
                    },
            
                    getPrimaryEmail: function () {
                        $.ajax({
                            async: false,
                            url: 'InboundEmail',
                            data: {},
                            success: function (groupEmails) {
                                if (groupEmails.total > 0) {
                                    groupEmails.list.forEach(function (groupEmail) {
                                        this.checkEmailExists(groupEmail);
                                    }, this);
                                }
                            }.bind(this),
                        });
                    },
            
                    checkEmailExists: function (groupEmail) {
                        defaultTeamId = this.getUser().get('defaultTeamId');
                        $.ajax({
                            async: false,
                            url: 'InboundEmail/' + groupEmail.id,
                            data: {},
                            success: function (returnedGroupEmail) {
                                var index = returnedGroupEmail.teamsIds.findIndex(function (teamId) {
                                    return teamId == defaultTeamId;
                                });
                                if (index != -1) {
                                    this.list.push(returnedGroupEmail.emailAddress);
                                }
                            }.bind(this),
                        });
                    }
                });
            
            }); 
            

            Comment

            • theBuzzyCoder
              Senior Member
              • Feb 2018
              • 102

              #7
              Hi tanya ,

              I am getting 403 error for one of the users when I click on reply/compose email after writing this code. How can I resolve it?

              Code:
              Espo.ERROR: API [GET]:/:controller, Params:Array (     [controller] => InboundEmail ) , InputData:  -  [] []
              Espo.ERROR: Display Error: , Code: 403 URL: /api/v1/InboundEmail [] []
              Please note that I cannot give admin access to the user/agent.

              Is there a way to access/load inbound-email model without ajax request where I can get all the database information to execute this.getPrimaryEmail() this code as mentioned in the above code.
              Last edited by theBuzzyCoder; 06-11-2018, 06:26 AM.

              Comment

              • tanya
                Senior Member
                • Jun 2014
                • 4308

                #8
                You can add new action to the Email controller, where you return only inbound email, if exists.

                Comment

                • theBuzzyCoder
                  Senior Member
                  • Feb 2018
                  • 102

                  #9
                  Hi tanya ,

                  Can you please give me a sample code so that I can understand how it works and file paths.

                  Also in this.getUser() of views it returns emailAddressList field. Can you please tell me where is it coming from. Because there isn't a field with same name.

                  Comment

                  • tanya
                    Senior Member
                    • Jun 2014
                    • 4308

                    #10
                    url in post ajax request "Email/action/inboundEmail"

                    custom/Espo/Custom/Controllers/Email.php

                    PHP Code:
                    <?php
                    
                    namespace Espo\Custom\Controllers;
                    
                    class Email extends \Espo\Controllers\Email
                    {
                        public function postActionInboundEmail($params, $data, $request)
                        {
                            return ["testEmail@emao.sds"];
                        }
                    }

                    Comment

                    • sschneider
                      Junior Member
                      • Jul 2023
                      • 3

                      #11
                      Hey theBuzzyCoder - do you found a workaround for your problem?
                      I am also searching for a solution on this.
                      cheers,Sven

                      Comment

                      Working...