easy loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

easy loop question

Post 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();
}
:?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$array = array_filter('strlen', $array);
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
		}
	}
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
Post Reply