By marvs on
This code works but I'm just wondering about the $message['id'] and 'user_register_pending_approval_admin'. I want to use profile module to add another information in registration. I want to add !fullname field to show in user's emailed information. Can you give me more efficient code than this?
<?php
/**
* Version: Drupal 6.x
*
* Implementation of hook_form_alter()
* Alter the user_admin_settings form to display the added email tokens
**/
function modifyMail_form_alter(&$form, $form_state, $form_id){
if ($form_id == "user_admin_settings") {
$form['email']['pending_approval']['#description'] .= ' Additional variables are: !first, !middle, !last.';
}
}
/**
* Implementation of hook_mail_alter().
*
* This hook is being used to get the full name of the registrant and replace
* the variables within emails (!first, !middle, !last) with the registrant's
* first, middle and last name as captured by the registration form and stored
* in profiles_values table.
*
* NOTE: Remember to add the new variables (!first, !middle, !last) to the email subject line and body in
* administration section > user settings > User e-mail settings > Welcome...
*/
function modifyMail_mail_alter(&$message) {
global $base_root;
global $base_path;
global $user;
switch($message['id']) {
case 'user_status_activated':
//NOTIFY ADMINS OF ACCOUNT ACTIVATION
$bcc = variable_get('modifyMail_addresses', '');
if ($bcc != '') $message['headers']['bcc'] = $bcc;
break;
case 'user_register_pending_approval_admin':
//get new registrant's username and id
$sql2 = 'SELECT uid,name FROM {users} WHERE uid=(SELECT MAX(uid) FROM {users})';
$result2 = db_query($sql2);
$row2 = db_fetch_object($result2);
$uid = $row2->uid;
$name = $row2->name;
//get new registrant's fullname name
//get the profile fields using user id
$sql = "SELECT fid,value FROM {profile_values} WHERE uid='%s' AND (fid=1 OR fid=2 OR fid=3)";
$result = db_query($sql, $uid);
//create array to hold values
$new_variables = array();
//loop through query result
while ($row = db_fetch_object($result)) {
//switch on the field id (fid) of the result
//fid 1 is first name, fid 2 is last name, fid 3 is middle name
switch ($row->fid) {
case 1:
$new_variables += array('!first' => $row->value);
break;
case 2:
$new_variables += array('!last' => $row->value);
break;
case 3:
$new_variables += array('!middle' => $row->value);
break;
}
}
//check the middle name and tweak for full name display
if (isset($new_variables['!middle'])) {
$new_variables['!middle'] .= " ";
}else{
$new_variables['!middle'] = '';
}
//create array of replacement vars
$replace = array(
'!fullname' => $new_variables['!first'] . " " . $new_variables['!middle'] . $new_variables['!last'],
'!site' => variable_get('site_name', 'Your Site Name'),
'!username' => $name
);
$email_list = variable_get('modifyMail_addresses', variable_get('site_mail',ini_get('sendmail_from')));
$message['subject'] = _user_mail_text('pending_approval_admin_subject', $message['language'], $variables);
//drupal_set_message('message subject is:'.$message['subject']);
$message['subject'] = strtr($message['subject'], $replace);
$message['body'][0] = $replace['!fullname'] . " registered as username: " . $replace['!username'] . ". \n\nAuthorize the registration at: " . $base_root . $base_path . "user/" . $uid . "/edit \n\nReview ".$replace['!fullname'] . "'s profile at: ".$base_root.$base_path."user/".$uid;
//drupal_set_message("fullname is:".$replace['!fullname']." and body is:".$message['body'][0]);
$message['to'] = $email_list;
break;
default :
//identify to whom the email is being sent
$email = $message['to'];
//get user id from db using email address
$sql2 = "SELECT uid FROM {users} WHERE mail='%s'";
$result2 = db_query($sql2, $email);
$row2 = db_fetch_object($result2);
$uid = $row2->uid;
//get the profile fields using user id
$sql = 'SELECT fid,value FROM {profile_values} WHERE uid=%d AND (fid=1 OR fid=2 OR fid=3)';
$result = db_query($sql, $uid);
//create array to hold values
$new_variables = array();
//loop through query result
while ($row = db_fetch_object($result)) {
//switch on the field id (fid) of the result
//fid 1 is first name, fid 2 is last name, fid 3 is middle name
switch ($row->fid) {
case 1:
$new_variables += array('!first' => $row->value);
break;
case 2:
$new_variables += array('!last' => $row->value);
break;
case 3:
$new_variables += array('!middle' => $row->value);
break;
}
}
//check for middle name existence
if (!isset($new_variables['!middle'])) { $new_variables['!middle'] = ''; }
//replace placeholders (!first, !last, !middle) in email
//with corresponding new variables
$message['subject'] = strtr($message['subject'], $new_variables);
$message['body'][0] = strtr($message['body'][0], $new_variables);
break;
}
}
/**
* Implementation of hook_help().
*
* Throughout Drupal, hook_help() is used to display help text at the top of
* pages. Some other parts of Drupal pages get explanatory text from these hooks
* as well. We use it here to provide a description of the module on the
* module administration page.
*/
function modifyMail_help($section) {
switch ($section) {
case 'admin/modules#description':
// This description is shown in the listing at admin/modules.
return t('The modfiyMail module adds variables available to the system-generated emails.');
}
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the modifyMail module
* Sets the permissions
*/
function modifyMail_perm() {
return array('administer modifyMail');
}
/*
The following adds a field for entering admin emails
to notify that a new member is requesting registration.
These should be the emails of admins that can approve/deny the request
*/
function modifyMail_admin() {
$form['modifyMail_addresses'] = array (
'#type' => 'textarea',
'#title' => t('Notify addresses'),
'#default_value' => variable_get('modifyMail_addresses', variable_get('site_mail',ini_get('sendmail_from'))),
'#description' => t('Enter each recipient’s email address on a new line or place a comma between addresses. The email addresses entered will receive new member registration notifications.'),
'#required' => TRUE
);
return system_settings_form($form);
}
/*
If you want to notify admins or others that a new member is requesting registration
This adds the entry in the admin menu.
*/
function modifyMail_menu() {
$items = array();
$items['admin/settings/modifyMail'] = array(
'title'=>'Modify Mail Settings',
'description' => 'Modify recipient list for new member registration notifications.',
'page callback' => 'drupal_get_form',
'page arguments' => array('modifyMail_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/*
Validates the entered admin email addresses
*/
function modifyMail_admin_validate($form, &$form_state) {
$email_list = trim($form_state['values']['modifyMail_addresses']);
if (!is_string($email_list)) {
form_set_error('modifyMail_admin_addresses', t('You must enter only valid email addresses separated by commas or on a new line.'));
}
if (!empty($email_list)) {
//check if email was entered as comma-delimited
$found = strpos($email_list, ",");
$pattern = '/(\s)*/i';
if ( is_numeric($found) && ($found != 0) ) {
//comma-delimited
//remove any whitespace character including [ \t\n\r\f\v]
//drupal_set_message('modifyMail_addresses is comma-delimited');
$email_list = preg_replace($pattern, '', $email_list);
}
//no comma was found, check if return-delimited
elseif( preg_match($pattern, $email_list) == 1 ) {
//return-delimited
//drupal_set_message('modifyMail_addresses is return-delimited');
//replace any whitespace character with comma
$email_list = preg_replace('/(\s)+/i', ',', $email_list);
}
//drupal_set_message('email_list is:'.$email_list);
//check again, because we just tried to normalize to comma-delimited
$found = strpos($email_list, ",");
//if comma-delimited (i.e. more than one email address);
if ( is_numeric($found) && ($found != 0) ) {
//convert email address list to array
$aEmail = explode(",", $email_list);
for($i=0;$i<count($aEmail);$i++) {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $aEmail[$i])){
form_set_error('modifyMail_addresses', 'The email address you entered in the #'.($i+1).' spot is not formatted properly or contains illegal characters.');
}
}
}else{
//contains only one email address
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_list)){
form_set_error('modifyMail_addresses', 'The email address you entered is not formatted properly or contains illegal characters. Be sure to use only commas between addresses or place each address on a separate line.');
}
}
}
$form_state['values']['modifyMail_addresses'] = $email_list;
}
Comments
Ugh! you paste too much code
Ugh! you paste too much code and then ask someone to disect it.. anyway, you can use content profile.. each field you add can be specified to show-up on registration form.
_
thanks.., but how I can I send the information entered in profile module fields in user registration. For example textfield "fullname" can i put !fullname variable like !username and !password in User e-mail settings.
Same here
I have the exact same question... anyone ??
Try this
Try this :
http://drupal.org/node/376102