User Email List

A perfect place for code snippets to small to be a MOD or a library.
Or for examples on how to use our libraries.
Post Reply
User avatar
bonelifer
Administrator
Administrator
Posts: 477
Joined: 24 Jun 2006, 17:48
Real name: William
Location: htpc.MythBuntu

User Email List

Post by bonelifer »

This post was originally posted by Kenny at 6 String MODs.

I already had this made up a while ago, but only decided to release it after noticing someone requesting it. This snippet gives you a very simple list of all users on the forum and their email addresses. User names a coloured, so you can see if they are an admin or a moderator, etc.
I've also included a little security in this - if you are not a founder, you will be shown an error saying the page does not exist. We don't want spambots harvesting all your user emails now do we? I don't think your users would appreciate that ;)

The list is also paginated. The value is hard-coded, so if you want to change it then change this value:

Code: Select all

$tf = '20';   &nbsp


list.php (can be renamed to whatever you like, just remember to change all the instances of list.php in the php file)

Code: Select all

<?php
/**
*
* @package User Email List
* @version $Id: list.php,v 0006 11:45 12/03/2009 cherokee red Exp $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include(
$phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// add a mode for a CSV Listing
$mode = request_var('mode', '');
        
// now check the mode...no surprises
if (!in_array($mode, array('', 'list')))
{
    trigger_error('NO_MODE');
}

// Founders only access
if ($user->data['user_type'] <> USER_FOUNDER)
{
    trigger_error('NOT_AUTHORISED');
}

if ($mode == 'list')
{
    $csv_output = $config['sitename'] . ' ' . $user->lang['EMAIL'];
    $csv_output .= "\n";
    $csv_output .= '#,' . $user->lang['USERNAME'] . ','. $user->lang['EMAIL_ADDRESS'];
    $csv_output .= "\n";

    //Pull Users from the database
    $sql = 'SELECT *
        FROM '
 . USERS_TABLE . '
            WHERE (user_type ='
 . USER_NORMAL . ' OR user_type =' .  USER_FOUNDER . ')
                ORDER BY user_id'
;

    $result = $db->sql_query($sql);
    while ($row = $db->sql_fetchrow($result))
    {
        $csv_output .= $row['user_id'] . ',' . $row['username'] . ',' . $row['user_email'];
        $csv_output .="\n";
    }

    $db->sql_freeresult($result);
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition:  attachment; filename=" . $config['sitename'] . '_' . $user->lang['EMAIL'] . 's_' . date("Y-m-d").".csv");
    print $csv_output;
    exit;
}

$start = request_var('start', 0);

// How many Users do we have?
$sql = 'SELECT COUNT(user_id) AS total_users
    FROM '
 . USERS_TABLE . '
        WHERE (user_type ='
 . USER_NORMAL . ' OR user_type =' .  USER_FOUNDER . ')';

$result = $db->sql_query($sql);
$total_users = (int) $db->sql_fetchfield('total_users');
$db->sql_freeresult($result);

$pagination_url = append_sid("list.$phpEx");
$tf = '20';

//Pull Users from the database
$sql = 'SELECT *
    FROM '
 . USERS_TABLE . '
        WHERE (user_type ='
 . USER_NORMAL . ' OR user_type =' .  USER_FOUNDER . ')
            ORDER BY user_id'
;
$result = $db->sql_query_limit($sql, $tf, $start);

// Assign specific vars
while ($row = $db->sql_fetchrow($result))
{
    $template->assign_block_vars('list', array(
        'ID'            => $row['user_id'],
        'EMAIL'            => $row['user_email'],
        'USERNAME_FULL'    => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
        'USERNAME'        => get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
        'USER_COLOR'        => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
        'U_VIEW_PROFILE'    => get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
    ));
}
$db->sql_freeresult($result);

$template->assign_vars(array(
    'PAGINATION'            => generate_pagination($pagination_url, $total_users, $tf, $start),
    'PAGE_NUMBER'            => on_page($total_users, $tf, $start),
    'U_CSV_LIST'            => append_sid("{$phpbb_root_path}list.$phpEx", 'mode=list'),
    'TOTAL_USERS'            => ($total_users == 1) ? $user->lang['USER_COUNT'] : sprintf($user->lang['USER_COUNTS'], $total_users),
));

// Output page
page_header("User Email List");

$template->set_filenames(array(
    'body' => 'email_list_body.html')
);

page_footer();

?>

I have found that if you have spaces in your site name, then the generated csv will not be named properly. This can be sorted by adding an str_replace to the sitename.
FIND:

Code: Select all

    header&#40;"Content-disposition:  attachment; filename=" . $config[&#39;sitename&#39;] . "_Email_List_" .date&#40;"Y-m-d"&#41;.".csv"&#41;;    Â &nbsp;&nbsp;&nbsp;&nbsp; 

REPLACE WITH:

Code: Select all

    header&#40;"Content-disposition:  attachment; filename=" . str_replace&#40;" ", "_", $config[&#39;sitename&#39;]&#41; . &#39;_&#39; . $user->lang[&#39;EMAIL&#39;] . &#39;s_&#39; . date&#40;"Y-m-d"&#41;.".csv"&#41;;    Â &nbsp;&nbsp;&nbsp;&nbsp; 

styles/prosilver/template/email_list_body.html

Code: Select all

<!-- INCLUDE overall_header.html -->
<br />
<h2>{PAGE_TITLE}</h2>

<div class="pagination">
   <a href="{U_CSV_LIST}">Export as CSV List</a> &bull; {TOTAL_USERS}<!-- IF PAGE_NUMBER --><!-- IF PAGINATION --> &bull; <a href="#" onclick="jumpto(); return false;" title="{L_JUMP_TO_PAGE}">{PAGE_NUMBER}</a> &bull; <span>{PAGINATION}</span><!-- ELSE --> &bull; {PAGE_NUMBER}<!-- ENDIF --><!-- ENDIF -->
</div>
<br /><br />
<div class="forumbg forumbg-table">
   <div class="inner"><span class="corners-top"><span></span></span>

      <table class="table1" cellspacing="1" padding="1">

         <thead>
            <tr>
               <th class="table1" style="text-align:center;"> # </th>
               <th class="table1" style="text-align:center;">Username</th>
               <th class="table1" style="text-align:center;">Email</th>
            </tr>
         </thead>

         <tbody>
            <!-- IF .list -->
            <!-- IF not S_IS_BOT -->
               <!-- BEGIN list -->
               <tr class="<!-- IF S_ROW_COUNT is even -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
                  <td style="text-align:center;">{list.ID}</td>
                  <td>{list.USERNAME_FULL}</td>
                  <td><a href="mailto:{list.EMAIL}">{list.EMAIL}</a></td>
               </tr>
               <!-- END list -->
            <!-- ENDIF -->
         </tbody>
      </table>
   <span class="corners-bottom"><span></span></span></div>
</div>

            <!-- ELSE -->
         </tbody>
      </table>
   <span class="corners-bottom"><span></span></span></div>
<div>

<div class="errorbox">
<br />
   <p><strong>There are no users in your database - you shouldn't ever see this message</strong></p>
<br />
</div>
<br />

            <!-- ENDIF -->
<div class="pagination">
   {TOTAL_USERS}<!-- IF PAGE_NUMBER --><!-- IF PAGINATION --> &bull; <a href="#" onclick="jumpto(); return false;" title="{L_JUMP_TO_PAGE}">{PAGE_NUMBER}</a> &bull; <span>{PAGINATION}</span><!-- ELSE --> &bull; {PAGE_NUMBER}<!-- ENDIF --><!-- ENDIF -->
</div>
       
<br /><br />
<!-- INCLUDE overall_footer.html -->

Lastly, make the following edit:

OPEN: language/en/common.php
FIND:

Code: Select all

   'USER_AVATAR'         => 'User avatar',

AFTER ADD:

Code: Select all

   'USER_COUNT'      => '1 user',
   'USER_COUNTS'      => '%d users',
bimjim
New member
New member
Posts: 1
Joined: 28 Oct 2008, 14:07

Re: User Email List

Post by bimjim »

I have been using this for years with my earlier versions of phpBB.

I am now creating a new Board for a yet another genealogy group with 3.1.6 and I would really appreciate having this work with it... I find it really useful when people are asking for password fixes and this is just a hop away. Because I administer so many groups I have to make sure what I use is fast and effectuve.

I do not need to list all the emails to any file, since I never give them out to anyone, and I broadcast periodic messages to the users through the software itself.

It would be much appreciated if someone could update this snippet... I tried installing it and it does not work as is - gives me back a blank page.

Many thanks for your consideration...

Jim Lynch
Post Reply