Index: webform.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.module,v
retrieving revision 1.196.2.49
diff -u -r1.196.2.49 webform.module
--- webform.module	18 Aug 2010 20:38:19 -0000	1.196.2.49
+++ webform.module	19 Aug 2010 05:18:44 -0000
@@ -384,15 +384,8 @@
 function webform_menu_email_load($eid, $nid) {
   module_load_include('inc', 'webform', 'includes/webform.emails');
   $node = node_load($nid);
+  $email = webform_email_load($eid, $nid);
   if ($eid == 'new') {
-    $email = array(
-      'email' => '',
-      'subject' => 'default',
-      'from_name' => 'default',
-      'from_address' => 'default',
-      'template' => 'default',
-      'excluded_components' => array(),
-    );
     if (isset($_GET['option']) && isset($_GET['email'])) {
       $type = $_GET['option'];
       if ($type == 'custom') {
@@ -403,9 +396,6 @@
       }
     }
   }
-  else {
-    $email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE;
-  }
 
   return $email;
 }
@@ -1924,6 +1914,7 @@
       }
 
       // Replace tokens in the message.
+      $email['html'] = ($email['html'] && module_exists('mimemail'));
       $email['message'] = _webform_filter_values($email['message'], $node, $submission, $email, FALSE, TRUE);
 
       // Build the e-mail headers.
@@ -1994,7 +1985,15 @@
           'node' => $node,
           'submission' => $submission,
         );
-        drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
+
+        if (module_exists('mimemail')) {
+          // TODO: Find attachments within the submission and add.
+          $attachments = array();
+          mimemail($email['from'], $address, $email['subject'], $email['message'], $email['html'] ? 0 : 1, $email['headers'], $email['html'] ? NULL : $email['message'], $attachments, 'webform');
+        }
+        else {
+          drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
+        }
       }
 
     }
@@ -2320,10 +2319,15 @@
     $replacements['safe']['%title'] = $node->title;
   }
 
+  // Determine the display format.
+  $format = isset($email['html']) && $email['html'] ? 'html' : 'text';
+
   // Submission replacements.
-  if (isset($submission) && !isset($replacements['submission_set'])) {
+  if (isset($submission) && !isset($replacements['email'][$format])) {
     module_load_include('inc', 'webform', 'includes/webform.components.inc');
-    $replacements['submission_set'] = TRUE;
+
+    // E-mails may be sent in two formats, keep tokens separate for each one.
+    $replacements['email'][$format] = array();
 
     foreach ($submission->data as $cid => $value) {
       $component = $node->webform['components'][$cid];
@@ -2331,9 +2335,9 @@
       // Find by form key.
       $parents = webform_component_parent_keys($node, $component);
       $form_key = implode('][', $parents);
-      $display_element = webform_component_invoke($component['type'], 'display', $component, $value['value'], 'text');
-      $replacements['unsafe']['%email[' . $form_key . ']'] = drupal_render($display_element);
-      $replacements['safe']['%value[' . $form_key . ']'] = isset($display_element['#children']) ? $display_element['#children'] : '';
+      $display_element = webform_component_invoke($component['type'], 'display', $component, $value['value'], $format);
+      $replacements['email'][$format]['%email[' . $form_key . ']'] = drupal_render($display_element);
+      $replacements['email'][$format]['%value[' . $form_key . ']'] = isset($display_element['#children']) ? $display_element['#children'] : '';
     }
 
     // Submission edit URL.
@@ -2341,8 +2345,8 @@
   }
 
   // Token for the entire form tree for e-mails.
-  if (isset($submission) && isset($email) && !isset($replacements['unsafe']['%email_values'])) {
-    $replacements['unsafe']['%email_values'] = webform_submission_render($node, $submission, $email, 'text');
+  if (isset($submission) && isset($email) && !isset($replacements['email'][$format]['%email_values'])) {
+    $replacements['email'][$format]['%email_values'] = webform_submission_render($node, $submission, $email, $format);
   }
 
   // Provide a list of candidates for token replacement.
@@ -2408,6 +2412,9 @@
   // anonymous page caching is enabled.
   if ($user->uid || $allow_anonymous) {
     $safe_replacements += $replacements['unsafe'];
+    if (isset($replacements['email'][$format])) {
+      $safe_replacements += $replacements['email'][$format];
+    }
   }
   else {
     foreach ($replacements['unsafe'] as $key => $value) {
Index: webform.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.install,v
retrieving revision 1.40.2.13
diff -u -r1.40.2.13 webform.install
--- webform.install	27 Jul 2010 06:08:00 -0000	1.40.2.13
+++ webform.install	19 Aug 2010 05:18:43 -0000
@@ -200,6 +200,22 @@
         'type' => 'text',
         'not null' => TRUE,
       ),
+      'html' => array(
+        'description' => 'Determines if the e-mail will be sent in an HTML format. Requires Mime Mail module.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'attachments' => array(
+        'description' => 'Determines if the e-mail will include file attachments. Requires Mime Mail module.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
     'primary key' => array('nid', 'eid'),
   );
@@ -1171,6 +1187,16 @@
 }
 
 /**
+ * Add columns for e-mail HTML and attachment settings.
+ */
+function webform_update_6318() {
+  $ret = array();
+  db_add_field($ret, 'webform_emails', 'html', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
+  db_add_field($ret, 'webform_emails', 'attachments', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
+  return $ret;
+}
+
+/**
  * Recursively delete all files and folders in the specified filepath, then
  * delete the containing folder.
  *
Index: includes/webform.emails.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/includes/webform.emails.inc,v
retrieving revision 1.9.2.9
diff -u -r1.9.2.9 webform.emails.inc
--- includes/webform.emails.inc	19 Aug 2010 03:00:45 -0000	1.9.2.9
+++ includes/webform.emails.inc	19 Aug 2010 05:18:44 -0000
@@ -256,15 +256,40 @@
     '#attributes' => array('id' => 'webform-template-fieldset'),
   );
 
+  $form['template']['template_option'] = array(
+    '#type' => 'select',
+    '#options' => array(
+      'default' => t('Default template'),
+      'custom' => t('Custom template'),
+    ),
+  );
+
   $default_template = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), $node, NULL, 'default');
   $template = $email['template'] == 'default' ? $default_template : $email['template'];
   $form['template']['template'] = array(
     '#type' => 'textarea',
     '#rows' => max(10, min(20, count(explode("\n", $template)))),
-    '#description' => theme('webform_token_help', $node),
     '#default_value' => $template,
   );
 
+  $form['template']['html'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Send e-mail as HTML'),
+    '#default_value' => $email['html'],
+    '#access' => module_exists('mimemail'),
+  );
+
+  $form['template']['attachments'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Include files as attachments'),
+    '#default_value' => $email['attachments'],
+    '#access' => module_exists('mimemail'),
+  );
+
+  $form['template']['tokens'] = array(
+    '#value' => theme('webform_token_help', $node),
+  );
+
   $form['template']['components'] = array(
     '#type' => 'select',
     '#title' => t('Included e-mail values'),
@@ -276,15 +301,6 @@
     '#process' => array('webform_component_select'),
   );
 
-  $form['template']['template_option'] = array(
-    '#type' => 'select',
-    '#options' => array(
-      'default' => t('Default template'),
-      'custom' => t('Custom template'),
-    ),
-    '#description' => t('Warning: Changing will immediately update the textarea above.'),
-  );
-
   // TODO: Allow easy re-use of existing templates.
   $form['templates']['#tree'] = TRUE;
   $form['templates']['default'] = array(
@@ -312,6 +328,7 @@
 function theme_webform_email_edit_form($form) {
   drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform-admin.css', 'theme', 'all', FALSE);
   drupal_add_js(drupal_get_path('module', 'webform') . '/js/webform-admin.js', 'module', 'header', FALSE, TRUE, FALSE);
+  drupal_add_js(array('webform' => array('revertConfirm' => t('Are you sure you want to revert any changes to your template back to the default?'))), 'setting');
 
   // Loop through fields, rendering them into radio button options.
   foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
@@ -346,8 +363,6 @@
   $form['templates']['#prefix'] = '<div id="webform-email-templates" style="display: none">';
   $form['templates']['#suffix'] = '</div>';
 
-  $form['template']['template']['#description'] .= drupal_render($form['template']['components']);
-
   return drupal_render($form);
 }
 
@@ -413,6 +428,10 @@
     $email['template'] = $form_state['values']['template'];
   }
 
+  // Save the attachment and HTML options provided by MIME mail.
+  $email['html'] = empty($form_state['values']['html']) ? 0 : 1;
+  $email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1;
+
   // Save the list of included components.
   // We actually maintain an *exclusion* list, so any new components will
   // default to being included in the %email_values token until unchecked.
@@ -469,6 +488,30 @@
 }
 
 /**
+ * Load an e-mail setting from the database or initialize a new e-mail.
+ */
+function webform_email_load($eid, $nid) {
+  $node = node_load($nid);
+  if ($eid == 'new') {
+    $email = array(
+      'email' => '',
+      'subject' => 'default',
+      'from_name' => 'default',
+      'from_address' => 'default',
+      'template' => 'default',
+      'excluded_components' => array(),
+      'html' => 0,
+      'attachments' => 0,
+    );
+  }
+  else {
+    $email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE;
+  }
+
+  return $email;
+}
+
+/**
  * Insert a new e-mail setting into the database.
  *
  * @param $email
@@ -477,7 +520,8 @@
 function webform_email_insert($email) {
   db_lock_table('webform_emails');
   $email['eid'] = isset($email['eid']) ? $email['eid'] : db_result(db_query('SELECT MAX(eid) FROM {webform_emails} WHERE nid = %d', $email['nid'])) + 1;
-  db_query("INSERT INTO {webform_emails} (nid, eid, email, subject, from_name, from_address, template, excluded_components) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s')", $email['nid'], $email['eid'], $email['email'], $email['subject'], $email['from_name'], $email['from_address'], $email['template'], implode(',', $email['excluded_components']));
+  $email['excluded_components'] = implode(',', $email['excluded_components']);
+  drupal_write_record('webform_emails', $email);
   db_unlock_tables();
   return $email['eid'];
 }
@@ -490,7 +534,8 @@
  *   other fields from the e-mail form.
  */
 function webform_email_update($email) {
-  return db_query("UPDATE {webform_emails} SET email = '%s', subject = '%s', from_name = '%s', from_address = '%s', template = '%s', excluded_components = '%s' WHERE nid = %d AND eid = %d", $email['email'], $email['subject'], $email['from_name'], $email['from_address'], $email['template'], implode(',', $email['excluded_components']), $email['nid'], $email['eid']);
+  $email['excluded_components'] = implode(',', $email['excluded_components']);
+  return drupal_write_record('webform_emails', $email, array('nid', 'eid'));
 }
 
 /**
Index: js/webform-admin.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/js/webform-admin.js,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 webform-admin.js
--- js/webform-admin.js	11 Apr 2010 06:13:51 -0000	1.1.2.2
+++ js/webform-admin.js	19 Aug 2010 05:18:44 -0000
@@ -68,7 +68,12 @@
 
   var updateTemplateText = function() {
     if ($(this).val() == 'default') {
-      $templateTextarea.val(defaultTemplate);
+      if (confirm(Drupal.settings.webform.revertConfirm)) {
+        $templateTextarea.val(defaultTemplate);
+      }
+      else {
+        $(this).val('custom');
+      }
     }
   }
 
