Jump to content

help with foreach or similar function


NathanS

Recommended Posts

hi guys,

 

i have a form, which the user selects multiple entries using a tickbox. when the form is submitted, it will email each ticked person (well, that's the theory!!)

 

I have tried using foreach in the below way, yet it only ever emails the first person. Any ideas on this?

 

Many thanks :)

 

foreach ($email as $e) {

if ($status == 'Pending') {
if ($sourceofbusiness == 'BusinessHere') {
$msg = "Message Here";
$recipient = $e;
$subject = "Email";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Company <[email protected]> \n";
mail($recipient, $subject, $msg, $headers);
}

 

(code continues in that way, dependednt on status/source of business)

 

 

Link to comment
https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/
Share on other sites

Thanks for the quick reply.

 

I've already done that as follows:

 

echo "<INPUT TYPE='checkbox' name='EMAIL[]' VALUE='$field20' style='visibility:hidden' checked>";

 

And in the php file, I have the variables declared as follows:

 

$email = $_POST["EMAIL"];

 

Then I use the foreach loop before the email code - However, it only sends an email to one address.

 

Thanks :)

if you name the checkboxes email[] and you are using POST variables then the foreach loop would look like this

 

<?php
foreach($_POST['email'] as $e){
if ($sourceofbusiness == 'BusinessHere') {
$msg = "Message Here";
$recipient = $e;
$subject = "Email";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Company <[email protected]> \n";
mail($recipient, $subject, $msg, $headers);
}
}
?>

 

Remember globals are not on by default so you just can't use $email you would have to call it as $_POST['email

 

Also if the message is going to be the same why send out the mail a bunch of times, put the recipients in a blind copy and send it out that way

 

<?php
$bcc = "";
$count = 0;
foreach($_POST['email'] as $e){
  $bcc .= $e;
  $count++;
  if ($count < $num_rows) {
  $bcc .= ", ";
  }
}
if ($sourceofbusiness == 'BusinessHere') {
$msg = "Message Here";
$recipient = "";
$subject = "Email";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Company <[email protected]> \n";
        $headers .= "BCC: $bcc\r\n";
mail($recipient, $subject, $msg, $headers);
}
?>

 

Ray

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.