Sending emails according checkbox

Hi guys,

I am trying to setup a form using array, however I got stuck there, would you please have a look for me?

I have a form using checkbox, this form will always submit to the admin, for example to admin@bigpond.com. At the same time I want it to be sent to email1@bigpond.com when user ticks Yes of “Caterers”. When user ticks Photographers then also send this form to another email email1@bigpond.com…etc.

I managed to send the form to admin, also to “Caterers” email address. However, on the submit email, just showing these:

Someone has sent the following enquiries:

Full Name: ABC

Email Address: user@bigpond.com

I would like you guys supply: Array

Line 1 to 3 are what I want, but on line 4, it is only showing “Array”, I want it to show Caterers, Photographers, Bakers, Florists if they are all ticked by user. Then the admin will know what the user actually order. Once again, the form will also send to different related department with their own email address according to the checkbox.

I just can’t figure out what I should do, would anyone please check on my setting?

Please help. Many many many thanks.

HTML code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8” />
<title>Untitled Document</title>
</head>

<body>
<form method=“post” action=“form.php”>
<table width=“573” cellpadding=“5” cellspacing=“0” class=“txt”>

<tr>
<td width=“10” height=“25” bgcolor=“#cee39e”> </td>
<td width=“149” height=“25” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>Your Full Name</span><span class=“txtred”>*</span></td>
<td width=“265” height=“25” bgcolor=“#cee39e”>
<input name=“Name” type=“text” size=“35” maxlength=“40” />
</td>
<td width=“107” bgcolor=“#cee39e”> </td>
</tr>

<tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>Email </span><span class=“txtred”>*</span></td>
<td height=“25” bgcolor=“#cee39e”>
<input name=“Email” type=“text” size=“35” maxlength=“50” />
</td>
<td bgcolor=“#cee39e”> </td>
</tr><tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” colspan=“2” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>What would you like us to supply:</span><span class=“txtred”></span></td>
<td bgcolor=“#cee39e”> </td>
</tr>

<tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” colspan=“2” valign=“top” bgcolor=“#cee39e” class=“txt”><table width=“456” cellpadding=“10” cellspacing=“0”>

<tr>

<td width=“212” height=“20”><span class=“txtbold”>Caterers</span><span class=“txt”></span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“caterers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Photographers</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“photographers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Bakers</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“bakers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Florists</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“florists”>
<span class=“txt”> Yes</span></td>
</tr>

</table></td>
<td bgcolor=“#cee39e”> </td>
</tr>

<tr>
<td bgcolor=“#cee39e”> </td>
<td height=“30” colspan=“2” bgcolor=“#cee39e”>
<input type=“submit” name=“submit” value=“Submit” />
<input type=“reset” name=“reset” value=“Reset” />
</td>
<td bgcolor=“#cee39e”> </td>
</tr>
</table>
</form>
</body>

</html>

PHP code:

<?php
$to = $_REQUEST[‘admin@bigpond.com’] ;
$from = $_REQUEST[‘Email’] ;
$name = $_REQUEST[‘Name’] ;
$headers = “From: $from”;
$subject = “Enquiries”;

$fields = array();
$fields{“Name”} = "
Full Name";
$fields{“Email”} = "
Email Address";
$fields{“suppliers”} = "
I would like you guys supply";

$body = "Someone has sent the following enquiries:

