Modifying inbound email

I am trying to manipulate inbound email (on group email) using a before-save logic hook. The manipulation logic seems to work (I have logs that show successful replacement of text) but is not saved to database, the original text is what shows up in the database.

  • Hi Ishani,

    Can you provide a portion of the code you are using? It may also help to see expected results versus the actual results.

    Thanks!

    -Alan

  • Thanks Alan,

    I am working on Sugar7.5. and have written a before-save logic hook on Emails module

       $hook_version = 1;

        $hook_array = array();

        $hook_array['before_save'] = array();

        $hook_array['before_save'][] = array(

            1,

            'Manipulate email content',

            'custom/modules/Emails/inboundEmailsLogic.php',

            'inboundEmailsLogic',

            'emailTrim'

        );

    And I am trying a simple test case of replacing a 'Hi' by 'Hello' (This of course is not the actual requirement)

    <?php

    class inboundEmailsLogic {

        function emailTrim($bean, $event, $arguments) {

            $bean->description_html = preg_replace('/Hi/s', 'Hello', $bean->description_html);

            $GLOBALS['log']->fatal($event.'Hook fired--inboundEmailsLogic--'.$bean->id.'--'.$bean->description_html);   

            //$this->emailNotify($bean, $event, $arguments);

        }

        function emailNotify($bean, $event, $arguments) {

            $emailObj = new Email();

            $defaults = $emailObj->getSystemDefaultEmail();

            $mail = new SugarPHPMailer();

            $mail->setMailerForSystem();

            $mail->From = $defaults['email'];

            $mail->FromName = $defaults['name'];

            $mail->Subject = "Inbound email from someone";

            $mail->Body = from_html("After email trimming ---- " . $bean->description_html);

            $mail->prepForOutbound();

            $mail->AddAddress('****@gmail.com');

            if($mail->Send())

            {

                //return true for completed

                return true;

            }

        }

    }

    ?>

    The log shows the modified content. And if I uncomment the emailNotify function call, it send out an email correctly with the modified content.

    I also tried using an '&' like

    emailTrim(&$bean, $event, $arguments)

    Im not sure if that is required but that doesn't work either.

  • Hi Ishani,

    I was able to replicate this issue, but was not able to determine why it wasn't working. However, I did find a workaround! If you change it to an after_save logic hook, you can do this:

    function after_save_method($bean, $event, $arguments) {
      if(preg_match('/Hi/s', $bean->description_html) == 1) {
        $bean->description_html = preg_replace('/Hi/s', 'Hello', $bean->description_html);
        $bean->save();
      }
    }
    

    I hope that helps!

    -Alan

  • Hi Alan,

    Great.. that worked!! Straightforward yet tricky. That also takes care of execution getting into an infinite loop.

    Just that the actual case is not as simple as replacing Hi by Hello. Its a sequence of preg_match and pre_replace which will add to the time complexity.

    But this still works very well. Thanks Alan.

    Ishani