You are here

function mailhandler_mailbox_validate in Mailhandler 6

Validate a Mailhandler Mailbox.

Validate the mailbox and set form errors. Even if this function is not used in a form (e.g. validating mailboxes created programatically), the function will still set form errors. These errors can be later recalled using the function form_get_errors().

<code> mailhandler_mailbox_validate($mailbox); if ($errors = form_get_errors()) { do_my_error_handling($errors); } else { mailhandler_mailbox_save($mailbox); } </code>

Parameters

$mailbox: A mailbox settings array.

$form: The $form where mailbox is being edited.

$form_state: The $form_state values of the form being validated.

Related topics

1 call to mailhandler_mailbox_validate()
mailhandler_add_edit_mailbox_validate in ./mailhandler.admin.inc
Validation hook for mailhandler_add_edit_mailbox() form.

File

./mailhandler.module, line 328
Mailhandler module code.

Code

function mailhandler_mailbox_validate($mailbox, $form = array(), &$form_state = array()) {

  // Check for valid email address.
  if ($error = user_validate_mail($mailbox['mail'])) {
    form_set_error('mail', $error);
  }

  // Set form error in case the email address does not comply email format.
  if ($form_state['values']['mailto'] && ($error = user_validate_mail($mailbox['mailto']))) {
    form_set_error('mailto', $error);
  }

  // Test POP/IMAP settings, and store result.
  $mailbox_appears_ok = TRUE;

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

  // Assume local folder.
  if (!$mailbox['domain'] && !$mailbox['port'] && $mailbox['folder']) {

    // check read and write permission
    if (!is_readable($mailbox['folder']) || !is_writable($mailbox['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;
    }
  }

  // If POP3 mailbox is chosen, messages must be deleted after processing or
  // they will be imported on each retrieval process. Warm about this case when
  // trying to save the mailbox.
  if ($mailbox['imap'] == 0 && $mailbox['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.'));
  }
}