Mass mail registered group only

danr 15 Dec, 2009
Hi
I am new to Chronoforms. I have messed around a bit and have created a form that I am happy with. What I am struggling to do is this;
1. Unregistered user fills in enquiry form and submits
2. Filled in enquiry form is then sent to all registered users on the site.

I have looked through the forum and have found this code posted by Max Payne
<?php

global $mainframe;
$database =& JFactory::getDBO();
$mytable = "table_name_which_contains_users";

$query = "SELECT * FROM $mytable";
$database->setQuery( $query );
$mydata = $database->loadObjectList();

$subject = "my subject";
$email_body = "we got your email";
$fromname = "Site.com";
$fromemail = "admin@site.com";


foreach($mydata as $edata){
   JUtility::sendMail($fromemail, $fromname, $mydata->emai_field_name, $subject, $email_body, true, NULL, NULL, NULL, NULL, NULL );
echo "an Email has been sent to ".$mydata->emai_field_name;
}


?>

With a bit of tweaking with the form fields is this the correct direction for my requirements ?
Any help greatly appreciated..
regards
Danny
danr 16 Dec, 2009
Hi
I have edited the code and added to the basicform HTML code to give the following
<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Name</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter your name" id="text_1" name="name" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Email address:</label>
    <input class="cf_inputbox required validate-email" maxlength="150" size="30" title="Please enter your email address" id="text_2" name="email" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Subject</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter a subject for your message" id="text_3" name="subject" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 150px;">Message</label>
    <textarea class="cf_inputbox required" rows="5" id="text_4" title="Please enter your message" cols="40" name="message"></textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_5" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>
<?php

global $mainframe;
$database =& JFactory::getDBO();
$mytable = "jos_users";

$query = "SELECT REGISTERED FROM $mytable";
$database->setQuery( $query );
$mydata = $database->loadObjectList();

$subject = "my subject";
$email_body = "we got your email";
$fromname = "Site.com";
$fromemail = "admin@site.com";


foreach($mydata as $edata){
   JUtility::sendMail($fromemail, $fromname, $mydata->emai_field_name, $subject, $email_body, true, NULL, NULL, NULL, NULL, NULL );
echo "an Email has been sent to ".$mydata->emai_field_name;
}


?>


My question is I have the registered users in the jos_users table. The Authors and admin are also kept in this table. The line
$query = "SELECT REGISTERED FROM $mytable";

is where I replaced the original * with REGISTERED, is this the correct way to call for registered users ?
Also I am not sure on this line
JUtility::sendMail($fromemail, $fromname, $mydata->emai_field_name, $subject, $email_body, true, NULL, NULL, NULL, NULL, NULL );
echo "an Email has been sent to ".$mydata->emai_field_name;

as to what I should change the emai_field_name to.
Any help greatly appreciated.
GreyHead 16 Dec, 2009
Hi danr,

REGISTERED won't do it. You need to build a valid MySQL query there so probably you'll need to check the MySQL docs.

The '*' is a request to fetch all the table columns; you'll need to add a WHERE clause to select registered users. From memory it's something like WHERE `gid` = 18 but you'll need to check the Joomla docs.

Bob
danr 16 Dec, 2009
Hi
Thanks for the advice. I have altered the query to include the where command and have linked the $mytable to email.
I now have the following code;

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Name</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter your name" id="text_1" name="name" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Email address:</label>
    <input class="cf_inputbox required validate-email" maxlength="150" size="30" title="Please enter your email address" id="text_2" name="email" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Subject</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="Please enter a subject for your message" id="text_3" name="subject" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 150px;">Message</label>
    <textarea class="cf_inputbox required" rows="5" id="text_4" title="Please enter your message" cols="40" name="message"></textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_5" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>


<?php

global $mainframe;
$database =& JFactory::getDBO();
$mytable = "jos_users";

$query = "SELECT * FROM $mytable WHERE gid>19";
$database->setQuery( $query );
$mydata = $database->loadObjectList();

$subject = "my subject";
$email_body = "we got your email";
$fromname = "test";
$fromemail = "test@test.com";


foreach($mydata as $edata){
   JUtility::sendMail($fromemail, $fromname, $mydata->email, $subject, $email_body, true, NULL, NULL, NULL, NULL, NULL );
}

?>


I now have an issue with the sendmail function failing and a warning showing " PHPMAILER_EXECUTE/usr/sbin/sendmail ". I have had the message only after adding additional code calling for jutility::sendmail.
Having looked at the code I am not sure it will do what I would like as it does not pass the data inputted into the enquiry form by the user. Does anyone know if this is possible ?
GreyHead 17 Dec, 2009
Hi darr,

a) You appear to have this code in the Form HTML which measn that it will run when the form is loaded. It probably needs to be in one of the OnSubmit boxes.

b) Because the form hasn't been submitted none of the form inputs will have any value. Though this may be irrelevant as you aren't using any of them.

c) I think the email To parameter has to be a string or an array of strings - you appear to be passing an array of objects. This is probably the immediate cause of the error message.

Bob
danr 17 Dec, 2009
Hi Bob
Thanks for your help. I will probably place a request in the paid work section as this is a bit beyond my knowledge.

Thanks
Danr
This topic is locked and no more replies can be posted.