Page 1 of 1
easy loop question
Posted: Sat Aug 06, 2005 7:39 pm
by s.dot
I want to have 10 email address fields, then when the form is submitted, send an email to the 10 or less emails.
Sounds simple. However I'm stuck on how to build my loop.
would I use
Code: Select all
$array = ('$email1','$email2','$email3','etc..');
foreach ($array as $email)
{
mail();
}

Posted: Sat Aug 06, 2005 8:00 pm
by feyd
if it's the same email to all of them use the BCC mail header to stick all of them into a single email..
Posted: Sun Aug 07, 2005 7:05 am
by s.dot
They will be different emails.
How do I get each email address into an array?
if I knew there were 10 email addresses put in, it would be easy. However there may be less than 10.
What happens if one of the emails isn't filled in, and lets say half of my array is empty?
EX:
Code: Select all
$allemails = array('$email1','$email2','$email3','$email4','$email5','$email6','$email7','$email8','$email9','$email10');
// above lets say email 6-10 is empty, then I do this
foreach($allemails as $email)
{
mail();
}
// what would happen?
Posted: Sun Aug 07, 2005 7:45 am
by feyd
Code: Select all
$array = array_filter('strlen', $array);
Posted: Sun Aug 07, 2005 9:24 am
by jwalsh
Are your emails being typed into boxes from your users? or are they pre-defined.
If they are being typed into boxes, simply set the name property of each of your textboxes to "email[]". After posting, you will have an $email[] array prefilled for you.
Josh
Posted: Sun Aug 07, 2005 3:25 pm
by s.dot
Okay, I'm having two problems.
#1. My check to see if at least 1 email has been entered always executes, so I figure it's wrong.
#2. 10 emails send every time.. even if only 1 email address was entered.
Here's my code.
Code: Select all
if($_POST['action'] == "invitefriends")
{
if(!isset($_COOKIE['username']))
{
header("Location: login.php");
}
$email1 = mysql_real_escape_string(strip_tags($_POST['email1']));
$email2 = mysql_real_escape_string(strip_tags($_POST['email2']));
$email3 = mysql_real_escape_string(strip_tags($_POST['email3']));
$email4 = mysql_real_escape_string(strip_tags($_POST['email4']));
$email5 = mysql_real_escape_string(strip_tags($_POST['email5']));
$email6 = mysql_real_escape_string(strip_tags($_POST['email6']));
$email7 = mysql_real_escape_string(strip_tags($_POST['email7']));
$email8 = mysql_real_escape_string(strip_tags($_POST['email8']));
$email9 = mysql_real_escape_string(strip_tags($_POST['email9']));
$email10 = mysql_real_escape_string(strip_tags($_POST['email10']));
$message = mysql_real_escape_string(htmlentities($_POST['message'],ENT_QUOTES));
if(!($email1 && $email2 && $email3 && $email4 && $email5 && $email6 && $email7 && $email8 && $email9 && $email10))
{
header("Location: invitefriends.php?error=1");
die();
}
if(!$message)
{
header("Location: invitefriends.php?error=2");
die();
}
$array = array('$email1','$email2','$email3','$email4','$email5','$email6','$email7','$email8','$email9','$email10');
foreach($array as $email)
{
if($email != '')
{
$recipient = $email;
$subject = "An invitation to join ShowMyPro.com";
$body = "email body";
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"Show My Pro.com\" <register@showmypro.com>\n";
mail($recipient,$subject,$body,$headers);
}
}
}
Posted: Sun Aug 07, 2005 4:21 pm
by josh
You could shorten that script by making an array out of your form elements,
Code: Select all
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
<input type="text" id ="email[]" name="email[]" />
then in php
Code: Select all
$array = $_POST['email'];
// The following line makes sure 10 emails aren't sent unless 10 emails are entered
$array = array_filter('strlen', $array);
$count = count($email);
// The following is optional
if ($count > 10) {
// someone tampered with your form
}
// This will make sure that at least one email was entered
if ($count==0) {
// error handler, no email was entered
}
foreach ($array as $email) {
mail($recipient, $whatever, $whatever);
}
You shouldn't need to use mysql_real_escape_string unless it's going into a database, what you should do however is compare the email to a regex to make sure its at
name@domain.tld format. Also I noticed you surrounded your variable names with single quotes, if you want php to expand a variable you should use double quotes, or no quotes at all, however if you use the method I just posted you won't need to worry about it at all. I realize I'm not pointing out anything new that hasn't already been mentioned in this thread, I'm just putting it all together to show you how it all works.
Try out my code, should work.