? ../mail_api/mail_api.hooks.inc
Index: ../mail_api/mail_api.admin.inc
===================================================================
RCS file: ../mail_api/mail_api.admin.inc
diff -N ../mail_api/mail_api.admin.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ../mail_api/mail_api.admin.inc	1 Jul 2008 00:37:02 -0000
@@ -0,0 +1,785 @@
+<?php
+
+
+function mail_api_admin($type = 'list') {
+
+
+  if($type == 'list') {
+    return drupal_get_form('mail_api_settings_overview');
+  }
+  else if ($type == 'add') {
+     return drupal_get_form('mail_api_add_server');
+  }
+
+
+}
+
+/**
+ * Main Mail API settings page.
+ *
+ * This will show links that any other modules have put in the
+ * admin/settings/mail_api path.  This is recommended for any
+ * spam filtering modules, mail server modules, front-end modules,
+ * etc., that need some site-wide configurations.
+ */
+function mail_api_settings() {
+  $item = menu_get_item();
+  $content = system_admin_menu_block($item);
+  return theme('mail_api_settings', $content);
+}
+
+/**
+ * Theme the Mail API settings page.
+ */
+function theme_mail_api_settings($content) {
+
+  $output = '';
+
+  if ($content) {
+    $output = '<dl class="admin-list">';
+    foreach ($content as $item) {
+      $output .= '<dt>'. l($item['title'], $item['href'], $item['options']) .'</dt>';
+      $output .= '<dd>'. $item['description'] .'</dd>';
+    }
+    $output .= '</dl>';
+  }
+
+  return $output;
+}
+
+/**
+ * List mail server configurations.
+ */
+function mail_api_settings_overview() {
+
+
+
+  $servers = mail_api_get_servers();
+  $protocols = mail_api_protocols();
+  $encryption = mail_api_encryption_options();
+
+
+  $options['unsuspend']='Unsuspend selected servers';
+  $options['suspend']='Suspend selected servers';
+  $options['delete']='Delete selected servers';
+
+  $form['action'] = array(
+    '#type' => 'fieldset', '#title' => t('Actions'),
+    '#prefix' => '<div class="container-inline">', '#suffix' => '</div>'
+  );
+
+  $form['action']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'suspend');
+  $form['action']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
+
+  $form['header'] = array('#type' => 'value', '#value' => array(
+    theme('table_select_header_cell'),
+    array('data' => t('Domain'), 'field' => 'domain'),
+    array('data' => t('Protocol'), 'field' => 'protocol'),
+    array('data' => t('Hostname'), 'field' => 'hostname'),
+    array('data' => t('Port'), 'field' => 'port'),
+    array('data' => t('Encryption'), 'field' => 'encryption'),
+    array('data' => t('Operations'))
+  ));
+
+  $result = pager_query('SELECT * FROM {mail_api_servers}', 50);
+
+  $destination = drupal_get_destination();
+
+  while($server = db_fetch_object($result)) {
+
+  	//print_r($server);
+  	$selected[$server->id] = '';
+
+  	//$form['checkboxes'][$index] = array();
+
+  	$server_extra = array();
+  	$server_extra_str = "";
+
+
+   // we need to mark suspended servers
+  	if($server->suspended==1) {
+      $server_extra[] = t('suspended');
+   }
+
+   // create a string to append to the domain
+   if($server_extra) {
+      $server_extra_str = " (".implode(", ", $server_extra).")";
+   }
+
+
+   $form['domain'][$server->id] = array('#value' => trim($server->domain.$server_extra_str));
+   $form['protocol'][$server->id] = array('#value' => $server->protocol);
+   $form['hostname'][$server->id] = array('#value' => $server->hostname);
+   $form['port'][$server->id] = array('#value' => $server->port);
+   $form['encryption'][$server->id] = array('#value' => $server->encryption);
+   $form['operations'][$server->id] = array('#value' => l(t('edit'), 'admin/settings/mail_api/edit/'.$server->id, array('query' => $destination)));
+  }
+
+  $form['selected'] = array('#type' => 'checkboxes', '#options' => $selected);
+  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
+
+  return $form;
+}
+
+
+function theme_mail_api_settings_overview($form) {
+
+  if(sizeof($form['domain'])<=0) {
+    $content .= t('No servers configured. Click \'Add Server\' above to configure a new server.');
+    return $content;
+
+  }
+
+  $output = drupal_render($form['action']);
+
+  if(isset($form['protocol'])) {
+
+	  foreach (element_children($form['protocol']) as $key) {
+	    $row = array();
+
+       $row[] = drupal_render($form['selected'][$key]);
+       $row[] = drupal_render($form['domain'][$key]);
+       //$row[] = drupal_render($form['name'][$key]);
+	    $row[] = drupal_render($form['protocol'][$key]);
+	    $row[] = drupal_render($form['hostname'][$key]);
+	    $row[] = drupal_render($form['port'][$key]);
+	    $row[] = drupal_render($form['encryption'][$key]);
+	    $row[] = drupal_render($form['operations'][$key]);
+
+	    $rows[] = $row;
+	  }
+
+  } else {
+    $rows[] = array(array('data' => t('No servers available.'), 'colspan' => '6'));
+  }
+
+  $output .= theme('table', $form['header']['#value'], $rows);
+
+  if ($form['pager']['#value']) {
+    $output .= drupal_render($form['pager']);
+  }
+
+  $output .= drupal_render($form);
+
+  return $output;
+
+}
+
+/**
+ * hook_submit processes the mail_api_settings_overview form
+ *
+ * @param unknown_type $form_id
+ * @param unknown_type $form_state
+ */
+function mail_api_settings_overview_submit($form_id, &$form_state) {
+
+
+	$destination = drupal_get_destination();
+
+
+	if($form_state['values']['operation'] == 'suspend' && sizeof($form_state['values']['selected'])>0) {
+		foreach($form_state['values']['selected'] as $id=>$selected) {
+			if($id != $selected) continue;
+			db_query("UPDATE {mail_api_servers} SET suspended=1 WHERE id=%d", $id);
+		}
+	}
+
+  if($form_state['values']['operation'] == 'unsuspend' && sizeof($form_state['values']['selected'])>0) {
+    foreach($form_state['values']['selected'] as $id=>$selected) {
+
+      if($id != $selected) continue;
+      db_query("UPDATE {mail_api_servers} SET suspended=0 WHERE id=%d", $id);
+
+    }
+  }
+
+	if($form_state['values']['operation'] == 'delete' && sizeof($form_state['values']['selected'])>0) {
+    foreach($form_state['values']['selected'] as $id=>$selected) {
+
+      if($id != $selected) continue;
+      db_query("DELETE FROM {mail_api_servers} WHERE id=%d", $id);
+
+    }
+	}
+
+	$form_state['redirect'] = 'admin/settings/mail_api';
+
+}
+
+function mail_api_edit_server() {
+
+	return drupal_get_form('mail_api_edit_server_form');
+}
+
+
+function mail_api_edit_server_form() {
+
+  //drupal_set_message('editing!');
+
+  $id = arg(4);
+  $server  = db_fetch_object(db_query("SELECT * FROM {mail_api_servers} WHERE id=%d", $id));
+
+
+  $encryption = mail_api_value_mirror_key(mail_api_encryption_options());
+  $protocols = mail_api_protocols();
+
+
+
+  $form['mail_api'] = array(
+    '#title' => t('Mail Server Information'),
+    '#type' => 'fieldset',
+  );
+
+  $form['mail_api']['id'] = array(
+    '#type' => 'hidden',
+    '#value' => $id
+  );
+
+  /*
+  $form['mail_api']['mail_api_connection_name'] = array(
+    '#title' => t('Name'),
+    '#description' => t('Give this connection a unique name.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->name,
+    '#required' => TRUE,
+  );
+  */
+
+  $form['mail_api']['mail_api_protocol'] = array(
+    '#title' => t('Protocol'),
+    '#type' => 'select',
+    '#default_value' => $server->protocol,
+    '#options' => $protocols,
+  );
+
+  $form['mail_api']['mail_api_protocol_module'] = array(
+    '#type' => 'hidden',
+    '#value' => $protocols[$name]['module'],
+  );
+
+  $form['mail_api']['mail_api_hostname'] = array(
+    '#title' => t('Server Hostname'),
+    '#description' => t('Host name or IP address of the mail server host'),
+    '#type' => 'textfield',
+    '#default_value' => $server->hostname,
+    '#required' => TRUE,
+  );
+
+  $form['mail_api']['mail_api_port'] = array(
+    '#title' => t('Server Port'),
+    '#type' => 'textfield',
+    '#default_value' => $server->port,
+    '#required' => TRUE,
+  );
+
+  $form['mail_api']['mail_api_encryption_values'] = array(
+    '#type' => 'value',
+    '#value' => $protocols[$name]['encryption'],
+  );
+
+  $form['mail_api']['mail_api_encryption'] = array(
+    '#title' => t('Encryption'),
+    '#description' => t('Encryption method used to log into the server'),
+    '#type' => 'select',
+    '#default_value' => $server->encryption,
+    '#options' => $encryption
+  );
+
+  $form['mail_api']['mail_api_domain'] = array(
+    '#title' => t('Domain'),
+    '#description' => t('Domain associated with this connection, e.g. domain.com.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->domain,
+    '#description' => t('The domain name in the users email address.  This is how Mail API can determine which server module to use for a user to access their email.'),
+  );
+
+  $form['mail_api']['mail_api_folder_prefix'] = array(
+    '#title' => t('Folder Prefix'),
+    '#description' => t('A prefix for folders, e.g. INBOX.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->folder_prefix,
+    '#description' => t('Some servers store folders as subfolders of INBOX so the full folder name looks like INBOX.folder. If this is the case, specify \'INBOX.\'.'),
+  );
+
+  $form['mail_api']['mail_api_hide_folder_prefix'] = array(
+    '#title' => t('Hide Folder Prefix'),
+    '#description' => t('Hide folder prefix.'),
+    '#type' => 'checkbox',
+    '#default_value' => $server->hide_folder_prefix,
+    '#description' => t('If your folders have a prefix and you want to hide it, check this box.'),
+  );
+
+  $form['mail_api']['mail_api_inbox_folder'] = array(
+    '#title' => t('Inbox Folder'),
+    '#description' => t('Full path to the Inbox folder.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->inbox_folder,
+    '#description' => t('The folder where all incoming mail arroves. If not sure set to INBOX'),
+  );
+
+  $form['mail_api']['mail_api_trash_folder'] = array(
+    '#title' => t('Trash Folder'),
+    '#description' => t('Full path to the Trash folder.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->trash_folder,
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Trash, e.g. INBOX.Trash.'),
+  );
+
+  $form['mail_api']['mail_api_sent_folder'] = array(
+    '#title' => t('Sent Folder'),
+    '#description' => t('Full path to the Sent folder.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->sent_folder,
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Sent, e.g. INBOX.Sent.'),
+  );
+
+  $form['mail_api']['mail_api_drafts_folder'] = array(
+    '#title' => t('Drafts Folder'),
+    '#description' => t('Full path to the Drafts folder.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->drafts_folder,
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Drafts, e.g. INBOX.Drafts.'),
+  );
+
+
+  $form['mail_api']['mail_api_options'] = array(
+    '#title' => t('Connection options'),
+    '#description' => t('Additional options, if any, to pass to the connection. Usually these are IMAP options.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->options,
+    '#description' => t('Some connections may require additional options, please specify them here.'),
+  );
+
+  $form['mail_api']['mail_api_login_suffix'] = array(
+    '#title' => t('Login Suffix'),
+    '#description' => t('A suffix supplied with the username during login.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->login_suffix,
+    '#description' => t('If your mail server requires a string appended to the username, e.g. user@domain.com or user+domain.com, enter it here.'),
+  );
+
+
+  $form['mail_api']['mail_api_outbox'] = array(
+    '#title' => t('Outbox'),
+    '#type' => 'textfield',
+    '#default_value' => $server->outbox,
+    '#disabled' => !$protocols[$name]['outbox'],
+    '#description' => t("This is the path to the 'Outbox' folder, if the mail server supports sending emails in this manner.  If this folder does not exist in the user's account, it will be created.  Any SMTP settings will be ignored if this is used."),
+  );
+
+  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
+  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
+
+
+  return $form;
+
+}
+
+function theme_mail_api_edit_server_form($form) {
+
+
+	return drupal_render($form);
+}
+
+
+function mail_api_edit_server_form_validate($form, &$form_state) {
+  $count = db_result(db_query("SELECT COUNT(*) FROM {mail_api_servers} WHERE id!=%d AND domain='%s'", $form_state['values']['id'], $form_state['values']['mail_api_domain']));
+
+  if($count>0) {
+    form_set_error('mail_api][domain', t('A connection for this domain already exists.'));
+  }
+}
+
+function mail_api_edit_server_form_submit($form, &$form_state) {
+
+
+  if (module_exists('aes')) {
+    if($form_state['values']['smtp_primary_password']) {
+      $smtp_primary_password = aes_encrypt($form_state['values']['smtp_primary_password']);
+    }
+
+    if($form_state['values']['smtp_backup_password']) {
+      $smtp_backup_password = aes_encrypt($form_state['values']['smtp_backup_password']);
+    }
+  }
+  else {
+    $smtp_primary_password = $form_state['values']['smtp_primary_password'];
+    $smtp_backup_password = $form_state['values']['smtp_backup_password'];
+  }
+
+
+
+  db_query(
+    "UPDATE {mail_api_servers} SET protocol='%s', protocol_module='%s', hostname='%s', port='%s', encryption='%s', domain='%s', folder_prefix='%s', hide_folder_prefix=%d, inbox_folder='%s', trash_folder='%s', sent_folder='%s', drafts_folder='%s', options='%s', login_suffix='%s', outbox='%s', smtp_primary_hostname='%s', smtp_primary_port='%s', smtp_primary_encryption='%s', smtp_primary_auth='%s', smtp_primary_username='%s', smtp_primary_password='%s', smtp_backup_hostname='%s', smtp_backup_port='%s', smtp_backup_encryption='%s', smtp_backup_auth='%s', smtp_backup_username='%s', smtp_backup_password='%s' WHERE id=%d",
+    $form_state['values']['mail_api_protocol'],
+    $form_state['values']['mail_api_protocol_module'],
+    $form_state['values']['mail_api_hostname'],
+    (int) $form_state['values']['mail_api_port'],
+    $form_state['values']['mail_api_encryption'],
+    $form_state['values']['mail_api_domain'],
+    $form_state['values']['mail_api_folder_prefix'],
+    $form_state['values']['mail_api_hide_folder_prefix'],
+    $form_state['values']['mail_api_inbox_folder'],
+    $form_state['values']['mail_api_trash_folder'],
+    $form_state['values']['mail_api_sent_folder'],
+    $form_state['values']['mail_api_drafts_folder'],
+    $form_state['values']['mail_api_options'],
+    $form_state['values']['mail_api_login_suffix'],
+    $form_state['values']['mail_api_outbox'],
+    $form_state['values']['smtp_primary_hostname'],
+    (int) $form_state['values']['smtp_primary_port'],
+    $form_state['values']['smtp_primary_encryption'],
+    $form_state['values']['smtp_primary_username'],
+    $smtp_primary_password,
+    $form_state['values']['smtp_primary_auth'],
+    $form_state['values']['smtp_backup_hostname'],
+    (int) $form_state['values']['smtp_backup_port'],
+    $form_state['values']['smtp_backup_encryption'],
+    $form_state['values']['smtp_backup_username'],
+    $smtp_backup_password,
+    $form_state['values']['smtp_backup_auth'],
+    $form_state['values']['id']
+  );
+
+  drupal_set_message(t('The server configuration has been updated.'));
+
+  $form_state['redirect'] = 'admin/settings/mail_api';
+
+
+}
+
+
+/**
+ * Create a form for adding new mail server configuration to database.
+ */
+function mail_api_add_server() {
+  $encryption = mail_api_value_mirror_key(mail_api_encryption_options());
+
+
+  //print_r($encryption);
+
+  $protocols = mail_api_protocols();
+
+
+  /*
+  foreach($protocols as $name => $value) {
+    if($name == $p_name['name']) {
+      break;
+    }
+  }
+  */
+
+  $form['mail_api'] = array(
+    '#title' => t('Mail Server Information'),
+    '#type' => 'fieldset',
+  );
+
+  /*
+  $form['mail_api']['mail_api_connection_name'] = array(
+    '#title' => t('Name'),
+    '#description' => t('Give this connection a unique name.'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+  );
+  */
+
+  $form['mail_api']['mail_api_protocol'] = array(
+    '#title' => t('Protocol'),
+    '#type' => 'select',
+    '#options' => $protocols,
+  );
+
+  $form['mail_api']['mail_api_protocol_module'] = array(
+    '#type' => 'hidden',
+    '#value' => $protocols[$name]['module'],
+  );
+
+  $form['mail_api']['mail_api_hostname'] = array(
+    '#title' => t('Server Hostname'),
+    '#description' => t('Host name or IP address of the mail server host'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+  );
+  $form['mail_api']['mail_api_port'] = array(
+    '#title' => t('Server Port'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+  );
+  $form['mail_api']['mail_api_encryption_values'] = array(
+    '#type' => 'value',
+    '#value' => $protocols[$name]['encryption'],
+  );
+  $form['mail_api']['mail_api_encryption'] = array(
+    '#title' => t('Encryption'),
+    '#description' => t('Encryption method used to log into the server'),
+    '#type' => 'select',
+    '#options' => $encryption
+  );
+
+  $form['mail_api']['mail_api_domain'] = array(
+    '#title' => t('Domain'),
+    '#description' => t('Domain associated with this connection, e.g. domain.com.'),
+    '#type' => 'textfield',
+    '#description' => t('The domain name in the users email address.  This is how Mail API can determine which server module to use for a user to access their email.'),
+  );
+
+
+  $form['mail_api']['mail_api_folder_prefix'] = array(
+    '#title' => t('Folder Prefix'),
+    '#description' => t('A prefix for folders, e.g. INBOX.'),
+    '#type' => 'textfield',
+    '#default_value' => $server->folder_prefix,
+    '#description' => t('Some servers store folders as subfolders of INBOX so the full folder name looks like INBOX.folder. If this is the case, specify \'INBOX.\'.'),
+  );
+
+  $form['mail_api']['mail_api_hide_folder_prefix'] = array(
+    '#title' => t('Hide Folder Prefix'),
+    '#description' => t('Hide folder prefix.'),
+    '#type' => 'checkbox',
+    '#description' => t('If your folders have a prefix and you want to hide it, check this box.'),
+  );
+
+  $form['mail_api']['mail_api_inbox_folder'] = array(
+    '#title' => t('Inobx Folder'),
+    '#description' => t('Full path to the Inbox folder.'),
+    '#type' => 'textfield',
+    '#default_value' => 'INBOX',
+    '#description' => t('The folder where all incoming mail arrives.'),
+  );
+
+  $form['mail_api']['mail_api_trash_folder'] = array(
+    '#title' => t('Trash Folder'),
+    '#description' => t('Full path to the Trash folder.'),
+    '#type' => 'textfield',
+    '#default_value' => 'Trash',
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Trash, e.g. INBOX.Trash.'),
+  );
+
+  $form['mail_api']['mail_api_sent_folder'] = array(
+    '#title' => t('Sent Folder'),
+    '#description' => t('Full path to the Sent folder.'),
+    '#type' => 'textfield',
+    '#default_value' => 'Sent',
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Sent, e.g. INBOX.Sent.'),
+  );
+
+  $form['mail_api']['mail_api_drafts_folder'] = array(
+    '#title' => t('Drafts Folder'),
+    '#description' => t('Full path to the Drafts folder.'),
+    '#type' => 'textfield',
+    '#default_value' => 'Drafts',
+    '#description' => t('If your server uses a folder prefix, specify PREFIX.Drafts, e.g. INBOX.Drafts.'),
+  );
+
+  $form['mail_api']['mail_api_options'] = array(
+    '#title' => t('Connection options'),
+    '#description' => t('Additional options, if any, to pass to the connection.'),
+    '#type' => 'textfield',
+    '#default_value' => '/notls',
+    '#description' => t('Some connections may require additional options, please specify them here.'),
+  );
+
+
+  $form['mail_api']['mail_api_login_suffix'] = array(
+    '#title' => t('Login Suffix'),
+    '#description' => t('A suffix supplied with the username during login.'),
+    '#type' => 'textfield',
+    '#description' => t('If your mail server requires a string appended to the username, e.g. user@domain.com or user+domain.com, enter it here.'),
+  );
+
+  $form['mail_api']['mail_api_outbox'] = array(
+    '#title' => t('Outbox'),
+    '#type' => 'textfield',
+    '#disabled' => !$protocols[$name]['outbox'],
+    '#description' => t("This is the path to the 'Outbox' folder, if the mail server supports sending emails in this manner.  If this folder does not exist in the user's account, it will be created.  Any SMTP settings will be ignored if this is used."),
+  );
+
+  /*
+  $form['smtp'] = array(
+    '#title' => t('SMTP Server Configuration'),
+    '#type' => 'fieldset',
+  );
+  $form['smtp']['primary'] = array(
+    '#title' => t('Primary SMTP Server'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['smtp']['primary']['smtp_primary_hostname'] = array(
+    '#title' => t('Hostname'),
+    '#type' => 'textfield',
+  );
+  $form['smtp']['primary']['smtp_primary_port'] = array(
+    '#title' => t('Port'),
+    '#type' => 'textfield',
+  );
+  $form['smtp']['primary']['smtp_primary_encryption_values'] = array(
+    '#type' => 'value',
+    '#value' => $encryption,
+  );
+  $form['smtp']['primary']['smtp_primary_encryption'] = array(
+    '#title' => t('Encryption'),
+    '#type' => 'select',
+    '#options' => $form['smtp']['primary']['smtp_primary_encryption_values']['#value'],
+  );
+  $form['smtp']['primary']['auth'] = array(
+    '#title' => t('SMTP Authentication'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['smtp']['primary']['auth']['smtp_primary_auth'] = array(
+    '#title' => t('Enable SMTP Authentication'),
+    '#type' => 'checkbox',
+    '#default_value' => FALSE,
+    '#description' => t('Leave username and password blank to use user credentials.'),
+  );
+  $form['smtp']['primary']['auth']['smtp_primary_username'] = array(
+    '#title' => t('Username'),
+    '#type' => 'textfield',
+    '#description' => t('Remove user name to clear password.'),
+  );
+  $form['smtp']['primary']['auth']['smtp_primary_password'] = array(
+    '#title' => t('Password'),
+    '#type' => 'password',
+    '#description' => t('Leave this blank unless setting a new password.'),
+  );
+
+  $form['smtp']['backup'] = array(
+    '#title' => t('Backup SMTP Server'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['smtp']['backup']['smtp_backup_hostname'] = array(
+    '#title' => t('Hostname'),
+    '#type' => 'textfield',
+  );
+  $form['smtp']['backup']['smtp_backup_port'] = array(
+    '#title' => t('Port'),
+    '#type' => 'textfield',
+  );
+  $form['smtp']['backup']['smtp_backup_encryption_values'] = array(
+    '#type' => 'value',
+    '#value' => $encryption,
+  );
+  $form['smtp']['backup']['smtp_backup_encryption'] = array(
+    '#title' => t('Encryption'),
+    '#type' => 'select',
+    '#options' => $form['smtp']['backup']['smtp_backup_encryption_values']['#value'],
+  );
+  $form['smtp']['backup']['auth'] = array(
+    '#title' => t('SMTP Authentication'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['smtp']['backup']['auth']['smtp_backup_auth'] = array(
+    '#title' => t('Enable SMTP Authentication'),
+    '#type' => 'checkbox',
+    '#default_value' => FALSE,
+    '#description' => t('Leave username and password blank to use user credentials.'),
+  );
+  $form['smtp']['backup']['auth']['smtp_backup_username'] = array(
+    '#title' => t('Username'),
+    '#type' => 'textfield',
+    '#description' => t('Remove user name to clear password.'),
+  );
+  $form['smtp']['backup']['auth']['smtp_backup_password'] = array(
+    '#title' => t('Password'),
+    '#type' => 'password',
+    '#description' => t('Leave this blank unless setting a new password.'),
+  );
+
+  */
+  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
+  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
+
+  return $form;
+}
+
+
+/**
+ * Insert new mail server configuration into database.
+ */
+function mail_api_add_server_submit($form, &$form_state) {
+
+  if (module_exists('aes')) {
+    $smtp_primary_password = aes_encrypt($form_state['values']['smtp_primary_password']);
+    $smtp_backup_password = aes_encrypt($form_state['values']['smtp_backup_password']);;
+  }
+  else {
+    $smtp_primary_password = $form_state['values']['smtp_primary_password'];
+    $smtp_backup_password = $form_state['values']['smtp_backup_password'];
+  }
+
+  db_query(
+    'INSERT INTO {mail_api_servers} (protocol, protocol_module, hostname, port, encryption, domain, folder_prefix, hide_folder_prefix, trash_folder, sent_folder, drafts_folder, options, login_suffix, outbox, smtp_primary_hostname, smtp_primary_port, smtp_primary_encryption, smtp_primary_auth, smtp_primary_username, smtp_primary_password, smtp_backup_hostname, smtp_backup_port, smtp_backup_encryption, smtp_backup_auth, smtp_backup_username, smtp_backup_password) VALUES ("%s", "%s", "%s", %d, "%s", "%s", "%s", %d, "%s", "%s", "%s", "%s", "%s", %d, %d, "%s", "%s", "%s", "%s", %d, %d, "%s", "%s", "%s", "%s")',
+    $form_state['values']['mail_api_protocol'],
+    $form_state['values']['mail_api_protocol_module'],
+    $form_state['values']['mail_api_hostname'],
+    (int) $form_state['values']['mail_api_port'],
+    $form_state['values']['mail_api_encryption'],
+    $form_state['values']['mail_api_domain'],
+    $form_state['values']['mail_api_folder_prefix'],
+    $form_state['values']['mail_api_hide_folder_prefix'],
+    $form_state['values']['mail_api_trash_folder'],
+    $form_state['values']['mail_api_sent_folder'],
+    $form_state['values']['mail_api_drafts_folder'],
+    $form_state['values']['mail_api_options'],
+    $form_state['values']['mail_api_login_suffix'],
+    $form_state['values']['mail_api_outbox'],
+    $form_state['values']['smtp_primary_hostname'],
+    (int) $form_state['values']['smtp_primary_port'],
+    $form_state['values']['smtp_primary_encryption'],
+    $form_state['values']['smtp_primary_username'],
+    $smtp_primary_password,
+    $form_state['values']['smtp_primary_auth'],
+    $form_state['values']['smtp_backup_hostname'],
+    (int) $form_state['values']['smtp_backup_port'],
+    $form_state['values']['smtp_backup_encryption'],
+    $form_state['values']['smtp_backup_username'],
+    $smtp_backup_password,
+    $form_state['values']['smtp_backup_auth']
+  );
+
+  drupal_set_message(t('The server configuration has been saved.'));
+
+  $form_state['redirect'] = 'admin/settings/mail_api';
+}
+
+
+
+
+function mail_api_add_server_validate($form, &$form_state) {
+  /*
+  if(strlen($form_state['values']['mail_api_connection_name'])>USERNAME_MAX_LENGTH) {
+    form_set_error('mail_api][connection_name', t('Connection name can not be longer than 64 characters.'));
+
+  }
+  */
+
+  $count = db_result(db_query("SELECT COUNT(*) FROM {mail_api_servers} WHERE domain='%s'", $form_state['values']['mail_api_domain']));
+
+  if($count>0) {
+    form_set_error('mail_api][connection_name', t('A connection with this name already exists.'));
+  }
+
+
+}
+
+/**
+ * takes an array and returns an array where each key equals to value
+ *
+ * @param unknown_type $array
+ * @return unknown
+ */
+function mail_api_value_mirror_key($array) {
+   if(sizeof($array)<=0) return FALSE;
+
+   foreach($array as $key => $value) {
+      $new[$value]=$value;
+   }
+
+   return $new;
+}
\ No newline at end of file
Index: ../mail_api/mail_api.helpers.inc
===================================================================
RCS file: ../mail_api/mail_api.helpers.inc
diff -N ../mail_api/mail_api.helpers.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ../mail_api/mail_api.helpers.inc	1 Jul 2008 00:37:02 -0000
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * takes an array and returns an array where each key equals to value
+ *
+ * @param unknown_type $array
+ * @return unknown
+ */
+function mail_api_value_mirror_key($array) {
+   if(sizeof($array)<=0) return FALSE;
+   
+   foreach($array as $key => $value) {
+      $new[$value]=$value;
+   }
+   
+   return $new;
+}
Index: ../mail_api/mail_api.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mail_api/mail_api.info,v
retrieving revision 1.4
diff -u -p -r1.4 mail_api.info
--- ../mail_api/mail_api.info	5 Mar 2008 05:02:34 -0000	1.4
+++ ../mail_api/mail_api.info	1 Jul 2008 00:37:02 -0000
@@ -2,4 +2,10 @@
 name = Mail API
 description = Provides an abstraction layer for accessing various mail servers.
 package = Mail API
-core = 6.x
\ No newline at end of file
+core = 6.x
+; Information added by drupal.org packaging script on 2008-03-18
+version = "6.x-1.x-dev"
+core = "6.x"
+project = "mail_api"
+datestamp = "1205798997"
+
Index: ../mail_api/mail_api.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mail_api/mail_api.install,v
retrieving revision 1.7
diff -u -p -r1.7 mail_api.install
--- ../mail_api/mail_api.install	12 Mar 2008 01:55:08 -0000	1.7
+++ ../mail_api/mail_api.install	1 Jul 2008 00:37:02 -0000
@@ -1,17 +1,28 @@
 <?php
 // $Id: mail_api.install,v 1.7 2008/03/12 01:55:08 gcopenhaver Exp $
 
+
+
 function mail_api_schema() {
   $schema['mail_api_servers'] = array(
     'fields' => array(
       'id' => array('type' => 'serial', 'not null' => TRUE),
+      'name' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),
       'protocol' => array('type' => 'text', 'not null' => TRUE, 'description' => 'Protocol ID'),
       'protocol_module' => array('type' => 'text', 'not null' => TRUE),
       'hostname' => array('type' => 'text'),
       'port' => array('type' => 'int', 'unsigned' => TRUE),
       'encryption' => array('type' => 'text', 'not null' => TRUE),
       'domain' => array('type' => 'text'),
-      'outbox' => array('type' => 'text'),
+      'folder_prefix' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'hide_folder_prefix' => array('type' => 'int', 'size' => 'tiny', 'default' => 0),
+      'inbox_folder' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'trash_folder' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'sent_folder' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'drafts_folder' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'options' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'outbox' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
+      'login_suffix' => array('type' => 'varchar', 'length' => 128, 'default' => ''),
       'smtp_primary_hostname' => array('type' => 'varchar', 'length' => 100),
       'smtp_primary_port' => array('type' => 'int'),
       'smtp_primary_encryption' => array('type' => 'int', 'unsigned' => TRUE),
@@ -24,12 +35,17 @@ function mail_api_schema() {
       'smtp_backup_auth' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE),
       'smtp_backup_username' => array('type' => 'varchar', 'length' => 30, 'default' => ''),
       'smtp_backup_password' => array('type' => 'varchar', 'length' => 50, 'default' => ''),
+      'suspended' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
+      'default' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
+
     ),
     'indexes' => array(
       'id' => array('id'),
     ),
     'primary key' => array('id'),
   );
+
+  /*
   $schema['mail_api_tag_cache'] = array(
     'fields' => array(
       'tid' => array('type' => 'serial', 'not null' => TRUE),
@@ -41,6 +57,9 @@ function mail_api_schema() {
     ),
     'primary key' => array('tid'),
   );
+  */
+
+  /*
   $schema['mail_api_folder_cache'] = array(
     'fields' => array(
       'fid' => array('type' => 'serial', 'not null' => TRUE),
@@ -57,7 +76,8 @@ function mail_api_schema() {
     ),
     'primary key' => array('fid'),
   );
-  
+  */
+
   return $schema;
 }
 
Index: ../mail_api/mail_api.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mail_api/mail_api.module,v
retrieving revision 1.19
diff -u -p -r1.19 mail_api.module
--- ../mail_api/mail_api.module	17 Mar 2008 18:12:35 -0000	1.19
+++ ../mail_api/mail_api.module	1 Jul 2008 00:37:02 -0000
@@ -1,5 +1,18 @@
 <?php
 // $Id: mail_api.module,v 1.19 2008/03/17 18:12:35 gcopenhaver Exp $
+define('NAME_MAX_LENGTH', 64);
+
+// set this to true to write debug info to the wathdog
+define('MAIL_API_DEBUG', FALSE);
+
+// set this to true to write the error log to the watchdog log
+define('MAIL_API_ERRORLOG', FALSE);
+
+define('MAIL_API_ARRAY', 'array');
+define('MAIL_API_OBJECT', 'object');
+
+
+$mail_api_module = NULL;
 
 /**
  * Implementation of hook_perm()
@@ -12,1053 +25,72 @@ function mail_api_perm() {
  * Implementation of hook_menu()
  */
 function mail_api_menu() {
+
   $items['admin/settings/mail_api'] = array(
     'title' => 'Mail API',
-    'page callback' => 'mail_api_settings',
+    'description' => 'List and edit mail servers.',
+    'page callback' => 'mail_api_admin',
     'access arguments' => array('administer mail_api'),
-    'description' => 'Mail API main configuration page',
-  );
-  $items['admin/settings/mail_api/servers'] = array(
-    'title' => 'Mail Servers',
-    'page callback' => 'mail_api_server_list',
-    'description' => 'Mail API server list.',
+    'file' => 'mail_api.admin.inc',
   );
-  $items['admin/settings/mail_api/servers/view'] = array(
-    'title' => 'Mail Servers',
+
+  // Tabs:
+  $items['admin/settings/mail_api/new'] = array(
+    'title' => 'Servers',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
-  $items['admin/settings/mail_api/servers/add'] = array(
-    'title' => 'Add Server',
-    'page callback' => 'mail_api_add_server',
-    'description' => 'Mail API add new server',
-    'type' => MENU_LOCAL_TASK,
-  );
-  $items['admin/settings/mail_api/servers/%/edit'] = array(
-    'title' => 'Edit Server',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('mail_api_form_edit_server', 4),
-    'type' => MENU_CALLBACK,
-  );
-  $items['admin/settings/mail_api/servers/%/delete'] = array(
-    'title' => 'Delete Server',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('mail_api_form_delete_server', 4),
-    'type' => MENU_CALLBACK,
-  );
-  
-  return $items;
-}
-
-/**
- * Implementation of hook_theme().
- */
-function mail_api_theme() {
-  return array(
-    'mail_api_settings' => array(
-      'arguments' => array('content' => NULL),
-    ),
-    'mail_api_add_server' => array(
-      'arguments' => array('protocols' => NULL),
-    ),
-    'mail_api_server_list' => array(
-      'arguments' => array('servers' => NULL, 'protocols' => NULL, 'encryption' => NULL),
-    ),
-  );
-}
 
-/**
- * Main Mail API settings page.
- * 
- * This will show links that any other modules have put in the
- * admin/settings/mail_api path.  This is recommended for any
- * spam filtering modules, mail server modules, front-end modules,
- * etc., that need some site-wide configurations.
- */
-function mail_api_settings() {
-  $item = menu_get_item();
-  $content = system_admin_menu_block($item);
-  return theme('mail_api_settings', $content); 
-}
 
-/**
- * Theme the Mail API settings page.
- */
-function theme_mail_api_settings($content) {
-  $output = '';
-  
-  if ($content) {
-    $output = '<dl class="admin-list">';
-    foreach ($content as $item) {
-      $output .= '<dt>'. l($item['title'], $item['href'], $item['options']) .'</dt>';
-      $output .= '<dd>'. $item['description'] .'</dd>';
-    }
-    $output .= '</dl>';
-  }
-  
-  return $output;
-}
-
-/**
- * List mail server configurations.
- */
-function mail_api_server_list() {
-  $servers = mail_api_get_servers();
-  $protocols = mail_api_protocols();
-  $encryption = mail_api_encryption_options();
-  return theme('mail_api_server_list', $servers, $protocols, $encryption);
-}
-
-/**
- * Theme the list of mail server configurations.
- */
-function theme_mail_api_server_list($servers, $protocols, $encryption) {
-  $content .= '<table class="sticky-enabled">';
-  $content .= '<thead>';
-  $content .= '<tr>';
-  $content .= '<th></th>';
-  $content .= '<th>Hostname</th>';
-  $content .= '<th>Protocol</th>';
-  $content .= '<th>Encryption</th>';
-  $content .= '<th>SMTP Hostname</th>';
-  $content .= '<th>SMTP Encryption</th>';
-  $content .= '<th></th>';
-  $content .= '</tr>';
-  $content .= '</thead>';
-  $content .= '<tbody>';
-  if(!empty($servers)) {
-    $count = 0;
-    foreach($servers as $server) {
-      $count++;
-      $content .= '<tr class="' . (($count % 2) ? 'odd' : 'even') . '">';
-      $content .= '<td></td>'; /* for future checkbox use, for adding mass functions similar to those in admin/content/node */
-      $content .= '<td>' . l($server['hostname'] . ':' . $server['port'], 'admin/settings/mail_api/servers/' . $server['id'] . '/edit') . '</td>';
-      $content .= '<td>' . $server['protocol'] . '</td>';
-      $content .= '<td>' . $protocols[$server['protocol']]['encryption'][$server['encryption']] . '</td>';
-      $content .= '<td>' . $server['smtp_primary_hostname'] . ':' . $server['smtp_primary_port'] . '</td>';
-      $content .= '<td>' . $encryption[$server['smtp_primary_encryption']] . '</td>';
-      $content .= '<td>' . l('Edit', 'admin/settings/mail_api/servers/' . $server['id'] . '/edit') . '</td>';
-      $content .= '<td>' . l('Delete', 'admin/settings/mail_api/servers/' . $server['id'] . '/delete') . '</td>';
-      $content .= '</tr>';
-    }
-  }
-  else {
-    $content .= '<tr class="odd"><td colspan="8">No configured servers.</td></tr>';
-  }
-  $content .= '</tbody>';
-  $content .= '</table>';
-  
-  return $content;
-}
-
-/**
- * Get list of mail servers.
- */
-function mail_api_get_servers() {
-  $result = db_query("SELECT * FROM {mail_api_servers}");
-  
-  $servers = array();
-  
-  while($server = db_fetch_array($result)) {
-    $servers[] = $server;
-  }
-  
-  return $servers;
-}
-
-/**
- * Add new mail server configuration.
- */
-function mail_api_add_server() {
-  $protocols = mail_api_protocols();
-  return theme('mail_api_add_server', $protocols);
-}
-
-/**
- * Theme list of mail server protocols.
- */
-function theme_mail_api_add_server($protocols) {
-  $content = t('No supported protocols found.<br>You must install a module that will provide a protocol (i.e. IMAP) be able to configure Mail API to connect to a mail server.');
-  
-  if ($protocols) {
-    $content = '<dl class="admin-list">';
-    foreach ($protocols as $name => $value) {
-      $content .= '<dt>' . l($name, 'admin/settings/mail_api/servers/add/' . $name, array('title' => $protocols[$name]['description'])) . '</dt>';
-      $content .= '<dd>' . $protocols[$name]['description'] . '</dd>';
-    }
-    $content .= '</dl>';
-  }
-  
-  return $content;
-}
-
-/**
- * Create a form for adding new mail server configuration to database.
- */
-function mail_api_form_add_server($p_name) {
-  $encryption = mail_api_encryption_options();
-  
-  $protocols = mail_api_protocols();
-  foreach($protocols as $name => $value) {
-    if($name == $p_name['name']) {
-      break;
-    }
-  }
-  
-  $form['mail_api'] = array(
-    '#title' => t('Mail Server Information'),
-    '#type' => 'fieldset',
-  );
 
-  $form['mail_api']['mail_api_protocol'] = array(
-    '#type' => 'hidden',
-    '#value' => $name,
-  );
-  $form['mail_api']['mail_api_protocol_module'] = array(
-    '#type' => 'hidden',
-    '#value' => $protocols[$name]['module'],
-  );
-
-  $form['mail_api']['mail_api_hostname'] = array(
-    '#title' => t('Server Hostname'),
-    '#type' => 'textfield',
-    '#required' => TRUE,
-  );
-  $form['mail_api']['mail_api_port'] = array(
-    '#title' => t('Server Port'),
-    '#type' => 'textfield',
-    '#required' => TRUE,
-  );
-  $form['mail_api']['mail_api_encryption_values'] = array(
-    '#type' => 'value',
-    '#value' => $protocols[$name]['encryption'],
-  );
-  $form['mail_api']['mail_api_encryption'] = array(
-    '#title' => t('Encryption'),
-    '#type' => 'select',
-    '#options' => $form['mail_api']['mail_api_encryption_values']['#value'],
-  );
-  $form['mail_api']['mail_api_domain'] = array(
-    '#title' => t('Domain'),
-    '#type' => 'textfield',
-    '#description' => t('The domain name in the users email address.  This is how Mail API can determine which server module to use for a user to access their email.'),
-  );
-  $form['mail_api']['mail_api_outbox'] = array(
-    '#title' => t('Outbox'),
-    '#type' => 'textfield',
-    '#disabled' => !$protocols[$name]['outbox'],
-    '#description' => t("This is the path to the 'Outbox' folder, if the mail server supports sending emails in this manner.  If this folder does not exist in the user's account, it will be created.  Any SMTP settings will be ignored if this is used."),
-  );
-
-  $form['smtp'] = array(
-    '#title' => t('SMTP Server Configuration'),
-    '#type' => 'fieldset',
-  );
-  $form['smtp']['primary'] = array(
-    '#title' => t('Primary SMTP Server'),
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['smtp']['primary']['smtp_primary_hostname'] = array(
-    '#title' => t('Hostname'),
-    '#type' => 'textfield',
-  );
-  $form['smtp']['primary']['smtp_primary_port'] = array(
-    '#title' => t('Port'),
-    '#type' => 'textfield',
-  );
-  $form['smtp']['primary']['smtp_primary_encryption_values'] = array(
-    '#type' => 'value',
-    '#value' => $encryption,
-  );
-  $form['smtp']['primary']['smtp_primary_encryption'] = array(
-    '#title' => t('Encryption'),
-    '#type' => 'select',
-    '#options' => $form['smtp']['primary']['smtp_primary_encryption_values']['#value'],
-  );
-  $form['smtp']['primary']['auth'] = array(
-    '#title' => t('SMTP Authentication'),
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['smtp']['primary']['auth']['smtp_primary_auth'] = array(
-    '#title' => t('Enable SMTP Authentication'),
-    '#type' => 'checkbox',
-    '#default_value' => FALSE,
-    '#description' => t('Leave username and password blank to use user credentials.'),
-  );
-  $form['smtp']['primary']['auth']['smtp_primary_username'] = array(
-    '#title' => t('Username'),
-    '#type' => 'textfield',
-    '#description' => t('Remove user name to clear password.'),
-  );
-  $form['smtp']['primary']['auth']['smtp_primary_password'] = array(
-    '#title' => t('Password'),
-    '#type' => 'password',
-    '#description' => t('Leave this blank unless setting a new password.'),
-  );
-
-  $form['smtp']['backup'] = array(
-    '#title' => t('Backup SMTP Server'),
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['smtp']['backup']['smtp_backup_hostname'] = array(
-    '#title' => t('Hostname'),
-    '#type' => 'textfield',
-  );
-  $form['smtp']['backup']['smtp_backup_port'] = array(
-    '#title' => t('Port'),
-    '#type' => 'textfield',
-  );
-  $form['smtp']['backup']['smtp_backup_encryption_values'] = array(
-    '#type' => 'value',
-    '#value' => $encryption,
-  );
-  $form['smtp']['backup']['smtp_backup_encryption'] = array(
-    '#title' => t('Encryption'),
-    '#type' => 'select',
-    '#options' => $form['smtp']['backup']['smtp_backup_encryption_values']['#value'],
-  );
-  $form['smtp']['backup']['auth'] = array(
-    '#title' => t('SMTP Authentication'),
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['smtp']['backup']['auth']['smtp_backup_auth'] = array(
-    '#title' => t('Enable SMTP Authentication'),
-    '#type' => 'checkbox',
-    '#default_value' => FALSE,
-    '#description' => t('Leave username and password blank to use user credentials.'),
-  );
-  $form['smtp']['backup']['auth']['smtp_backup_username'] = array(
-    '#title' => t('Username'),
-    '#type' => 'textfield',
-    '#description' => t('Remove user name to clear password.'),
-  );
-  $form['smtp']['backup']['auth']['smtp_backup_password'] = array(
-    '#title' => t('Password'),
-    '#type' => 'password',
-    '#description' => t('Leave this blank unless setting a new password.'),
+  $items['admin/settings/mail_api/add'] = array(
+    'title' => 'Add Server',
+    'page arguments' => array('add'),
+    'access arguments' => array('administer mail_api'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'mail_api.admin.inc',
   );
-  
-  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
-  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
-    
-  return $form;
-}
 
-/**
- * Validate new mail server configuration form.
- */
-function mail_api_form_add_server_validate($form, &$form_state) {
-  if(!$form_state['values']['mail_api_hostname']) {
-    form_set_error('mail_api][hostname', t('Please enter a hostname or ip address.'));
-  }
-  if(!is_numeric($form_state['values']['mail_api_port'])) {
-    form_set_error('mail_api][port', t('Please enter a port number.'));
-  }
-}
 
-/**
- * Insert new mail server configuration into database.
- */
-function mail_api_form_add_server_submit($form, &$form_state) {
-  if (module_exists('aes')) {
-    $smtp_primary_password = aes_encrypt($form_state['values']['smtp_primary_password']);
-    $smtp_backup_password = aes_encrypt($form_state['values']['smtp_backup_password']);;
-  }
-  else {
-    $smtp_primary_password = $form_state['values']['smtp_primary_password'];
-    $smtp_backup_password = $form_state['values']['smtp_backup_password'];
-  }
-  
-  db_query(
-    'INSERT INTO {mail_api_servers} (protocol, protocol_module, hostname, port, encryption, domain, outbox, smtp_primary_hostname, smtp_primary_port, smtp_primary_encryption, smtp_primary_auth, smtp_primary_username, smtp_primary_password, smtp_backup_hostname, smtp_backup_port, smtp_backup_encryption, smtp_backup_auth, smtp_backup_username, smtp_backup_password) VALUES ("%s", "%s", "%s", %d, %d, "%s", "%s", "%s", %d, %d, "%s", "%s", "%s", "%s", %d, %d, "%s", "%s", "%s")',
-    $form_state['values']['mail_api_protocol'],
-    $form_state['values']['mail_api_protocol_module'],
-    $form_state['values']['mail_api_hostname'],
-    (int) $form_state['values']['mail_api_port'],
-    $form_state['values']['mail_api_encryption'],
-    $form_state['values']['mail_api_domain'],
-    $form_state['values']['mail_api_outbox'],
-    $form_state['values']['smtp_primary_hostname'],
-    (int) $form_state['values']['smtp_primary_port'],
-    $form_state['values']['smtp_primary_encryption'],
-    $form_state['values']['smtp_primary_username'],
-    $smtp_primary_password,
-    $form_state['values']['smtp_primary_auth'],
-    $form_state['values']['smtp_backup_hostname'],
-    (int) $form_state['values']['smtp_backup_port'],
-    $form_state['values']['smtp_backup_encryption'],
-    $form_state['values']['smtp_backup_username'],
-    $smtp_backup_password,
-    $form_state['values']['smtp_backup_auth']
+  $items['admin/settings/mail_api/edit'] = array(
+    'title' => 'Edit Server',
+    'page callback' => 'mail_api_edit_server',
+    'access arguments' => array('administer mail_api'),
+    'type' => MENU_CALLBACK,
+    'file' => 'mail_api.admin.inc',
   );
 
-  drupal_set_message(t('The server configuration has been saved.'));
-  
-  $form_state['redirect'] = 'admin/settings/mail_api/servers';
+  return $items;
 }
 
-/**
- * Create form for editing a mail server configuration.
- */
-function mail_api_form_edit_server($form_state, $server_id) {
-  $server = db_fetch_object(db_query("SELECT * FROM {mail_api_servers} WHERE id = %d", $server_id));
-  $form = mail_api_form_add_server($server->protocol);
-  
-  $form['mail_api']['mail_api_server_id'] = array(
-    '#type' => 'hidden',
-    '#value' => (int) $server_id,
-  );
-  
-  $form['mail_api']['mail_api_hostname']['#default_value'] = $server->hostname;
-  $form['mail_api']['mail_api_port']['#default_value'] = $server->port;
-  $form['mail_api']['mail_api_encryption']['#default_value'] = $server->encryption;
-  $form['mail_api']['mail_api_domain']['#default_value'] = $server->domain;
-  $form['mail_api']['mail_api_outbox']['#default_value'] = $server->outbox;
-  
-  $form['smtp']['primary']['smtp_primary_hostname']['#default_value'] = $server->smtp_primary_hostname;
-  $form['smtp']['primary']['smtp_primary_port']['#default_value'] = $server->smtp_primary_port;
-  $form['smtp']['primary']['smtp_primary_encryption']['#default_value'] = $server->smtp_primary_encryption;
-  $form['smtp']['primary']['auth']['smtp_primary_auth']['#default_value'] = $server->smtp_primary_auth;
-  $form['smtp']['primary']['auth']['smtp_primary_username']['#default_value'] = $server->smtp_primary_username;
-  
-  $form['smtp']['backup']['smtp_backup_hostname']['#default_value'] = $server->smtp_backup_hostname;
-  $form['smtp']['backup']['smtp_backup_port']['#default_value'] = $server->smtp_backup_port;
-  $form['smtp']['backup']['smtp_backup_encryption']['#default_value'] = $server->smtp_backup_encryption;
-  $form['smtp']['backup']['auth']['smtp_backup_auth']['#default_value'] = $server->smtp_backup_auth;
-  $form['smtp']['backup']['auth']['smtp_backup_username']['#default_value'] = $server->smtp_backup_username;
-
-  if($server->smtp_primary_hostname) {
-    $form['smtp']['primary']['#collapsed'] = FALSE;
-  }
-  if($server->smtp_primary_auth) {
-    $form['smtp']['primary']['auth']['#collapsed'] = FALSE;
-  }
-  if($server->smtp_backup_hostname) {
-    $form['smtp']['backup']['#collapsed'] = FALSE;
-  }
-  if($server->smtp_backup_auth) {
-    $form['smtp']['backup']['auth']['#collapsed'] = FALSE;
-  }
 
-  $form['buttons']['delete'] = array(
-    '#type' => 'submit',
-    '#value' => t('Delete'),
-    '#submit' => array('mail_api_form_edit_delete_server'),
-  );
-  
-  return $form;
-}
 
 /**
- * Validate the edited mail server configuration form.
- */
-function mail_api_form_edit_server_validate($form, &$form_state) {
-  if(!$form_state['values']['mail_api_hostname']) {
-    form_set_error('mail_api][hostname', t('Please enter a hostname or ip address.'));
-  }
-  if(!is_numeric($form_state['values']['mail_api_port'])) {
-    form_set_error('mail_api][port', t('Please enter a port number.'));
+ * Implementation of hook_help()
+ *
+ * @param unknown_type $path
+ * @param unknown_type $arg
+ * @return unknown
+ */
+function mail_api_help($path, $arg) {
+  switch ($path) {
+    case 'admin/help#mail_api':
+      return t('Implements API for all mail-related functions. This module doesn\'t do anything by itself');
   }
 }
 
 /**
- * Update edited mail server configuration in database.
+ * Implementation of hook_theme().
  */
-function mail_api_form_edit_server_submit($form, &$form_state) {
-  if (module_exists('aes')) {
-    $smtp_primary_password = aes_encrypt($form_state['values']['smtp_primary_password']);
-    $smtp_backup_password = aes_encrypt($form_state['values']['smtp_backup_password']);;
-  }
-  else {
-    $smtp_primary_password = $form_state['values']['smtp_primary_password'];
-    $smtp_backup_password = $form_state['values']['smtp_backup_password'];
-  }
-  
-  $query = 'UPDATE {mail_api_servers} SET hostname = "%s", port = %d, encryption = "%s", domain = "%s", outbox = "%s", smtp_primary_hostname = "%s", smtp_primary_port = "%s", smtp_primary_encryption = "%s", smtp_primary_auth = "%s", smtp_primary_username = "%s", smtp_backup_hostname = "%s", smtp_backup_port = "%s", smtp_backup_encryption = "%s", smtp_backup_auth = "%s", smtp_backup_username = "%s"';
-  
-  db_query(
-    $query,
-    $form_state['values']['mail_api_hostname'],
-    (int) $form_state['values']['mail_api_port'],
-    $form_state['values']['mail_api_encryption'],
-    $form_state['values']['mail_api_domain'],
-    $form_state['values']['mail_api_outbox'],
-    $form_state['values']['smtp_primary_hostname'],
-    (int) $form_state['values']['smtp_primary_port'],
-    $form_state['values']['smtp_primary_encryption'],
-    $form_state['values']['smtp_primary_auth'],
-    $form_state['values']['smtp_primary_username'],
-    $form_state['values']['smtp_backup_hostname'],
-    (int) $form_state['values']['smtp_backup_port'],
-    $form_state['values']['smtp_backup_encryption'],
-    $form_state['values']['smtp_backup_auth'],
-    $form_state['values']['smtp_backup_username']
+function mail_api_theme() {
+  return array(
+    'mail_api_settings_overview' => array(
+      'arguments' => array('form' => NULL),
+    ),
   );
-  
-  $primary_pass_changed = (empty($form_state['values']['smtp_primary_password'])) ? FALSE : TRUE;
-  $backup_pass_changed = (empty($form_state['values']['smtp_primary_password'])) ? FALSE : TRUE;
-
-  if($primary_pass_changed && $backup_pass_changed) {
-    db_query('UPDATE {mail_api_servers} SET smtp_primary_password = "%s", smtp_backup_password = "%s"', $smtp_primary_password, $smtp_backup_password);
-    drupal_set_message(t('Updated smtp server passwords.'));
-  }
-  else if($primary_pass_changed) {
-    db_query('UPDATE {mail_api_servers} SET smtp_primary_password = "%s"', $smtp_primary_password);
-    drupal_set_message(t('Updated primary smtp server password.'));
-  }
-  else if($backup_pass_changed) {
-    db_query('UPDATE {mail_api_servers} SET smtp_backup_password = "%s"', $smtp_backup_password);
-    drupal_set_message(t('Updated backup smtp server password.'));
-  }
-
-  drupal_set_message(t('The server configuration has been saved.'));
-  
-  $form_state['redirect'] = 'admin/settings/mail_api/servers';
-}
-
-/**
- * Simple form for deleting mail server configuration.
- */
-function mail_api_form_delete_server($form_state, $server_id) {
-  $server = db_fetch_object(db_query("SELECT * FROM {mail_api_servers} WHERE id = %d", $server_id));
-  
-  if(empty($server)) {
-    drupal_set_message(t('Server does not exist.'));
-    drupal_goto('admin/settings/mail_api/servers');
-    return FALSE;
-  }
-  else {
-    $form['mail_api']['mail_api_server_id'] = array(
-      '#type' => 'hidden',
-      '#value' => (int) $server_id,
-    );
-    return confirm_form($form, t('Delete server configuration for %hostname?', array('%hostname' => $server->hostname)), 'admin/settings/mail_api/servers', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
-  }
-}
-
-/**
- * Redirect to mail_api_form_delete_server().
- * 
- * This is called from the mail_api_form_edit_server() page.
- */
-function mail_api_form_edit_delete_server(&$form_state) {
-  $server_id = $form_state['mail_api']['mail_api_server_id']['#value'];
-  drupal_goto('admin/settings/mail_api/servers/'. $server_id. '/delete');
-  return FALSE;
-}
-
-/**
- * Delete mail server configuration from database.
- */
-function mail_api_form_delete_server_submit($form, &$form_state) {
-  $sid = $form_state['values']['mail_api_server_id'];
-
-  $result = db_query('DELETE FROM {mail_api_servers} WHERE id = %d', $sid);
-  
-  if(empty($result)) {
-    drupal_set_message(t('There was an error when attempting to delete server configuration.'));
-  }
-  else {
-    drupal_set_message(t('Server configuration deleted.'));
-  }
-  $form_state['redirect'] = 'admin/settings/mail_api/servers';
-}
-
-/**
- * Invoke Mail API hook in a module based on users mail domain.
- */
-function mail_api_invoke() {
-  $args = func_get_args();
-  
-  if ($domain = mail_api_get_domain()) {  
-    $server_info = mail_api_server_configuration($domain);
-    
-    array_unshift($args, $server_info['protocol_module']);
-    
-    return call_user_func_array('module_invoke', $args);
-  }
-}
-
-/**
- * Get the domain name from the user's email address.
- */
-function mail_api_get_domain() {
-  global $user;
-
-  if ($_SESSION['mail_api']) {
-    $domain = end(explode('@', $user->name));
-
-    return $domain;
-  }
-  return FALSE;
-}
-
-/**
- * Implementation of hook_init().
- */
-function mail_api_init() {
-  global $user;
-  
-  if($domain = mail_api_get_domain()) {
-    // if we find the user's mail server domain name
-    // get the server's configuration information
-    $server_info = mail_api_server_configuration($domain);
-    if ($auth = mail_api_invoke('mail_api_authenticate', $user->name, mail_api_get_password(), $domain, $server_info)) {
-      // if successfully authenticated
-      // register a shutdown function to log out of the mail server
-      // at the end of this page load.
-      register_shutdown_function('mail_api_invoke', 'mail_api_logout');
-    }
-  }
-}
-
-/**
- * Implementation of hook_user().
- */
-function mail_api_user($op, &$edit, &$account, $category = NULL) {
-  switch($op) {
-    case 'login':
-      // if logging in
-      // rebuild keyword cache
-      mail_api_rebuild_folder_cache();
-      // rebuild tag cache
-      mail_api_rebuild_tag_cache();
-      break;
-    case 'logout':
-      // if logging out
-      // clear all mail server session information
-      unset($_SESSION['mail_api']);
-      break;
-  }
-}
-
-/**
- * Get configured server configuration by domain name from the database
- */
-function mail_api_server_configuration($domain) {
-  $result = db_fetch_array(db_query("SELECT * FROM {mail_api_servers} WHERE domain = '%s'", $domain));
-  
-  return $result;
-}
-
-/**
- * Attempt to authenticate the user against the appropriate mail server.
- */
-function mail_api_authenticate($form_values = array('name' => '', 'pass' => '')) {
-  global $user;
-  
-  if(!valid_email_address($form_values['name']) || empty($form_values['pass'])) {
-    return FALSE;
-  }
-  
-  $domain = end(explode('@', $form_values['name']));
-
-  $server_info = mail_api_server_configuration($domain);
-  if (module_invoke($server_info['protocol_module'], 'mail_api_authenticate', $form_values['name'], $form_values['pass'], $domain, $server_info)) {
-    user_external_login_register($form_values['name'], 'mail_api');
-    user_authenticate_finalize($form_values);
-    $_SESSION['mail_api'] = array(
-      'protocol_module' => $server_info['protocol_module'],
-    );
-    mail_api_set_password($form_values['pass']);
-    return $user;
-  }
-  return FALSE;
-}
-
-/**
- * Get the user's password from the $_SESSION variable.
- */
-function mail_api_get_password() {
-  if($_SESSION['mail_api']['enc'] && module_exists('aes')) {
-    // if the AES module is installed, and the 'enc' session variable
-    // is TRUE, use aes_decrypt to decrypt the module.
-    return aes_decrypt($_SESSION['mail_api']['pass']);
-  }
-  else {
-    // otherwise just return the password stored in the session.
-    return $_SESSION['mail_api']['pass'];
-  }
-}
-
-/**
- * Set the user's password in the $_SESSION variable.
- */
-function mail_api_set_password($password) {
-  if(module_exists('aes') && $_SESSION['mail_api']['pass'] = aes_encrypt($password)) {
-    // if the AES module is installed, and setting the password for the
-    // session in encrypted form works:
-    $_SESSION['mail_api']['enc'] = TRUE;
-  }
-  else {
-    // otherwise set the password in the session variable in plain text
-    $_SESSION['mail_api']['pass'] = $password;
-    $_SESSION['mail_api']['enc'] = FALSE;
-  }
-}
-
-/**
- * Return array of encryption options for SMTP
- */
-function mail_api_encryption_options() {
-  return array(t('None'), t('SSL'), t('TLS'));
-}
-
-/**
- * Return list of supported protocols.
- */
-function mail_api_protocols() {
-  $protocols = module_invoke_all('mail_api_protocols');
-  
-  return $protocols;
-}
-
-/**
- * Return the quota for the mail account of the user.
- */
-function mail_api_account_quota() {
-  return mail_api_invoke('mail_api_account_quota');
-}
- 
-/**
- * Get user's folders from the server and store them in the database
- */
-function mail_api_rebuild_folder_cache() {
-  global $user;
-  
-  // get the user's folders from the server
-  if($folders = mail_api_invoke('mail_api_folders')) {
-    // and store them in the database
-    db_query('DELETE FROM {mail_api_folder_cache} WHERE uid = %d', $user->uid);
-    foreach($folders as $folder) {
-      db_query('INSERT INTO {mail_api_folder_cache} (path, quota, quota_used, num_messages, num_recent, uid) VALUES ("%s", %d, %d, %d, %d, %d)', $folder['path'], $folder['quota'], $folder['quota_used'], $folder['num_messages'], $folder['num_recent'], $user->uid);
-    }
-  }
-}
-
-/**
- * Get folders from cache.
- * 
- * @return: Return an array of folder information.
- */
-function mail_api_folders() {
-  global $user;
-  // get folders from cache
-  $result = db_query('SELECT * FROM {mail_api_folder_cache} WHERE uid = %d', $user->uid);
-  while ($folder = db_fetch_array($result)) {
-    $folders[] = $folder;
-  }
-  
-  return $folders;
-}
-
-/**
- * Create new folder in user's mail server account
- */
-function mail_api_create_folder($folder_path) {
-  // Call hook_mail_api_create_folder() on the appropriate mail server module.
-  $success = mail_api_invoke('mail_api_create_folder', $folder_path);
-  
-  // Rebuild the folder cache.
-  if ($success) {
-    mail_api_rebuild_folder_cache();
-  }
-  
-  return $success;
-}
-
-/**
- * Move/rename folder in user's mail server account
- */
-function mail_api_move_folder($folder_path, $new_folder_path) {
-  // Call hook_mail_api_move_folder() on the appropriate mail server module.
-  $success = mail_api_invoke('mail_api_move_folder', $folder_path, $new_folder_path);
-  
-  // Rebuild the folder cache.
-  if ($success) {
-    mail_api_rebuild_folder_cache();
-  }
-  
-  return $success;
-}
-
-/**
- * Delete folder in user's mail server account
- */
-function mail_api_delete_folder($folder_path) {
-  // Call hook_mail_api_delete_folder() on the appropriate mail server module.
-  $success = mail_api_invoke('mail_api_delete_folder', $folder_path);
-  
-  // Rebuild the folder cache.
-  if ($success) {
-    mail_api_rebuild_folder_cache();
-  }
-  
-  return $success;
-}
-
-/**
- * Get all tags from mail server and store them in the cache.
- */
-function mail_api_rebuild_tag_cache() {
-  global $user;
-  
-  // get user's tags from the mail server
-  if($tags = mail_api_invoke('mail_api_tags')) {
-    // store them in the database
-    db_query('DELETE FROM {mail_api_tag_cache} WHERE uid = %d', $user->uid);
-    foreach($tags as $tag) {
-      db_query('INSERT INTO {mail_api_tag_cache} (uid, name) VALUES (%d, "%s")', $user->uid, $tag['name']);
-    }
-  }
-}
-
-/**
- * Get tags from cache.
- * 
- * @return: Returns an array of tags.
- */
-function mail_api_tags() {
-  global $user;
-  $result = db_query('SELECT * FROM {mail_api_tag_cache} WHERE uid = %d', $user->uid);
-  
-  while($tag = db_fetch_array($result)) {
-    $tags[] = $tag;
-  }
-  
-  return $tags;
 }
 
-/**
- * Rename a tag.
- */
-function mail_api_rename_tag($tag, $new_tag) {
-  // Call hook_mail_api_rename_tag() on the appropriate mail server module.
-  $success = mail_api_invoke('mail_api_rename_tag', $tag, $new_tag);
-  
-  // Rebuild the tag cache.
-  if ($success) {
-    mail_api_rebuild_tag_cache();
-  }
-  
-  return $success;
-}
 
-/**
- * Delete a tag.
- */
-function mail_api_delete_tag($tag) {
-  // Call hook_mail_api_delete_tag() on the appropriate mail server module.
-  $success = mail_api_invoke('mail_api_delete_tag', $tag);
-  
-  // Rebuild the tag cache.
-  if ($success) {
-    mail_api_rebuild_tag_cache();
-  }
-  
-  return $success;
-}
 
-/**
- * Get a page of results from a search.
- */
-function mail_api_pager_search($keywords = '', $folders = array(), $flags = array(), $header_fields = array(), $tags = array(), $sections = array(), $sort = array(), $limit = 25, $element = 0) {
-  global $pager_page_array, $pager_total, $pager_total_items;
-  $page = isset($_GET['page']) ? $_GET['page'] : '';
-  
-  // Call hook_mail_api_pager_search().
-  $search_results = mail_api_invoke('mail_api_pager_search', $keywords, $folders, $flags, $header_fields, $tags, $sections, $sort, $limit);
-  
-  $pager_page_array = explode(',', $page);
-  
-  $pager_total_items[$element] = $search_results['count'];
-  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
-  $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
-  
-  return $search_results;
-}
 
-/**
- * Send an email.
- */
-function mail_api_send($message) {
-  global $user;
-  
-  // Get domain name of user's account.
-  $domain = mail_api_get_domain();
-  // Get server configuration for that domain.
-  $server = mail_api_server_configuration($domain);
-  
-  if($server->outbox) {
-    // if the mail server supports sending messages, do this
-    return mail_api_invoke('mail_api_send', $message);
-  }
-  else if (module_exists('smtp')) {
-    // this is based off the drupal_mail_wrapper() function from the SMTP Auth module
-    // we could not use that function as is, however, since it uses site-wide settings
-    // and we are using mail server specific and user specific settings
-    
-    $mail = new phpmailer(); //Create a new phpmailer object.
-    $username = $server->smtp_primary_username;
-    $password = $server->smtp_primary_password;
-    
-    $from_name = $user->name;
-    $from = $user->name;
-    
-    $auth = $server->smtp_primary_auth ? TRUE : FALSE;
-     
-    if(!$username) {
-      // if there's not a smtp username set for this mail server
-      // use the user's username and password
-      $mail->Username = $user->name;
-      $mail->Password = mail_api_get_password();
-    }
-    else {
-      // else use the configured smtp username/passwords for this server.
-      $mail->Username = $username;
-      $mail->Password = $password;
-    }
-    
-    //Take care of the email headers.
-    foreach ($message['headers'] as $name => $value) {
-      //watchdog('error', 'Key: ' . $name . ' Value: ' . $value);
-      if (strtolower($name) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
-        $mail->IsHTML(TRUE);
-      }
-      else if (strtolower($name) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
-        //$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
-        $mail->AddCustomHeader($name . ': ' . $value);
-        $mail->message_type = 'pre';
-      }
-      else if (strtolower($name) == 'reply-to') {
-        $mail->AddReplyTo = $value;
-      }
-      else if (strtolower($name) == 'return-path') {
-        if (trim($value) !=  '') {
-          $mail->Sender = $value;
-        }
-      }
-      else if (strtolower($name) == 'content-transfer-encoding') {
-        $mail->Encoding = $value;
-      }
-      else if (strtolower($name) == 'mime-version') {
-        // just ommit MIME-Version it since it will be set by PHP-Mailer
-      }
-      else if (strtolower($name) == 'bcc') {
-        $bccrecipients = split(',', $value);
-        foreach ($bccrecipients as $bccrecipient) {
-          if ( strpos($bccrecipient, '<') !== false ) {
-            $bccparts = explode(' <', $bccrecipient);
-            $bccname = $bccparts[0];
-            $bccaddr = rtrim($bccparts[1], '>');
-          }
-          else {
-            $bccname = '';
-            $bccaddr = $bccrecipient;
-          }
-          $mail->AddBCC($bccaddr, $bccname);
-        }
-      }
-      else { //Else the header key is not special, just add it.
-        $mail->AddCustomHeader($name . ': ' . $value); //Add header line.
-      }
-    }
-  
-    $enc_options = mail_api_encryption_options();
-    switch(strtolower($enc_options[$server->smtp_primary_encryption])) {
-      case 'ssl':
-        $mail->Protocol = 'ssl://';
-        break;
-      case 'tls':
-        $mail->Protocol = 'tls://';
-        break;
-      case 'none':
-        $mail->Protocol = '';
-    }
-    
-    $mail->Host = $server->smtp_primary_hostname;
-    $mail->Port = $server->smtp_primary_port;
-    $mail->Mailer = 'smtp';
-    $mail->SMTPAuth = $auth;
-    
-    $mail->CharSet = 'utf-8';
-    
-    $mail->From = $from;
-    $mail->FromName = $from_name;
-    
-    $torecipients = split(',', $message['to']);
-    foreach ($torecipients as $torecipient) {
-      if (strpos($torecipient, '<') !== false) {
-        $toparts = explode(' <', $torecipient);
-        $toname = $toparts[0];
-        $toaddr = rtrim($toparts[1], '>');
-      }
-      else {
-        $toname = "";
-        $toaddr = $torecipient;
-      }
-      $mail->AddAddress($toaddr, $toname);
-    }
-  
-    $mail->Subject = $message['subject'];
-    $mail->Body = $message['body'];
-    
-    watchdog('mail_api', t('Sending mail to: !to', array('!to' => $message['to'])));
-  
-    //Try to send email, if it fails set watchdog entry.
-    if(!$mail->Send()) {
-      watchdog('mail_api', t('Error sending e-mail from !from to !to: .  Attempting any configured backup mail delivery methods.', array('!from' => $from, '!to' => $message['to'])) . $mail->ErrorInfo, NULL, WATCHDOG_ERROR);
-      
-      // change to backup smtp server information
-      $mail->Host = $server->smtp_backup_hostname;
-      $mail->Port = $server->smtp_backup_port;
-      
-      switch(strtolower($enc_options[$server->smtp_backup_encryption])) {
-        case 'ssl':
-          $mail->Protocol = 'ssl://';
-          break;
-        case 'tls':
-          $mail->Protocol = 'tls://';
-          break;
-        case 'none':
-          $mail->Protocol = '';
-      }
-      
-      $username = $server->smtp_backup_username;
-      $password = $server->smtp_backup_password;
-      $auth = $server->smtp_backup_auth ? TRUE : FALSE;
-      
-      if($username == '' && $password == '') {
-        $mail->Username = $user->name;
-        $mail->Password = mail_api_get_password();
-      }
-      else {
-        $mail->Username = $username;
-        $mail->Password = $password;
-      }
-      
-      $mail->SMTPAuth = $auth;
-      
-      if(!$mail->Send()) {
-        watchdog('mail_api', t('Error sending e-mail from !from to !to: from backup SMTP server.', array('!from' => $from, '!to' => $message['to'])) . $mail->ErrorInfo, NULL, WATCHDOG_ERROR);
-        return FALSE;
-      }
-    }
-    
-    $mail->SmtpClose();
-    return TRUE;
-  }
-  else {
-    // error: no supported methods for sending emails
-    watchdog('mail_api', t('Error: no supported/configured methods for sending emails.'), NULL, WATCHDOG_ERROR);
-    return FALSE;
-  }
-}
\ No newline at end of file
+require_once "mail_api.hooks.inc";
\ No newline at end of file
