Forums

Send email to all administrators

timmah 04 Aug, 2011
Hi,

I have created a form in ChronoForms but I need the to address to automatically send to site administrators, specifically administrators who have 'Receive System Emails' enabled.

Is it possible to automatically import the relevant addresses into the forms To field via MySQL.
Is anybody able to help me with the code to do this?

Thanks in advance.
GreyHead 05 Aug, 2011
Hi Timmah,

I think you can probably do this by querying the jos_users table with WHERE `sendEmail` = 1

Bob
timmah 05 Aug, 2011
Hi Bob,

Thanks for your reply.
I was able to figure it out this arvo, so if anyone else is wanting to do this, here are the steps.

1. Create a textbox within your form to hold the admin email address

2. Place this code above your form code.
<?php
 $db =& JFactory::getDBO();
$query = "SELECT email FROM #__users WHERE sendEmail = 1;";
$db->setQuery($query);
$result = $db->query();
$result = $db->loadResult();
?>


3. Set the value of your textbox to
value="<?php print_r($result); ?>"

This only returns one email address, but that can be easily changed if you alter the code.

4. Select that textbox as your Dynamic To field.
NOTE: If you are receiving a bunch of javascript instead of a plain email you may need to disable Joomla's 'Email Cloaking' plugin.

Cheers,
Timmah
GreyHead 06 Aug, 2011
Hi Timmah,

That will get you just one admin email address (getResult() only returns one value). If you want all of them then the code would be something like
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT `email` 
    FROM `#__users` 
    WHERE `sendEmail` = 1
";
$db->setQuery($query);
$result = $db->loadAssocList();
$result = implode(',', $result);
?>

Bob
timmah 07 Aug, 2011
Thanks for that.
I tried out your code but the result keeps coming as 'Array' instead of email addresses.
Any idea why this would be?
GreyHead 07 Aug, 2011
Hi Tim,

Sorry, $db->loadAssocList() gives you an array of arrays :-(

Please check with $db->loadResultArray() instead.

Bob
timmah 08 Aug, 2011
Hmm, now I am receiving this error.
Warning: implode() [function.implode]: Invalid arguments passed in {URL}\components\com_chronocontact\chronocontact.html.php(184) : eval()'d code on line 6

EDIT: Nope, sorry, I made a mistake.
That code is working perfectly, thank you for all your help Bob.
This topic is locked and no more replies can be posted.