Drupal Notes 5: admin module emails users from profile: hook_mail(), hook_mail_alter(), hook_help(), hook_menu(), Forms API, hook_user()

For this module it’s best that your development environment is configured to send live emails.
If it’s local, xampp perhaps, and not currently configured as such,
you may want to refer to this post first:
http://alexyz.com/sending-mail-xampp-gmail/

Step 1, of course, create a new folder entitled emailusers in sites/all/modules

Step 2, in that folder, create emailusers.info, containing the following
(if you’ve read Notes 1-4 this should all be very familiar already):

;$Id$
name = “emailusers”
description = “admin module for emailing users from within their profile”
core = 6.x
php = 5.1

Step 3, create emailusers.module in same folder, contents thereof follow:

// $Id$
/**
* This module provides an email interface for administrators.
* Using this module, administrators can send email to a user from the
* user’s “view” page.
* @file
*/
/**

* Implementation hook_help()
*/

function emailusers_help($path, $arg){
if($path == ‘admin/help#emailusers’){
$txt = ‘This module provides a way for an administrator to send’
. ’email to a user. ‘
. ‘It assumes that the Drupal mailer is configured.’;
return ‘<p>’ . t($txt) . ‘</p>’;
}
}
/**
* Implementation of hook_munu()
*/

function emailusers_menu(){
// Need to pass User ID via placeholder %,
// thus URL will be available here: ?q=admin/emailusers/compose/userIdHere

$items[‘admin/emailusers/compose/%‘] = array(
‘title’ => ‘Compose a Message’,
‘page callback’ => ’emailusers_compose’,
‘page arguments’ => array(3), // <- userId (from % in node path)
‘access arguments’ => array(‘administer users’),
‘type’ => MENU_CALLBACK,
);
return $items;
}
/**
* Compose a message.
* This creates the form necessary to compose an email message.
*
* @param $to
* The address to send to.
* @return
* HTML.
*/

function emailusers_compose($userid){
$userid = intval($userid);
if($userid == 0){
return t(‘User ID must be an integer.’);
}
$account = user_load($userid);
if(empty($account)){
return t(‘No such user found.’);
}
$to = $account->mail;
$sb = ‘<p>’
. t(‘Send a message to @email.’, array(‘@email’ => $to))
. ‘</p>’;
$sb .= drupal_get_form(’emailusers_compose_form’, $account);
// Forms API / FAPI
// http://drupal.org/node/751826
// http://api.drupal.org/api/drupal/developer–topics–forms_api_reference.html/6

return $sb;
}
/**
* Form constructor
*/

function emailusers_compose_form($context, $account){
// This is a value only — equivalent to a hidden field
// except that it is never renered into the HTML

$form[‘to’] = array(
‘#type’ => ‘value’,
‘#value’ => $account,
);
// Create a fieldset for the body:
$form[‘message’] = array(
‘#type’ => ‘fieldset’,
‘#title’ => t(‘Compose the Message’),
);
// Textfield for subject of the body
$form[‘message’][‘subject’] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘Subject’),
‘#size’ => 50,
‘#maxlength’ => 255,
‘#description’ => t(‘The subject of the email message.’),
);
// And a text area for the body.
$form[‘message’][‘body’] = array(
‘#type’ => ‘textarea’,
‘#title’ => t(‘Message’),
‘#cols’ => 50,
‘#rows’ => 5,
‘#description’ => t(‘The body of the email message.’),
);
// Create a fieldset for details
$form[‘details’] = array(
‘#type’ => ‘fieldset’,
‘#title’ => t(“Details”),
);
// Checkbox: if checked, CC the author too
$form[‘details’][‘cc_me’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => t(‘BCC Yourself’),
‘#default_value’ => 1,
‘#description’ => t(‘If this is checked, the message will also be sent to you.’),
);
// Finally, a submit button:
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Send Mail’),
);
return $form;
}
/**
* Form submission handler, which functions like a hook.
* Note that the params $form and &$form_state are new in D6.
* They replace $form_id and $form_values.
*/

function emailusers_compose_form_submit($form, &$form_state){
$form_values = $form_state[‘values’];
$account = $form_values[‘to’];
drupal_mail(
’emailusers’,
‘composemessage’,
$account->mail,
user_preferred_language($account),
$form_values,
variable_get(‘site_mail’, null),
true // Automatically send
);
// drupal_mail will invoke hook_mail(), or in this case, emailusers_mail(), below
$form_state[‘redirect’] = sprintf(‘user/%d’, $account->uid);
}
/**
* Implementation of hook_mail()
*/

function emailusers_mail($key, &$message, $params){
// Just catch calls to this hook from compose form.
if ($key == ‘composemessage’){
$language = $params[‘language’];
$account = $params[‘to’];
if($params[‘cc_me’]){
// Look up current user’s email address:
//$my_account = user_load(null);

$message[‘headers’][‘bcc’] = $GLOBALS[‘user’]->mail;
}
$message[‘to’] = $account->mail;
$message[‘subject’] = t(‘Drupal Message: ‘, array(), $language->language);
// If these were automatically-generated messages
// they should be run through t()
// but since text is user-entered
// don’t use t()

$message[‘subject’] .= $params[‘subject’];
//$message[‘body’] = $params[‘body’];
$message[‘body’][] = $params[‘body’];
}
}
/**
* Implements hook_mail_alter()
* !! Note: similar, and also of interest are hook_link_alter() & hook_menu_alter()
* simply put, _alter hooks alter data right after their parent hooks are called, but before output
* also important to note: this _alter() below will augment ALL emails sent to the Mail API
* not just those from this module!
*/

function emailusers_mail_alter(&$message){
$append = “\n==================================\n”
. “This message was sent from !site_name (!website). ”
. “If you believe this message to be a case of abuse, ”
. “please contact !site_email.\n”;
$args = array(
‘!website’ => url(”, array(‘absolute’ => true)),
‘!site_email’ => variable_get(‘site_mail’, null),
‘!site_name’ => variable_get(‘site_name’, ‘Unknown’),
);
$message[‘body’] .= t($append, $args);
//$message[‘body’][] = t($append, $args);
}
/**
* Implementation of hook_user().
*/

function emailusers_user($op, &$edit, &$account, $category){
if($op == ‘view’ && user_access(‘administer users‘)){
// Create the outer “block”
$account->content[‘EmailUsers’] = array(
‘#type’ => ‘user_profile_category‘,
‘#attributes’ => array(‘class’ => ‘user-member’),
‘#weight’ => 0,
‘#title’ => t(‘Contact user’),
);
// Create the content of the block
$account->content[‘EmailUsers’][‘EmailLink’] = array(
‘#type’ => ‘user_profile_item‘,
‘#title’ => t(‘Send a message to this user from the site administrator.’),
‘#value’ => l(
‘Email’,
‘admin/emailusers/compose/’ . $account->uid
),
);
}
}

Comments are closed.