From d30d54762ca50f652ac701f8cf277ac94cd2e3d2 Mon Sep 17 00:00:00 2001
From: Erik Roelofs <e.roelofs@samson-it.nl>
Date: Wed, 25 Jul 2012 12:20:32 +0200
Subject: [PATCH 2/2] Addding an event to email_api that allows Plugins to
 overrule the Mantis mail sender.

---
 core/email_api.php  | 38 +++++++++++++++++++++++++++-----------
 core/events_inc.php |  1 +
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/core/email_api.php b/core/email_api.php
index ed70dc7..71a3ece 100755
--- a/core/email_api.php
+++ b/core/email_api.php
@@ -336,7 +336,7 @@ function email_collect_recipients( $p_bug_id, $p_notify_type, $p_extra_user_ids_
 	user_cache_array_rows( $t_user_ids );
 	user_pref_cache_array_rows( $t_user_ids );
 	user_pref_cache_array_rows( $t_user_ids, $t_bug->project_id );
-
+    
 	# Check whether users should receive the emails
 	# and put email address to $t_recipients[user_id]
 	foreach( $t_recipients as $t_id => $t_ignore ) {
@@ -408,7 +408,6 @@ function email_collect_recipients( $p_bug_id, $p_notify_type, $p_extra_user_ids_
 			$t_final_recipients[$t_id] = $t_email;
 		}
 	}
-
 	return $t_final_recipients;
 }
 
@@ -566,15 +565,32 @@ function email_generic( $p_bug_id, $p_notify_type, $p_message_id = null, $p_head
 		if( is_array( $t_recipients ) ) {
 			# send email to every recipient
 			foreach( $t_recipients as $t_user_id => $t_user_email ) {
-				log_event( LOG_EMAIL, sprintf( "Issue = #%d, Type = %s, Msg = '%s', User = @U%d, Email = '%s'.", $p_bug_id, $p_notify_type, $p_message_id, $t_user_id, $t_user_email ) );
-
-				# load (push) user language here as build_visible_bug_data assumes current language
-				lang_push( user_pref_get_language( $t_user_id, $t_project_id ) );
-
-				$t_visible_bug_data = email_build_visible_bug_data( $t_user_id, $p_bug_id, $p_message_id );
-				$t_ok = email_bug_info_to_one_user( $t_visible_bug_data, $p_message_id, $t_project_id, $t_user_id, $p_header_optional_params ) && $t_ok;
-
-				lang_pop();
+                
+                # check if any plugin(s) wish to handle the mail that is going to be sent
+                $t_completed = false;
+                $t_overrides = event_signal( 'EVENT_NOTIFY_MAIL_SENDING', array( $p_bug_id, $p_notify_type, $t_user_id ) );
+                foreach( $t_overrides as $t_plugin => $t_callbacks ) {
+                    foreach ( $t_callbacks as $t_callback => $t_override ) {
+                        if ( $t_override ) {
+                            log_event( LOG_EMAIL_RECIPIENT, sprintf( 'Issue = #%d, %s plugin handled email for user @U%d', $p_bug_id, $t_plugin, $t_id ) );
+                            $t_completed = true;
+                        }
+                    }
+                }
+                if ( $t_completed ) {
+                    # this mail was handled by a plugin
+                }
+                
+                # use the default mailer
+                log_event( LOG_EMAIL, sprintf( "Issue = #%d, Type = %s, Msg = '%s', User = @U%d, Email = '%s'.", $p_bug_id, $p_notify_type, $p_message_id, $t_user_id, $t_user_email ) );
+
+                # load (push) user language here as build_visible_bug_data assumes current language
+                lang_push( user_pref_get_language( $t_user_id, $t_project_id ) );
+
+                $t_visible_bug_data = email_build_visible_bug_data( $t_user_id, $p_bug_id, $p_message_id );
+                $t_ok = email_bug_info_to_one_user( $t_visible_bug_data, $p_message_id, $t_project_id, $t_user_id, $p_header_optional_params ) && $t_ok;
+
+                lang_pop();
 			}
 		}
 
diff --git a/core/events_inc.php b/core/events_inc.php
index 292ca1d..b373376 100755
--- a/core/events_inc.php
+++ b/core/events_inc.php
@@ -124,4 +124,5 @@ event_declare_many( array(
     'PRINT_ATTACHMENT'          => EVENT_TYPE_EXECUTE,    
     'EVENT_BLOCK_ATTACHMENT'    => EVENT_TYPE_CHAIN,    
     'EVENT_BUG_BUTTONS'         => EVENT_TYPE_EXECUTE,    
+    'EVENT_NOTIFY_MAIL_SENDING' => EVENT_TYPE_DEFAULT,
 ) );
-- 
1.7.11.msysgit.0

