3

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)

  • you mean want the admin of the site to resend an activation email to a specific user?! – Aboodred1 Apr 22 '13 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 '13 at 16:51
3

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.

| improve this answer | |
0

The previous answer will not work. _user_mail_notify states that "By default, we always notify except for canceled and blocked.

Here is an implementation that notifies the user when they try to log in and sends them another email at that time using a custom user_mail_notify. "cgps_my_misc" is the name of a custom module.

<?php

function cgps_my_misc_form_alter(&$form, $form_state, $form_id)  {

  switch ($form_id)  {
    case 'user_login_block':
          $form['#validate'][] = 'cgps_my_misc_user_login_name_validate';
          break;
  }
}

function cgps_my_misc_user_login_name_validate($form, &$form_state) {
  if (!empty($form_state['values']['name']) && user_is_blocked($form_state['values']['name'])) {

    drupal_get_messages('error'); //Purges drupal core error message so we can set out own
    form_set_error('cgps_my_name', t('The account %name has not been activated.', array('%name' => $form_state['values']['name'])));


    $user = user_load_by_name($form_state['values']['name']);


    $mail = cgps_my_misc_user_mail_notify('register_no_approval_required', $user);

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

function cgps_my_misc_user_mail_notify($op, $account, $language = NULL) {
    $params['account'] = $account;
    $language = $language ? $language : user_preferred_language($account);
    $mail = drupal_mail('user', $op, $account->mail, $language, $params);
  return empty($mail) ? NULL : $mail['result'];
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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