Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I would like to code a way for a user to request the activation email be resent. Is there a function I can call from within a form-submit hook that will accomplish this?

(using Drupal 7)

share|improve this question
you mean want the admin of the site to resend an activation email to a specific user?! – Aboodred1 Apr 22 at 16:17
No. I would like to create a 'trouble logging in' page, where a unactive user can request the activation email to be resent based on email address – Damon Apr 22 at 16:51

1 Answer

First you need to create a custom form page using hook_menu() to allow users to enter their emails.

 /**
 * Implements hook_menu().
 */
function custom_user_menu() {
  $items = array();

  $items['user/trouble'] = array(
    'title' => 'trouble logging in',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_user_trouble_logging_in_form'),
    'access callback' => TRUE,
  );

  return $items;
}

Second, you need to create form structure

function custom_user_trouble_logging_in_form($form, &$form_state) {
  $form['mail'] = array(
    '#type' => 'textfield',
    '#title' => t('E-Mail address'),
    '#size' => 60,
    '#required' => TRUE,
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Resend activation email'),
  );

  return $form;
}

Third, validation step (check if user email already registered)

/*
 * Validation function
 */
function custom_user_trouble_logging_in_form_validate($form, &$form_state) {
  $account = user_load_by_mail($form_state['values']['mail']);

  if(!$account) {
     form_error($form['mail'], t('Invalid user email'));
  }
}

Fourth, resend user activation email on form submission.

/*
 * Submit function for the 'Re-send welcome message'.
 */
function custom_user_trouble_logging_in_form_submit($form, &$form_state) {
  global $language;

  $destination = array();

  if (isset($_GET['destination'])) {
    $destination = drupal_get_destination();
    unset($_GET['destination']);
  }

  $account = user_load_by_mail($form_state['values']['mail']);
  $user_register = variable_get('user_register', 2);

  switch ($user_register) {
    case 0:
      $op = 'register_admin_created';
      break;
    case 1:
      $op = 'register_no_approval_required';
      break;
    case 2:
      $op = 'register_pending_approval';
      break;
  }

  $mail = _user_mail_notify($op, $account, $language);
  if (!empty($mail)) {
    watchdog('user', 'Welcome message has been re-sent to %name at %email.', array('%name' => isset($account->realname)? $account->realname : $account->name, '%email' => $account->mail));
    drupal_set_message(t('Welcome message has been re-sent to %name at %email', array('%name' => isset($account->realname)? $account->realname : $account->name, '%email' => $account->mail)));
  } else {
    watchdog('user', 'There was an error re-sending welcome message to %name at %email', array('%name' => isset($account->realname)? $account->realname : $account->name, '%email' => $account->mail));
    drupal_set_message(t('There was an error re-sending welcome message to %name at %email', array('%name' => isset($account->realname)? $account->realname : $account->name, '%email' => $account->mail)), 'error');
  }
  $form_state['redirect'] = $destination;
}

Note: custom_user is the name of the module that I used. You can change to whatever you like.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.