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.