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 <company@company.com> \n";
mail($recipient, $subject, $msg, $headers);
}

 

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

 

 

Link to comment
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 :)

Link to comment
Share on other sites

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 <company@company.com> \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 <company@company.com> \n";
        $headers .= "BCC: $bcc\r\n";
mail($recipient, $subject, $msg, $headers);
}
?>

 

Ray

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.