Index: print_mail/print_mail.inc
===================================================================
@@ -136,8 +142,10 @@
   );
   $form['btn_cancel'] = array(
     '#name' => 'cancel',
-    '#type' => 'submit',
+    '#type' => 'button',
     '#value' => t('Cancel'),
+    '#executes_submit_callback' => TRUE,
+    '#submit' => array('print_mail_form_cancel'),
   );
 
   if ($user->uid != 0) {
@@ -194,6 +202,8 @@
  */
 function print_mail_form_validate($form, &$form_state) {
   if (array_key_exists('cancel', $form['#post'])) {
+    form_set_error(NULL,'',TRUE);
+    drupal_get_messages('error');
     return;
   }
   $from_addr = trim($form_state['values']['fld_from_addr']);
@@ -248,77 +258,75 @@
  * @ingroup forms
  */
 function print_mail_form_submit($form, &$form_state) {
-  if (!array_key_exists('cancel', $form_state['values'])) {
-    if (!empty($form_state['values']['fld_from_name'])) {
-      $from = '"'. $form_state['values']['fld_from_name'] .'" <'. $form_state['values']['fld_from_addr'] .'>';
-    }
-    else {
-      $from = $form_state['values']['fld_from_addr'];
-    }
-    $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
+  if (!empty($form_state['values']['fld_from_name'])) {
+    $from = '"'. $form_state['values']['fld_from_name'] .'" <'. $form_state['values']['fld_from_addr'] .'>';
+  }
+  else {
+    $from = $form_state['values']['fld_from_addr'];
+  }
+  $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
 
-    $print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
-    $sender_message = $print_mail_text_message .':<br /><br /><em>'. nl2br(check_plain($form_state['values']['txt_message'])) .'</em>';
+  $print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
+  $sender_message = $print_mail_text_message .':<br /><br /><em>'. nl2br(check_plain($form_state['values']['txt_message'])) .'</em>';
 
-    $print = print_controller($form_state['values']['path'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
+  $print = print_controller($form_state['values']['path'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
 
-    // Spaces in img URLs must be replaced with %20
-    $pattern = '!<(img\s[^>]*?)>!is';
-    $print['content'] = preg_replace_callback($pattern, '_print_mail_encode_urls', $print['content']);
+  // Spaces in img URLs must be replaced with %20
+  $pattern = '!<(img\s[^>]*?)>!is';
+  $print['content'] = preg_replace_callback($pattern, '_print_mail_encode_urls', $print['content']);
 
-    if ($print !== FALSE) {
-      $params = array();
-      $params['subject'] = $form_state['values']['fld_subject'];
-      $params['message'] = $form_state['values']['txt_message'];
-      $params['link'] = $print['url'];
-      $params['title'] = $form_state['values']['title'];
+  if ($print !== FALSE) {
+    $params = array();
+    $params['subject'] = $form_state['values']['fld_subject'];
+    $params['message'] = $form_state['values']['txt_message'];
+    $params['link'] = $print['url'];
+    $params['title'] = $form_state['values']['title'];
 
-      $node = $print['node'];
-      $params['body'] = theme('print_page', $print, PRINT_MAIL_FORMAT, $node);
-      $params['body'] = drupal_final_markup($params['body']);
+    $node = $print['node'];
+    $params['body'] = theme('print_page', $print, PRINT_MAIL_FORMAT, $node);
+    $params['body'] = drupal_final_markup($params['body']);
 
-      $ok = FALSE;
-      if (function_exists('job_queue_add') && variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT)) {
-        $use_job_queue = TRUE;
-        $this_file = drupal_get_path('module', 'print_mail') .'/print_mail.inc';
-      }
-      else {
-        $use_job_queue = FALSE;
-      }
+    $ok = FALSE;
+    if (function_exists('job_queue_add') && variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT)) {
+      $use_job_queue = TRUE;
+      $this_file = drupal_get_path('module', 'print_mail') .'/print_mail.inc';
+    }
+    else {
+      $use_job_queue = FALSE;
+    }
 
-      $addresses = explode(', ', $form_state['values']['txt_to_addrs']);
-      foreach ($addresses as $to) {
-        // Call to hook_print_mail_before_send in order to know if the mail can be sent
-        // Handlers must return TRUE or FALSE
-        $can_send = module_invoke_all('print_mail_before_send', $node, $to, $from, $params);
+    $addresses = explode(', ', $form_state['values']['txt_to_addrs']);
+    foreach ($addresses as $to) {
+      // Call to hook_print_mail_before_send in order to know if the mail can be sent
+      // Handlers must return TRUE or FALSE
+      $can_send = module_invoke_all('print_mail_before_send', $node, $to, $from, $params);
 
-        if (!in_array(FALSE, $can_send)) {
-          if ($use_job_queue) {
-            // Use job queue to send mails during cron runs
-            job_queue_add('drupal_mail', t('print_mail: From %from', array('%from' => $from)), array('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE), $this_file, TRUE);
-          }
-          else {
-            // Send mail immediately using Drupal's mail handler
-            $ret = drupal_mail('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE);
-          }
-          if ($ret['result'] || $use_job_queue) {
-            // Call to hook_print_mail_after_send in order to provide information to other modules.
-            module_invoke_all('print_mail_after_send', $node, $to, $from, $params);
+      if (!in_array(FALSE, $can_send)) {
+        if ($use_job_queue) {
+          // Use job queue to send mails during cron runs
+          job_queue_add('drupal_mail', t('print_mail: From %from', array('%from' => $from)), array('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE), $this_file, TRUE);
+        }
+        else {
+          // Send mail immediately using Drupal's mail handler
+          $ret = drupal_mail('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE);
+        }
+        if ($ret['result'] || $use_job_queue) {
+          // Call to hook_print_mail_after_send in order to provide information to other modules.
+          module_invoke_all('print_mail_after_send', $node, $to, $from, $params);
 
-            flood_register_event('print_mail');
-            $ok = TRUE;
-          }
+          flood_register_event('print_mail');
+          $ok = TRUE;
         }
       }
-      if ($ok) {
-        watchdog('print_mail', '%name [%from] sent %page to [%to]', array('%name' => $form_state['values']['fld_from_name'], '%from' => $form_state['values']['fld_from_addr'], '%page' => $form_state['values']['path'], '%to' => $form_state['values']['txt_to_addrs']));
-        $site_name = variable_get('site_name', t('us'));
-        $print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
-        drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
+    }
+    if ($ok) {
+      watchdog('print_mail', '%name [%from] sent %page to [%to]', array('%name' => $form_state['values']['fld_from_name'], '%from' => $form_state['values']['fld_from_addr'], '%page' => $form_state['values']['path'], '%to' => $form_state['values']['txt_to_addrs']));
+      $site_name = variable_get('site_name', t('us'));
+      $print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
+      drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
 
-        $nodepath = drupal_get_normal_path($form_state['values']['path']);
-        db_query("UPDATE {print_mail_page_counter} SET sentcount = sentcount + %d, sent_timestamp = %d WHERE path = '%s'", count($addresses), time(), $nodepath);
-      }
+      $nodepath = drupal_get_normal_path($form_state['values']['path']);
+      db_query("UPDATE {print_mail_page_counter} SET sentcount = sentcount + %d, sent_timestamp = %d WHERE path = '%s'", count($addresses), time(), $nodepath);
     }
   }
 
