Jump to content

Allow Email Sending to be Aborted via Event Handler


Chance

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...