“; foreach($fields as $a => $b){ $body .= sprintf(”%20s: %s
",$b,$_REQUEST[$a]); }

$headers2 = “From: admin@bigpond.com”;
$subject2 = “Thank you for contacting us”;
$autoreply = "Thank you, your enquiry has been received.

The team will get back to you asap.";

if(isset($_REQUEST[‘suppliers’]) && count($_REQUEST[‘suppliers’]))
{

$recipient_email_addresses = array
(

“caterers” => array(‘email1@bigpond.com’),
“photographers” => array(‘email2@bigpond.com’),
“bakers” => array(‘email3@bigpond.com’),
“florists” => array(‘email4@bigpond.com’)

);

foreach($_REQUEST[‘suppliers’] as $value){foreach($recipient_email_addresses[$value] as $value2){
mail($value2,$subject, $body, $headers);}}

}

if($from == ‘’) {header( “Location: http://www.company.com.au/pages/failure.html” );}
else {
if($name == ‘’) {header( “Location: http://www.company.com.au/pages/failure.html” );}
else {
$send = mail(‘admin@bigpond.com’, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( “Location: http://www.company.com.au/pages/thankyou.html” );}
else
{print “We encountered an error sending your mail, please notify admin@bigpond.com”; }
}
}
?>

The bit where you append the fields to $body just assumes a string (by using the concatenation operator), and because PHP is dynamically typed, it converts your array to a string, just ‘Array’.

You need to either loop over the array, or use implode to turn it into a string.

$body = "Someone has sent the following enquiries:\
\
";
foreach($fields as $a => $b) {
 if(is_array($b)) {
     $body .= $a . ": " . implode(", ", $b) . "\
";
 } else {
     $body .= sprintf("%20s: %s\
",$b,$_REQUEST[$a]);
 }
}

Hi Richard,

Thanks for your help. I tried to replace the above code into my php. It does send to different emails address when the checkboxes are ticked. However on the email line 4 “I would like you guys supply: Array” is still showing “Array”, not showing “caterers”, “photographers”, “bakers” , “florists”. I need to make it to show what are the users want to order on the email. Can it be fixed? I have tried to change the values of the checkboxes on html, it doesn’t work! Would you please tell me where I have done wrong?

Many thanks.

Hmm, that should fix it - the code I posted checks if the field’s value is an array, and then uses implode to stick the array’s elements together, separated by ", ".

Could you post the code you have now?

Hi Richard,

Thanks for your reply again. Here is now my code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8” />
<title>Untitled Document</title>
</head>
<body>
<form method=“post” action=“form.php”>
<table width=“573” cellpadding=“5” cellspacing=“0” class=“txt”>
<tr>
<td width=“10” height=“25” bgcolor=“#cee39e”> </td>
<td width=“149” height=“25” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>Your Full Name</span><span class=“txtred”></span></td>
<td width=“265” height=“25” bgcolor=“#cee39e”>
<input name=“Name” type=“text” size=“35” maxlength=“40” />
</td>
<td width=“107” bgcolor=“#cee39e”> </td>
</tr>
<tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>Email </span><span class=“txtred”>
</span></td>
<td height=“25” bgcolor=“#cee39e”>
<input name=“Email” type=“text” size=“35” maxlength=“50” />
</td>
<td bgcolor=“#cee39e”> </td>
</tr><tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” colspan=“2” valign=“middle” bgcolor=“#cee39e” class=“txt”><span class=“txtbold”>What would you like us to supply/span><span class=“txtred”></span></td>
<td bgcolor=“#cee39e”> </td>
</tr>
<tr>
<td height=“25” bgcolor=“#cee39e”> </td>
<td height=“25” colspan=“2” valign=“top” bgcolor=“#cee39e” class=“txt”><table width=“456” cellpadding=“10” cellspacing=“0”>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Caterers</span><span class=“txt”></span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“caterers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Photographers</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“photographers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Bakers</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“bakers”>
<span class=“txt”> Yes</span></td>
</tr>
<tr>
<td width=“212” height=“20”><span class=“txtbold”>Florists</span></td>
<td width=“202” height=“20”><input type=“checkbox” name=“suppliers” value=“florists”>
<span class=“txt”> Yes</span></td>
</tr>
</table></td>
<td bgcolor=“#cee39e”> </td>
</tr>
<tr>
<td bgcolor=“#cee39e”> </td>
<td height=“30” colspan=“2” bgcolor=“#cee39e”>
<input type=“submit” name=“submit” value=“Submit” />
<input type=“reset” name=“reset” value=“Reset” />
</td>
<td bgcolor=“#cee39e”> </td>
</tr>
</table>
</form>
</body>
</html>

<?php
$to = $_REQUEST[‘suensh@bigpond.com’] ;
$from = $_REQUEST[‘Email’] ;
$name = $_REQUEST[‘Name’] ;
$headers = “From: $from”;
$subject = “Enquiries”;

$fields = array();
$fields{“Name”} = "
Full Name";
$fields{“Email”} = "
Email Address";
$fields{“suppliers”} = "
I would like you guys supply";

$body = "Someone has sent the following enquiries:

";
foreach($fields as $a => $b) {
if(is_array($b)) {
$body .= $a . “: " . implode(”, ", $b) . "
“;
} else {
$body .= sprintf(”%20s: %s
",$b,$_REQUEST[$a]);
}
}

$headers2 = “From: suensh@bigpond.com”;
$subject2 = “Thank you for contacting us”;
$autoreply = "Thank you, your enquiry has been received.

The team will get back to you asap.";

if(isset($_REQUEST[‘suppliers’]) && count($_REQUEST[‘suppliers’]))
{

$recipient_email_addresses = array
(

“caterers” => array(‘testpc@iinet.net.au’),
“photographers” => array(‘shsuen@hotmail.com’),
“bakers” => array(‘suensh@bigpond.com’),
“florists” => array(‘sinbeer@bigpond.com.au’)

);

foreach($_REQUEST[‘suppliers’] as $value){foreach($recipient_email_addresses[$value] as $value2){
mail($value2,$subject, $body, $headers);}}

}

if($from == ‘’) {header( “Location: http://www.webiste/pages/failure.html” );}
else {
if($name == ‘’) {header( “Location: http://www.webiste/pages/failure.html” );}
else {
$send = mail(‘test@crushme.com.au’, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( “Location: http://www.webiste/pages/thankyou.html” );}
else
{print “We encountered an error sending your mail, please notify www.webiste”; }
}
}
?>

Thank you sooooooo much.

Cheers
SuenSH

Sorry I made a mistake in my code, I wasn’t paying full attention!

It should be:

$body = "Someone has sent the following enquiries:\
\
";
foreach($fields as $a => $b) {
if(is_array($_REQUEST[$a])) {
$body .= $b . ": " . implode(", ", $_REQUEST[$a]) . "\
";
} else {
$body .= sprintf("%20s: %s\
",$b,$_REQUEST[$a]);
}
} 

Hi Richard,

Thank you soooo much!!! It is working beautifully!! You are life saver!!

Cheers
SuenSH