 README.txt              |  5 ++-
 reroute_email.admin.inc |  2 +-
 reroute_email.module    | 15 ++++----
 reroute_email.test      | 91 ++++++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 93 insertions(+), 20 deletions(-)

diff --git a/README.txt b/README.txt
index 6ae429f..513d1e4 100644
--- a/README.txt
+++ b/README.txt
@@ -31,7 +31,10 @@ To enable this module do the following:
 1. Go to Admin -> Modules, and enable reroute email.
 
 2. Go to Admin -> Configuration -> Development -> Reroute email, and enter an
-   email address to route all email to.
+   email address to route all email to. If the field is empty and no value is
+   provided for rerouted email addresses, all outgoing emails would be aborted
+   and recorded in the recent log entries, with a full dump of the email
+   variables, which could provide an additional debugging method.
 
 Tips and Tricks
 ---------------
diff --git a/reroute_email.admin.inc b/reroute_email.admin.inc
index fae415b..b82aec8 100644
--- a/reroute_email.admin.inc
+++ b/reroute_email.admin.inc
@@ -19,7 +19,7 @@ function reroute_email_settings() {
     '#type'          => 'textfield',
     '#title'         => t('Email addresses'),
     '#default_value' => variable_get(REROUTE_EMAIL_ADDRESS, variable_get('site_mail', ini_get('sendmail_from'))),
-    '#description'   => t('Provide a space, comma, or semicolon-delimited list of email addresses to pass through. Every destination email address which is not on this list will be rerouted to the first address on the list.'),
+    '#description'   => t('Provide a space, comma, or semicolon-delimited list of email addresses to pass through. Every destination email address which is not on this list will be rerouted to the first address on the list.<br/> If the field is empty and no value is provided, <strong>all outgoing emails would be aborted</strong> and the email would be recorded in the <a href="@dblog">recent log entries</a>.', array('@dblog' => url('admin/reports/dblog'))),
     '#states' => array(
       'visible' => array(':input[name=reroute_email_enable]' => array('checked' => TRUE)),
     ),
diff --git a/reroute_email.module b/reroute_email.module
index 5f1d5f2..273309a 100644
--- a/reroute_email.module
+++ b/reroute_email.module
@@ -70,11 +70,6 @@ function reroute_email_mail_alter(&$message) {
   if (variable_get(REROUTE_EMAIL_ENABLE, 0)) {
     global $base_url;
 
-    if (!variable_get(REROUTE_EMAIL_ADDRESS, '')) {
-      // If email address not in settings, then do nothing.
-      return;
-    }
-
     if (empty($message)) {
       return;
     }
@@ -119,7 +114,7 @@ function reroute_email_mail_alter(&$message) {
     if (!in_array($to, $addresslist)) {
       // Not on the list, so reroute to the first address in the list.
       $message['headers']['X-Rerouted-Original-To'] = $to;
-      $message['to'] = $addresslist[0];
+      $message['to'] = !empty($addresslist[0]) ? $addresslist[0] : t('[No reroute email address configured]');
 
       if (variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1)) {
         // Format a message to show at the top.
@@ -141,6 +136,14 @@ function reroute_email_mail_alter(&$message) {
         }
       }
     }
+    // Abort sending of the email only if the address is an empty string.
+    if (variable_get(REROUTE_EMAIL_ADDRESS, NULL) === '') {
+      $message['send'] = FALSE;
+      // Record a variable dump of the email in the recent log entries.
+      watchdog('reroute_email', 'Aborted email sending for <em>@message_id</em>. <br/>Detailed email data: Array $message <pre>@message</pre>', array('@message_id' => $message['id'], '@message' => print_r($message, TRUE)));
+      // Let users know email has been aborted, but logged.
+      drupal_set_message(t('<em>@message_id</em> was aborted by reroute email, please check the <a href="@dblog">recent log entries</a> for complete details on the rerouted email.', array('@message_id' => $message['id'], '@dblog' => url('admin/reports/dblog'))));
+    }
   }
 }
 
diff --git a/reroute_email.test b/reroute_email.test
index 1393f80..777e614 100644
--- a/reroute_email.test
+++ b/reroute_email.test
@@ -49,8 +49,8 @@ class RerouteEmailTestCase extends DrupalWebTestCase {
     parent::setUp($modules);
 
     // Authenticate test user.
-    $this->admin_user = $this->drupalCreateUser($this->permissions);
-    $this->drupalLogin($this->admin_user);
+    $this->adminUser = $this->drupalCreateUser($this->permissions);
+    $this->drupalLogin($this->adminUser);
   }
 
   /**
@@ -389,7 +389,7 @@ class RerouteEmailSpecialTestCase extends RerouteEmailTestCase {
 }
 
 /**
- * Test default reroute destination email address when it is not configured.
+ * Test default reroute destination email address when it is empty or unset.
  */
 class RerouteEmailDefaultAddressTestCase extends RerouteEmailTestCase {
 
@@ -398,29 +398,55 @@ class RerouteEmailDefaultAddressTestCase extends RerouteEmailTestCase {
    */
   public static function getInfo() {
     return array(
-      'name' => 'Default reroute destination email address',
+      'name' => 'Default or empty reroute destination email address',
       'description' => "When reroute email addresses field is not configured, attempt to use the site email address, otherwise use sendmail_from system variable.",
       'group' => 'Reroute Email',
     );
   }
 
   /**
-   * Test default destination address is set to site_mail or sendmail_from.
+   * Enable modules and create user with specific permissions.
+   */
+  public function setUp() {
+    // Add more permissions to access recent log messages in test.
+    $this->permissions[] = 'access site reports';
+    parent::setUp();
+  }
+
+  /**
+   * Test reroute email address is set to site_mail, sendmail_from or empty.
    *
    * When reroute email addresses field is not configured and settings haven't
    * been configured yet, check if the site email address or the sendmail_from
-   * system variable are properly used a fallbacks.
+   * system variable are properly used as fallbacks. Additionally, check that
+   * emails are aborted and a watchdog entry logged if reroute email address is
+   * set to an empty string.
    */
   public function testRerouteDefaultAddress() {
     // Check default value for reroute_email_address when not configured.
     // If Site email is not empty, it should be the default value.
     $default_destination = variable_get('site_mail', NULL);
-    $this->assertTrue(isset($default_destination), format_string('Site mail is not empty: @default_destination', array('@default_destination' => $default_destination)));
+    $this->assertTrue(isset($default_destination), format_string('Site mail is not empty: @default_destination.', array('@default_destination' => $default_destination)));
 
-    // Load the Reroute Email Settings form page.
+    // Programmatically enable email rerouting.
+    variable_set(REROUTE_EMAIL_ENABLE, TRUE);
+    // Load the Reroute Email Settings form page. Ensure rerouting is enabled.
     $this->drupalGet("admin/config/development/reroute_email/reroute_email");
-    // Check Email addresses field default value.
-    $this->assertFieldByName(REROUTE_EMAIL_ADDRESS, $default_destination, format_string('Site email address is configured and is the default value of the Email addresses field: @default_destination', array('@default_destination' => $default_destination)));
+    $this->assertFieldByName(REROUTE_EMAIL_ENABLE, TRUE, 'Email rerouting was programmatically successfully enabled.');
+    // Check Email addresses field default value should be site_mail.
+    $this->assertFieldByName(REROUTE_EMAIL_ADDRESS, $default_destination, format_string('Site email address is configured and is the default value of the Email addresses field: @default_destination.', array('@default_destination' => $default_destination)));
+
+    // Ensure reroute_email_address is not set yet.
+    $reroute_email_address = variable_get(REROUTE_EMAIL_ADDRESS, NULL);
+    $this->assertFalse(isset($reroute_email_address), 'Reroute email destination address is not configured.');
+
+    // Submit a test email and check if it is rerouted to site_mail address.
+    $this->drupalPost("admin/config/development/reroute_email/test",  array('to' => "to@example.com"), t("Send email"));
+    $this->assertText(t("Test email submitted for delivery."));
+    $mails = $this->drupalGetMails();
+    $mail = end($mails);
+    // Check rerouted email is the site email address.
+    $this->assertMail('to', $default_destination, format_string('Email was properly rerouted to site email address: @default_destination.', array('@default_destination' => $default_destination)));
 
     // Now unset site_mail to check if system sendmail_from is properly used.
     variable_del('site_mail');
@@ -428,7 +454,48 @@ class RerouteEmailDefaultAddressTestCase extends RerouteEmailTestCase {
 
     // Reload the Reroute Email Settings form page.
     $this->drupalGet("admin/config/development/reroute_email/reroute_email");
-    // Check Email addresses field default value.
-    $this->assertFieldByName('reroute_email_address', $default_destination, format_string('Site email address is not configured, Email addresses field defaults to system sendmail_from: @default_destination', array('@default_destination' => $default_destination)));
+    // Check Email addresses field default value should be system default.
+    $this->assertFieldByName('reroute_email_address', $default_destination, format_string('Site email address is not configured, Email addresses field defaults to system sendmail_from: @default_destination.', array('@default_destination' => $default_destination)));
+
+    // Submit a test email to check if it is rerouted to sendmail_from address.
+    $this->drupalPost("admin/config/development/reroute_email/test",  array('to' => "to@example.com"), t("Send email"));
+    $this->assertText(t("Test email submitted for delivery."));
+    $mails = $this->drupalGetMails();
+    $mail = end($mails);
+    // Check rerouted email is the system sendmail_from email address.
+    $this->assertMail('to', $default_destination, format_string('Email was properly rerouted to system sendmail_from email address: @default_destination.', array('@default_destination' => $default_destination)));
+
+    // Configure reroute email address to be emtpy: ensure emails are aborted.
+    $this->configureRerouteEmail('');
+
+    // Make sure reroute_email_address variable is an empty string.
+    $reroute_email_address = variable_get(REROUTE_EMAIL_ADDRESS, NULL);
+    $this->assertTrue(is_string($reroute_email_address), 'Reroute email destination address is configured to be an empty string.');
+    // Flush the Test Mail collector to ensure it is empty for this tests.
+    variable_set('drupal_test_email_collector', array());
+
+    // Submit a test email to check if it is aborted.
+    $this->drupalPost("admin/config/development/reroute_email/test",  array('to' => "to@example.com"), t("Send email"));
+    $mails = $this->drupalGetMails();
+    $mail_aborted = end($mails);
+    $this->assertFalse($mail_aborted, 'Email sending was properly aborted because rerouting email address is an empty string.');
+    // Check status message is displayed properly after email form submission.
+    $this->assertRaw(t('<em>@message_id</em> was aborted by reroute email, please check the <a href="@dblog">recent log entries</a> for complete details on the rerouted email.', array('@message_id' => $mail['id'], '@dblog' => url('admin/reports/dblog'))), format_string('Status message displayed as expected to the user with the mail ID <em>(@message_id)</em> and a link to recent log entries.', array('@message_id' => $mail['id'])));
+
+    // Check the watchdog entry logged with aborted email message.
+    $this->drupalGet('admin/reports/dblog');
+    // Check the link to the watchdog detailed message.
+    $watchdog_link = $this->xpath('//table[@id="admin-dblog"]/tbody/tr[contains(@class,"dblog-reroute-email")][1]/td[text()="reroute_email"]/following-sibling::td/a[contains(text(),"reroute_email")]');
+    $link_label = (string)$watchdog_link[0];
+    $this->assertTrue(isset($watchdog_link[0]), format_string("Recorded successfully a watchdog log entry in recent log messages: <em>@link</em>.", array('@link' => $link_label)));
+    // Open the full view page of the log message found for reroute_email.
+    $this->clickLink($link_label);
+
+    // Recreate expected logged message based on email submitted previously.
+    $mail['send'] = FALSE;
+    $mail['body'] = array($mail['body'], NULL);
+    // Ensure the correct email is logged with default 'to' placeholder. 
+    $mail['to'] = t('[No reroute email address configured]');
+    $this->assertRaw(t('Aborted email sending for <em>@message_id</em>. <br/>Detailed email data: Array $message <pre>@message</pre>', array('@message_id' => $mail['id'], '@message' => print_r($mail, TRUE))), format_string('The log entry recorded by Reroute Email contains a full dump of the aborted email message <em>@message_id</em> and is formatted as expected.', array('@message_id' => $mail['id'])));
   }
 }
