You are here

function mailhandler_add_edit_mailbox_validate in Mailhandler 7

Same name and namespace in other branches
  1. 6 mailhandler.admin.inc \mailhandler_add_edit_mailbox_validate()

Verify that the Mailbox is valid, and save it to the database.

File

./mailhandler.admin.inc, line 194

Code

function mailhandler_add_edit_mailbox_validate($form, &$form_state) {

  // Check for valid email address
  if ($error = user_validate_mail($form_state['values']['mail'])) {
    form_set_error('mail', $error);
  }
  if ($form_state['values']['mailto'] && ($error = user_validate_mail($form_state['values']['mailto']))) {
    form_set_error('mailto', $error);
  }

  // Test POP/IMAP settings, and store result
  $mailbox_appears_ok = TRUE;
  if ($form_state['values']['domain'] && $form_state['values']['port'] && !is_numeric($form_state['values']['port'])) {

    // assume external mailbox
    form_set_error('port', t('Mailbox port must be an integer.'));
    $mailbox_appears_ok = FALSE;
  }
  if (!$form_state['values']['domain'] && !$form_state['values']['port'] && $form_state['values']['folder']) {

    // assume local folder
    // check read and write permission
    if (!is_readable($form_state['values']['folder']) || !is_writable($form_state['values']['folder'])) {
      form_set_error('port', t('The local folder has to be readable and writable by owner of the webserver process, e.g. nobody.'));
      $mailbox_appears_ok = FALSE;
    }
  }

  // Test POP/IMAP settings are valid in all cases, and test connection if requested
  if ($mailbox_appears_ok && $form_state['clicked_button']['#value'] == t('Test connection')) {

    // Call the test function
    mailhandler_test_mailbox($form_state);

    // We don't want to save, so we set an 'invisible error' that means the form is not submitted
    form_set_error('mailhandler');
  }

  // If POP3 mailbox is chosen, messages must be deleted after processing.
  if ($form_state['values']['imap'] == 0 && $form_state['values']['delete_after_read'] == 0) {
    form_set_error('delete_after_read', t('You must check off "Delete messages after they are processed" when using a POP3 mailbox.'));
  }
}