Jump to content

Recommended Posts

Posted

It would be nice to prevent emails being sent from system.  In our particular case it is because we have some contacts that have a valid email address stored, but we know for FACT the email address is NOT a working email address.  Blesta sends emails to these users not caring about the bounce-backs that will occur due to no such mailbox.  Other plugin developers may have their own reason to prevent emails being sent. 

In reviewing the event triggers in app/models/emails.php, it would be very simple to add a variable that plugins can set to prevent email sending, or leave alone to allow emails to flow as normal.  It involves changing this block of code currently in /app/models/emails.php, line #558:

// Trigger the event
$eventFactory = $this->getFromContainer('util.events');
$eventListener = $eventFactory->listener();
$eventListener->register('Emails.send');
$tags = array_merge(
   $tags,
  (array) $eventListener->trigger(
      $eventFactory->event('Emails.send', compact('action', 'options', 'tags'))
  )->getReturnValue()
);

to this block of code (not much difference at all):

// Trigger the event
$abort = null;
$eventFactory = $this->getFromContainer('util.events');
$eventListener = $eventFactory->listener();
$eventListener->register('Emails.send');
$event = $eventFactory->event('Emails.send', compact('action', 'options', 'tags', 'abort'));
$event_result = $eventListener->trigger($event)->getReturnValue();
if( $event_result['abort'] ) {
	$this->Input->setErrors([
		'email' => [
			'sending_restricted' => $event_result['abort']
		]
	]);				
	return '';
}
if( is_array($event_result['tags']) ) {
	$tags = array_merge($tags, $event_result['tags']);
}

The same code could also replace the current code for Emails.sendCustom event trigger.  Plugins could theoritecally define their reason for stopping the email by defining a string error message in the abort parameter.  I believe the abort variable would need to be handled as a possible array upon return of event in case multiple plugins define reasons to abort sending.

 

  • Chance changed the title to Allow Email Sending to be Aborted via Event Handler

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...