Дублирование уведомлений по электронной почте SuiteCrm - PullRequest
1 голос
/ 06 мая 2019

Я создал модуль в suitecrm, который позволит пользователям создавать или просматривать nc case.Когда создается случай nc, уведомление по электронной почте отправляется лицу, назначенному этой переменной $ rev_email, однако при отправке электронного письма оно дублируется в папке входящих сообщений пользователя, когда администратор должен получить его только один раз.

function createCaseEmail(&$email,$rev_email,$subject,$bean,$xtpl){
        /* Set Email Subject */
        $email->Subject=$subject;

    /* Get NC Case Number */
    $case_number = $bean ->case_number;
    $xtpl->assign('Case_Number', $case_number );

    /* Get NC Subject */
    $xtpl->assign('Case_Subject', $bean->name);

    /* Get Case Description */
    $xtpl->assign('Case_Desc', $bean->description_c);



    /* Create email message using email template and data above */
    $xtpl->parse('main');               
    $email->Body = from_html($xtpl->text('main'));


    return $email;
}
class cases_email_notification
{
function send_notification($bean, $event, $arguments)
{

    /*Get sugar email engine*/

    $email = new SugarPHPMailer();
    $email->From = 'it@gmail.com';
    $email->FromName ='SuiteCRM';

    $rev_email = 'admin@gmail.com';

    /* Get sugar template engine */
    $xtpl = new XTemplate("XTemplate/CasesEmailTemplate.html");

        /*GEt the URL for the NC Case */
        $url =  $GLOBALS['sugar_config']['site_url'].'index.php?module=Cases&action=DetailView&record='.$bean->id; 
        $xtpl -> assign('URL', $url);


        /* Get Case ID */
        $id = $bean->getFieldValue('id');

        if(empty($bean->fetched_row['id'])){

            $email=createCaseEmail($email,$rev_email,'New Case Email Notification',$bean,$xtpl);                

            /* Send email to Production Department */
            $email->addAddress($rev_email);
        }
        //Send email
        $email->isHTML(true);
        $email->prepForOutbound();
        $email->setMailerForSystem();

        if(!$email->Send()){
            $GLOBALS['log']->info("Could not send notification email: ". $email->ErrorInfo);
        }       
}

}

1 Ответ

0 голосов

Проблема дублирования в ловушке before_save очень распространена.Мы хотим, чтобы логический хук запускался только в первый раз.Чтобы убедиться в этом, мы можем использовать статическую переменную с именем $already_ran.

class cases_email_notification
{
  static $already_ran = false;

  function send_notification($bean, $event, $arguments)
  {
        $GLOBALS['log']->info("send_notification start!"
        if(self::$already_ran == true) return;
        self::$already_ran = true;

        /*Get sugar email engine*/
        $email = new SugarPHPMailer();
        $email->setMailerForSystem();
        $email->From = 'it@gmail.com';
        $email->FromName ='SuiteCRM';
        /* Set Email Subject */
        $email->Subject=$subject;          
        $email->Body = ''; 
        $email->prepForOutbound();
        /* Send email to Production Department */
        $email->addAddress('admin@gmail.com');

        if(!$email->Send()){
            $GLOBALS['log']->info("Could not send notification email: ". $email->ErrorInfo);
        } else {
            $GLOBALS['log']->info("I send notification email!!!"
        }
  }       
}
...