Unable to Send Email Notification Using Logic

I’m using logic hook in order to send email notifications. But each time the page redirects, it redirects to index page rather the detail view page.

$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(1, 'Create PI email object', 'custom/Extension/modules/PI_ProductInvestigation/Ext/pi_email_notification.php','pi_email_notification', 'emailnotification'); 

–File: pi_email_notification.php’–

function createPIEmail(&$email,$rev_email,$rev_email1,$subject,$bean,$xtpl){
	/* Set Email Subject */
	$email->Subject=$subject;
	
	/* Get PI Number */
	$pi_number = $bean ->pinum_c;
	$xtpl->assign('PI_Number', $pi_number);
	
	/* Get Product Name */
	$xtpl->assign('Product_Name', $bean->name);
	
	/* Get Product Type */
	$xtpl->assign('Product_Type', $bean->name);
	
	/* Get Batch # */
	$xtpl->assign('Batch_Number', $bean->name);
	
	/* Get PI Description */
	$xtpl->assign('PI_Desc', $bean->description);
	
	/* Get Cases/Case  */
	$cases_to_pi = $bean->getFieldValue('pi_productinvestigation_cases_1_name');
	$xtpl->assign('Cases', $cases_to_pi);

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


class pi_email_notification
{
	static $already_ran = false;

	
	function emailnotification($bean, $event, $arguments)
	{
		$GLOBALS['log']->info("emailnotification start!");
		if(self::$already_ran == true) return;
		self::$already_ran = true;
		
		/*Get sugar email engine*/
	   
		$email = new SugarPHPMailer();
		
		$email->From = 'it_proc@tradewindscitrus.com';
		$email->FromName ='SuiteCRM';
		
		
		$rev_email = 'sue-ann.brown@tradewindscitrus.com'; 
		$rev_email1 = 'sueannbrown@ymail.com'; 
		
		/* Get sugar template engine */
		$xtpl = new XTemplate("XTemplate/PIEmailTemplate.html");
			
			/*Get the URL for the PI */
			$url =  $GLOBALS['sugar_config']['site_url'].'index.php?module=PI_ProductInvestigation&action=DetailView&record='.$bean->id; 
			$xtpl -> assign('URL', $url);

			/* Get PI ID */
			$id = $bean->getFieldValue('id');
			
			if(empty($bean->fetched_row['id'])){
				
				$email=createPIEmail($email,$rev_email,$rev_email1,'New Product Investigation Email Notification',$bean,$xtpl);				
				
				/* Send email to Production Department */
				
				$email->addAddress($rev_email);
				$email->addAddress($rev_email1);
			}
			//Send email
			$email->isHTML(true);
			$email->prepForOutbound();
			$email->setMailerForSystem();
			
			if(!$email->Send()){
				$GLOBALS['log']->info("Could not send notification email: ". $email->ErrorInfo);
			}	
			else {
			$GLOBALS['log']->info("I send notification email!!!". $email->ErrorInfo);
		}
			
	}

I don’t know what’s wrong but I see you’re mixing two separate mechanisms: logic hooks and Extension framework.

You should place your hook file outside the directories that the Extension framework scans (custom/Extension).

Try it, run a QR&R, and see if that changes anything.

And make sure you check your logs for any errors at the time of the redirect. See also your browser console’s network tab and JS errors tab.

This is the location for the hook file \custom\modules\PI_ProductInvestigation

And its the logic hook causing the issue; because if I remove it, it redirects to the detail view page.

Also I did rebuild and it is still the same issue.

And the logs?

Are those the exact contents of the file? Did you forget to add

<?php

at the beginning?

<?php yes it is there. I’m not seeing anything the logs

Well there’s nothing obvious I can see from here either :slight_smile:

You’ll need to troubleshoot, or debug with XDEBUG, see how far it gets in your procedure, try simpler procedures to see which part is breaking your code, etc.

I tried commenting out the details of the function and nothing happens that’s why I suspect is the ```
$hook_array[‘after_save’][] = Array(1, ‘Create PI email object’, ‘custom/Extension/modules/PI_ProductInvestigation/Ext/pi_email_notification.php’,‘pi_email_notification’, ‘emailnotification’);

I thought you said you had changed the path.

Your logic hook definition must point to the file where the hook code is. And that should not be under custom/Extension.

The hook file location: \custom\modules\PI_ProductInvestigation
The php file ( pi_email_notification.php) location: \custom\Extension\modules\PI_ProductInvestigation\Ext

So the pi_email_notification php file in the wrong location?

Funny thing is that I’ve used this and it worked before so now I’m not getting why its not working

Yes change the location of the PHP file.

You probably ran a QR&R meanwhile, so the file you have under custom/Extension got picked up by the Extension Framework and got copied to custom/Ext/ and is running at a different moment.

Now your hook is probably complaining that the class is already defined or something. Your PHP error_log (the one defined in php.ini) is probably telling you this.

The Line
$hook_array[‘after_save’] = Array(1, ‘Create PI email object’, ‘custom/Extension/modules/PI_ProductInvestigation/Ext/pi_email_notification.php’,‘pi_email_notification’, ‘emailnotification’);
may be different
$hook_array[‘after_save’] = Array(1, ‘Create PI email object’, ’ /custom/modules/PI_ProductInvestigation’,‘pi_email_notification’, ‘emailnotification’);

So the hook and pi_email_notification.php should be in the same folder

this code should be located in dir custom/Extension/modules/PI_ProductInvestigation/Ext/any_name.php

    $hook_array['after_save'] = Array(); 
    $hook_array['after_save'][] = Array(1, 'Create PI email object', 'custom/modules/path_from_hook.php,'pi_email_notification', 'emailnotification');

in custom/modules/path_from_hook.php should be class pi_email_notification.Tthis class sould be have method emailnotification.

1 Like

Okay thanks for your help

Thank you was finally able to get it done