Setting up send to a friend for non-members

Was working on some modifications for the Print module's send to a friend functionality. In this case we have a legacy database that has several years worth of site registrations. If a user isn't registered for the site yet we want to send them an additional paragraph explaining how to get the registration code for the site.

By doing a hook_form_alter() on the send as email form it's possible to extract the submit function for the form and have our own processor handle it. Here's a way to handle splitting the members and non-members up for mailing.

<?php
function cali_print_mail_form_submit($form_id, $form_values) {
 
$to_array = array();
 
$to_members = array();
 
$to_nonmembers = array();
  if (!
array_key_exists('cancel', $form_values)) {
  if (!empty(
$form_values['fld_from_name'])) {
   
$from = '"'. $form_values['fld_from_name'] .'" <'. $form_values['fld_from_addr'] .'>';
  }
  else {
   
$from = $form_values['fld_from_addr'];
  }
 
$to = $form_values['txt_to_addrs'];
 
// Split $to into members and non-members array
 
$to_array = explode(',', $to);
  foreach (
$to_array as $key => $value) {
   
$result = db_query('SELECT lr.uid from {legacy_registration} lr where lr.email = "%s"', trim($value));
   
$count = db_num_rows($result);
    if (
$count) {
     
$to_members[] = $value;
    }
    else {
     
$to_nonmembers[] = $value;
    }
  }
 
$params = array();
 
$params['subject'] = $form_values['fld_subject'];
 
$params['message'] = $form_values['txt_message'];
 
$params['path'] = $form_values['path'];
 
$params['cid'] = isset($form_values['cid']) ? $form_values['cid'] : NULL;
 
$params['teaser'] = $form_values['chk_teaser'];
 
$to_members = string(implode(', ', $to_members));
 
$to_nonmembers = string(implode(', ', $to_nonmembers));
 
 
//send to members frist then we can modify the message
 
print_mail_mail('sendpage', $message, $params);
 
$ret_members = drupal_mail('print_mail_sendpage', $to_members, $message['subject'], $message['body'], $from, $message['headers']);
 
//send to non-members
 
$params['message'] = "You have received a message for a for the super website." .
                       
"You need to go to your site registrar to get a registration key.\n\n" . $params['message'];
 
print_mail_mail('sendpage', $message, $params);
 
$ret_nonmembers = drupal_mail('print_mail_sendpage', $to_nonmembers, $message['subject'], $message['body'], $from, $message['headers']);
 
 
  if (
$ret_members || $ret_nonmembers) {
   
flood_register_event('print_mail');
   
watchdog('print_mail', t('%name [%from] sent %page to [%to]', array('%name' => $form_values['fld_from_name'], '%from' => $form_values['fld_from_addr'], '%page' => $form_values['path'], '%to' => $to)));
   
$site_name = variable_get('site_name', t('us'));
   
$print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
   
drupal_set_message(t($print_mail_text_confirmation, array('!site' => $site_name)));

   

$nodepath = drupal_get_normal_path($params['path']);
   
db_query("UPDATE {print_mail_page_counter} SET sentcount = sentcount + %d, sent_timestamp = %d WHERE path = '%s'", count(split(',', $to)), time(), $nodepath);
  }
  }
  return
preg_replace('!^book/export/html/!', 'node/', $form_values['path']);
}
?>

Comments

Mar
16
2011

Thank you for your post it is

Thank you for your post it is nice