Announcement

Collapse
No announcement yet.

Custom formula function sends email two times

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom formula function sends email two times

    Hi

    After this thread I wrote a custom formula function which allows me to send an email using a specific user:
    Code:
    <?php
    namespace Espo\Custom\Core\Formula\Functions\ExtGroup\EmailGroup;
    
    use Espo\Core\Formula\{
       Functions\BaseFunction,
       ArgumentList,
    };
    
    use Espo\Core\Di;
    
    class SendFromUserType extends BaseFunction implements
       Di\EntityManagerAware,
       Di\ServiceFactoryAware,
       Di\ConfigAware
    {
       use Di\EntityManagerSetter;
       use Di\ServiceFactorySetter;
       use Di\ConfigSetter;
    
       public function process(ArgumentList $args)
       {
          if (count($args) < 2) {
             $this->throwTooFewArguments(2);
          }
    
          $args = $this->evaluate($args);
    
          $emailId = $args[0];
          $userId = $args[1];
    
          if (!$emailId || !is_string($emailId)) {
             $this->throwBadArgumentType(1, 'string');
          }
    
          if (!$userId || !is_string($userId)) {
             $this->throwBadArgumentType(1, 'string');
          }
    
          $em = $this->entityManager;
    
          $email = $em->getEntity('Email', $emailId);
          $user = $em->getEntity('User', $userId);
    
          if (!$email) {
             $this->log("Email '{$emailId}' does not exist.");
             return false;
          }
          if (!$user) {
             $this->log("User '{$userId}' does not exist.");
             return false;
          }
    
          $status = $email->get('status');
    
          if ($status && in_array($status, ['Sent'])) {
             $this->log("Can't send email that has 'Sent' status.");
             return false;
          }
    
          $service = $this->serviceFactory->create('Email');
          $service->loadAdditionalFields($email);
    
          $toSave = false;
    
          if ($status !== 'Sending') {
             $email->set('status', 'Sending');
             $toSave = true;
          }
    
          if (!$email->get('from')) {
             $from = $this->config->get('outboundEmailFromAddress');
             if ($from) {
                $email->set('from', $from);
                $toSave = true;
             }
          }
    
          if ($toSave) {
             $em->saveEntity($email, [
                'modifiedById' => 'system',
                'silent' => true,
             ]);
          }
    
          try {
             $service->sendEntity($email,$user);
          } catch (\Exception $e) {
             $message = $e->getMessage();
             $this->log("Error while sending. Message: {$message}." , 'error');
             return false;
          }
    
          return true;
       }
    }
    But now when I call the custom function in a formular, the email gets sent twice:
    - One time it gets sent using the correct user and every user-based variable in the email-template get filled in according to the user attributes.
    - The other time the email gets sent from the correct user, but the user variables in the email-template get filled in using the System-user attributes.

    Has anybody in here an idea, why the email gets sent two times?

  • #2
    Does anybody see the error here?

    Comment


    • esforim
      esforim commented
      Editing a comment
      Can't see it (I don't have programming skill). I would suggest to try two idea that may help you 'debug':

      Switch the code around so that the "2nd" email get send "1st", see if you still get the issue.

      Second way to test is to just make the code send only the 'second' email and see it work.

  • #3
    Hi esforim

    The problem is that the code should only send the email one time... But it gets sent two times, and I don't know why.

    Comment


    • esforim
      esforim commented
      Editing a comment
      I see, I don't know too much but from reading the code you provide I see lots of "if", so as long as all these "if" is true then it will get send for every true statement isn't it?

      From my "Excel" coding skill, wouldn't the correct way to write is, "ifelse". That would be my only guess, you could try to change the code slightly a little with a additional words; for example adding "test1" and "test2", maybe it will reveal which area of the code you that causing it.

      Also, are you sure two email get send? And it not just "saving" two copy in your EspoCRM?

      Perhaps simplifying down your code will also help, just have the send function without those fancy feature.
      Last edited by esforim; 01-18-2021, 01:24 AM.
Working...
X