Send Multiple Emails from Email Array

shawpn 21 Jan, 2009
I have a simple form where checkboxes will be used to dictate email recipients. My form:

<style type="text/css">
<!--
.style1 {
	color: #0f5a4a;
	font-weight: bold;
}
-->
</style>
<table width="100%" border="0">
  
  <tr>
    <td><span class="style1">Send To:</span> (Select the departments you want copied)</td>
  </tr>
  <tr>
    <td><input value="Preschool" name="department[]" id="checkbox_pre" type="checkbox" />
        <strong>Preschool</strong>
        <input value="Elementary" name="department[]" id="checkbox_el" type="checkbox" />
        <strong>Elementary</strong>
        <input value="High School" name="department[]" id="checkbox_hs" type="checkbox" />
        <strong>Jr./Sr. High</strong>
        <input value="Corporate" name="department[]" id="checkbox_corp" type="checkbox" />
        <strong>Corporate</strong></td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>
<p class="style1">Contact Details:</p>
<table width="100%" border="0">
    <tr>
      <td align="left" valign="middle"><strong>Title:</strong></td>
      <td align="left" valign="middle"><select name="title" size="1" id="title">
        <option value="blank" selected></option>
        <option value="Mr.">Mr.</option>
        <option value="Mrs.">Mrs.</option>
        <option value="Miss.">Miss.</option>
        <option value="Dr.">Dr.</option>
      </select>
      </td>
    </tr>
    <tr>
      <td align="left" valign="middle"><strong>
      <label>First Name:</label>
      </strong></td>
      <td align="left" valign="middle"><input type="text" name="firstname" id="firstname"></td>
    </tr>
    <tr>
      <td align="left" valign="middle"><strong>
      <label>Last Name:</label>
      </strong></td>
      <td align="left" valign="middle"><input type="text" name="lastname" id="lastname"></td>
    </tr>
    <tr>
      <td align="left" valign="middle"><strong>
      <label>Telephone:</label>
       </strong></td>
      <td align="left" valign="middle"><input name="telephone" type="text" id="telephone" size="13"></td>
    </tr>
    <tr>
      <td align="left" valign="middle"><strong>
      <label>Email:</label>
       </strong></td>
      <td align="left" valign="middle"><input name="email" type="text" id="email" size="35"></td>
    </tr>
</table>
  <label></label>
  <table width="100%" border="0">
    <tr>
      <td> </td>
    </tr>
    <tr>
      <td class="style1">Your Message:</td>
    </tr>
    <tr>
      <td><textarea name="message" id="message" cols="45" rows="10"></textarea></td>
    </tr>
  </table>
  <table width="100%" border="0">
    <tr>
      <td> </td>
    </tr>
    <tr>
      <td><span class="style1">Upload Resume:</span> (PDF files only please) (convert <a title="" target="_blank" href="http://online.primopdf.com">here</a>)</td>
    </tr>
    <tr>
      <td><input name="resume" type="file" id="resume" size="37" /></td>
    </tr>
    <tr>
      <td> </td>
    </tr>
  </table>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit Form" />
    <input type="reset" name="Reset" id="Reset" value="Reset" />
  </p>


When sending to a single defined email address, the email is received and {department} is populated with the departments that were selected.

Now I insert the following code after submit but before email:

<?php
$emails = array('Preschool' => 'preschool@myemail.org', 'Elementary' => 'elementary@myemail.org', 'High School' => highschool@myemail.org', 'Corporate' => corporate@myemail.org');
$em_array = array();
foreach ( $_POST['department'] as $em ) {
  $em_array[] = $emails[$em];
}
$rows[0]->extraemail = implode(",", $em_array);
?>


I have now added a Dynamic To email with extraemail as the field to reference but NO emails are received. So now I add the debug code after email is sent:

<?php
if ( $debug ) {
  echo "extraemail: "; print_r($rows[0]->extraemail);echo "<br />";
}
?>


The debug output shows that extraemail shows the selected department's email(s) seperated by a comma. Any ideas? I think maybe that I am not using the proper "email setup" in my form.
Max_admin 21 Jan, 2009
Hi shawpn,

if you are using CF V3.0 then you will need to replace:
$rows[0]->extraemail


by

$emails[0]->to


Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
shawpn 21 Jan, 2009
Thank you for the quick response.

I updated my code but emails are still not being generated (it is turned on.) The debug output is the same with exception to 'extraemail' being replaced by 'to'. Here it is:

Form passed first SPAM check OK 
Form passed the submissions limit (if enabled) OK 
Form passed the Image verification (if enabled) OK 
Form passed the server side validation (if enabled) OK 
Form passed the plugins step (if enabled) OK 
Emails data loaded OK 
Form passed all before email code evaluation OK 
Debug End 
_POST: Array ( [department] => Array ( [0] => Preschool ) [title] => Mr. [firstname] => John [lastname] => Doe [telephone] => [email] => email@email.com [message] => Message [submit] => Submit Form [b7482b5ae932311f780e578d3c9bfee2] => 1 ) 
to: preschool@mymail.org


I have only one "Email Setup" entry that contains To, Subject, FromName, and FromEmail fields.

~Shawn
shawpn 22 Jan, 2009
Should I be using a standard To: in my email setup or should I be using a Dymanic To: when using the code I have listed? Any help would be greatly appreciated. Thank you.
shawpn 22 Jan, 2009
Problem Solved!

1. I discovered that the "Dynamic To" field was only capable of processing one email address at a time so wrong option.
2. I realized that I needed to use a standard "To" field which was going to be replaced by my "prior to sending" PHP code. The PHP code was corrected by Max. Thank you Max.
3. After digging into chronocontact.php I saw that the variable $emails was already used and therfore important 😀
4. Changed my code to reflect a different variable and all was fixed.

Previous Code:
<?php
$emails = array('Preschool' => preschool@myemail.org', 'Elementary' => elementary@myemail.org', 'High School' => highschool@myemail.org', 'Corporate' => corporate@myemail.org');
$em_array = array();
foreach ( $_POST['department'] as $em ) {
  $em_array[] = $emails[$em];
}
$emails[0]->to = implode(",", $em_array);
?>


New Code:
<?php
$sendto = array('Preschool' => preschool@myemail.org', 'Elementary' => elementary@myemail.org', 'High School' => highschool@myemail.org', 'Corporate' => corporate@myemail.org');
$em_array = array();
foreach ( $_POST['department'] as $em ) {
  $em_array[] = $sendto[$em];
}
$emails[0]->to = implode(",", $em_array);
?>


My form now works as intended.
Max_admin 22 Jan, 2009
Great news, thanks!🙂

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.