How to retrieve email address of admin users?

Hi,

I need some help to retrieve the email address of all admin users on my site to send a notification email to them when needed. Can someone advise me on this? Thanks in advance.

  • Write a simple SQL query to fetch all the user emails from the user entity table where admin = yes

    Take a look at http://docs.elgg.org/ also

  • Example code used in the FAQ plugin for admin notification:


    /**
     * A Frequently Asked Question Plugin
     *
     * @module faq
     * @author ColdTrick
     * @copyright ColdTrick 2009
     * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
     * @link http://www.coldtrick.com
     *
     * Updated for Elgg 1.8 by iionly
     * iionly@gmx.de
     */

    function notifyAdminNewQuestion(){

        global $CONFIG;

        $db_prefix = elgg_get_config('dbprefix');
        $admins1 = elgg_get_entities(array('type' => 'user',
                                           'wheres' => "{$db_prefix}users_entity.admin = 'true'",
                                           'joins' => "JOIN {$db_prefix}users_entity ON {$db_prefix}users_entity.guid = e.guid"));
        $admins2 = elgg_get_entities(array('type' => 'user',
                                           'wheres' => "{$db_prefix}users_entity.admin = 'yes'",
                                           'joins' => "JOIN {$db_prefix}users_entity ON {$db_prefix}users_entity.guid = e.guid"));

        if(is_array($admins1) && is_array($admins2)) {
            $admins = array_merge($admins1, $admins2);
        } elseif(is_array($admins1)) {
            $admins = $admins1;
        } elseif(is_array($admins2)) {
            $admins = $admins2;
        } else {
            $admins = array();
        }

        $result = array();
        foreach($admins as $admin) {
            $result[] = notify_user($admin->guid, $admin->site_guid, elgg_echo("faq:ask:notify:admin:subject"), sprintf(elgg_echo("faq:ask:notify:admin:message"), $admin->name, $CONFIG->wwwroot . "faq/asked"));
        }

        if(in_array(true, $result)) {
            $result = true;
        } else {
            $result = false;
        }

        return $result;
    }

  • Aqeel and iionly, thanks so much, I am trying your suggestions out. :)

  • $admin_guids = elgg_get_admins(array(
    'limit' => 0,
    'callback' => function ($row) { return $row->guid; }, // no overhead of entity creation
    ));
    notify_user($admin_guids, elgg_get_site_entity()->guid, $subject, $message, null, 'email');
  • @Steve: cool! I didn't know about that function.

     

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking