You are here

function mailhandler_mailbox_test in Mailhandler 6

Test the connection to a mailbox.

Attempt a mailbox connection and set form errors. Even if this function is not used in a form (e.g. testing 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_test($mailbox); if ($errors = form_get_errors()) { do_my_error_handling($errors); } else { mailhandler_mailbox_save($mailbox); } </code>

This function can be called safely from within a form.

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_test()
mailhandler_mailbox_test_submit in ./mailhandler.admin.inc
Submit handler for Test mailbox button on mailhandler_add_edit_mailbox form.

File

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

Code

function mailhandler_mailbox_test($mailbox, $form = array(), &$form_state = array()) {
  $result = FALSE;

  // Verify the mailbox by connecting and getting mailbox status.
  if ($stream = mailhandler_mailbox_stream_open($mailbox)) {

    // Close stream to mailbox.
    mailhandler_mailbox_stream_close($stream);
    $result = TRUE;
  }
  else {
    if ($mailbox['domain']) {
      form_set_error('mailhandler', t('%c connection to %m failed.', array(
        '%c' => $mailbox['imap'] ? 'IMAP' : 'POP3',
        '%m' => $mailbox['mail'],
      )));
    }
    else {
      form_set_error('mailhandler', t('Mailhandler could not access local folder: %m', array(
        '%m' => $mailbox['mail'],
      )));
    }
  }
  return $result;
}