• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

Enter Multiple Recipients in PHP

I have created a "Tell a Friend" form in which you enter your name, your email, friends name, friends email, and a comment and it sends you an html email.

What we'd like to do is add the option for senders to add multiple recipients.

Instead of "Line 1" Line 2" "Line 3" ect., we'd like to have one "Email" text box where they can enter addresses like so: a@bc.com, d@ef.com, g@hi.com etc. up to however many address they choose.
This version would be an internal site so I am not concerned with Spammers taking it over.

The code as I have it written right now doesn't allow for more than one address.

I am new to php and don't know enough about the code to tweak it to my needs.

URGENT! PLEASE HELP!!

Attached is my current coding.
<?php
 
function isEmail($email) {
        return (preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email))? true : false;
}
 
$FriendName = $_POST['FriendName'];
$FriendEmail = (isEmail($_POST['FriendEmail']))? $_POST['FriendEmail'] : "";
$SenderName = $_POST['SenderName'];
$SenderEmail = (isEmail($_POST['SenderEmail']))? $_POST['SenderEmail'] : "info@campushousing.com";
$emailtext = $_POST['emailtext'];
$string = $_SERVER['QUERY_STRING'];
 
if ($FriendEmail == "" || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");
 
 
?>
<?php
if (!$FriendEmail) {
?>
<?php
}
else {
if ($FriendEmail) {
 
$subject = "Message from $SenderName from www.campushousing.com/acu";
//format the message as html and add the image here.  something like:
$message .= "<html>";
$message .= "<head>";
$message .= "<style type=\"text/css\" media=\"screen\">";
$message .= "a:link, a:visited {color:#A0C804; text-decoration:none;}";
$message .= "a:hover {color:#339933; text-decoration:none;}";
$message .= "body {background-color:#FFFFFF;";
$message .= "font-family:Arial, Helvetica, sans-serif;";
$message .= "color:#514f50;";
$message .= "font-size:10px;}";
$message .= "p {font-size: 11px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= "h1 {font-size: 14px;";
$message .= "line-height: normal;";
$message .= "font-weight:bold;";
$message .= "margin-bottom:-5pt;}";
$message .= ".page {background-color:#ecebeb;";
$message .= "padding:0px;";
$message .= "width:570px;}";
$message .= ".content {width:570px;";
$message .= "height:auto;";
$message .= "background-color:#FFFFFF;";
$message .= "padding:0px;";
$message .= "border:#ECEBEB;}";
$message .= ".unsubscribe {width:570px;";
$message .= "padding-left:0px;";
$message .= "padding-right:0px;}";
$message .= ".unsubscribe p{font-size:10px;}";
$message .= ".smallprint p{font-size: 9px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= ".style2 {font-size: 30px}";
$message .= ".style5 {color: #A0C804;";
$message .= "font-size: 30px;";
$message .= "font-weight: bold;}";
$message .= "</style>";
$message .= "<body>";
$message .= "<div class=\"page\">";
$message .= "<div class=\"content\">";
$message .= "<table width=\"570\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#ECEBEB\">";
$message .= "<tr>";
$message .= "<td><img src=\"http://www.campushousing.com/postcards/CLVNoWorriesApartmentHunting05.jpg\" alt=\"no worries!\" width=\"570\" height=\"847\" /></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td><table width=\"570\" cellpadding=\"0\" cellspacing=\"0\">";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td width=\"536\" cellpadding=\"20\" align=\"left\"><br>";
$message .= "<p>$FriendName,</p><br>";
$message .= "<p>".$emailtext."</p><br>";
$message .= "<p>-$SenderName<br><br></p>  ";            
$message .= "</td>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "</table></td>";
$message .= "</tr>";
$message .= "</table>"; 
$message .= "</div>";
$message .= "</div>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $SenderEmail\n" .
    "Reply-To: $SenderEmail\n" .
    'Content-type: text/html; charset=iso-8859-1' . "\n" .
    'X-Mailer: PHP/' . phpversion() . "\n";
    'MIME-Version: 1.0' . "\n" .
 
 
mail($FriendEmail, $subject, $message, $headers);
 
                    
  }
 
 
echo "<center><br><br><font face=Arial size=3 color=black>Thank you <font color=green>$SenderName</font>. The email has been sent to <font color=green>$FriendName</font>. <br><br></font><p>";
}
?>

Open in new window

0
natsully
Asked:
natsully
  • 10
  • 7
  • 3
2 Solutions
 
khaledfCommented:
get emails in the form email1@domain.com, email2@domain2.com, ....
thats all,
mail function will do the rest.

but you need to validate all emails first.
0
 
khaledfCommented:
FriendName will not be required to send to multiple recipients, or is it?
0
 
natsullyAuthor Commented:
I get the error "Please enter a valid email for your or your friend." when I enter emails like that.
So something in that 1st part is preventing multiple email addresses.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nanharbisonCommented:
here is my refer a friend form

	<div align="left"><table width= "100%"><tr> 
    	<td> <form action="'.$_SERVER['PHP_SELF'].'" method="post" name="refer-friend"> 
	    <table width="100%" cellpadding = "4" border="0">
		<tr>
	        <td colspan="2">Enter up to five friends\' email addresses, separated by commas.</td>
		</tr>
		<tr>
	        <td colspan="2"><textarea name="friendemail" id="friendemail" cols="42" rows="4"></textarea></td>
        </tr>
        <tr>
           	<td><span class="right" width="40%">Your Name: </span></td>
			<td align="left" width="60%">Your Email Address:</td>
		</tr>			  
	    <tr>
	        <td><input type="text" name="name"></td>
	        <td align="left"><input type="text" name="email"></td>
        </tr>
	 	<tr>
	   		<td colspan="2">Write a note to your friend(s) who will receive this invitation to join our website(optional):</td>
		</tr>
		<tr>
			<td colspan="2"><textarea name="message" cols="42" rows="4"></textarea></td>
		</tr>
       	<tr>
			<td colspan="2" align="center"><input type="submit" class="submitbutton" name="submit" value="Send"> </td>
		</tr>
        </table>
      </form>
    </td>
  </tr>
</table></div>

Open in new window

0
 
natsullyAuthor Commented:
Can you explain the .$_SERVER['PHP_SELF']?

and I need to be able to enter more info
0
 
nanharbisonCommented:
then here is my submit code, which allows any number of emails even though the form says 5 email addresses. I added a check to see if the entry in the form is spam, just some basic stuff, although I don't check for valid email addresses. Use your own subject and headers.

$referform is the form above, entered as the content on the page.

 if (isset($_POST['submit']))
	  { 
	  //clean list of potential bad code
 		while(list($key, $value) = each($_POST))
		{
			$value = trim($value);
                        //you have to be connected to a db to use this line
			$value = mysql_real_escape_string($value);
		}	
	 // echo  $_POST['name'].' '.$_POST['email'].' '.$_POST['phone'].' '.$_POST['select'];
	 //print("ok<br>");
	 $findspam1 = strpos($_POST['message'], "[");
	 $findspam2 = strpos($_POST['message'], "]");
	 $findspam3 = strpos($_POST['message'], "url");
	 $findspam4 = strpos($_POST['message'], "http://");
	 $findspam5 = strpos($_POST['message'], "link=");
	 if ($findspam1==false && $findspam2==false && $findspam3==false && $findspam4==false && $findspam5==false)
	 {
	  if (($_POST['name']) && ($_POST['friendemail']) && ($_POST['email']))
	  {
	  	$friendemails = $_POST['friendemail'];
		$separateemails = split(",",$friendemails);
		$count = count($separateemails);
		for ( $i = 0; $i < $count; $i ++) {
			//echo $separateemails[$i];
      mail($separateemails[$i], $subject, $_POST['message'], $headers);
 
				
		}
		$flag="yes";
		if ($flag == "yes") {
			
				$content .=  'Thanks for spreading the word about us and for being a part of our exciting growth! Invite more friends or return to the home page.</a><br><br>';
				$content .=  $referform;
  		 	} else {
   				$content .=  'You email could not be sent. Please try again later.';
			}
	} else {
		$content .= '<h2>Please be sure you have filled out your name, email address, type of question. Thank you, we appreciate your comments.</h2>';
		$content .=  $referform;
		$content .=  '<br><br><br>';
	} 
}

Open in new window

0
 
khaledfCommented:
isEmail function should be called with one email at a time
let me produce the code for that
0
 
nanharbisonCommented:
.$_SERVER['PHP_SELF'] means the action doesn't take you to a new page, it stays on the same page.

0
 
khaledfCommented:
try this code, though not sure if it will work without errors.
<?php
 
function isEmail($email) {
        return (preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email))? true : false;
}
$tempArray;
$FriendsEmails=split(",",$_POST['FriendEmail']);
foreach($FriendsEmails as $email){
        $email=trim($email);
        if(!empty($email) && isEmail($email)){
         array_push($tempArray,$email);
        }else{
            echo "email: " . $email . " is not valid email.";
            exit;
        }
}
 
$FriendEmail= implode(', ',$tempArray);
$FriendName = $_POST['FriendName'];
$SenderName = $_POST['SenderName'];
$SenderEmail = (isEmail($_POST['SenderEmail']))? $_POST['SenderEmail'] : "info@campushousing.com";
$emailtext = $_POST['emailtext'];
$string = $_SERVER['QUERY_STRING'];
 
if ($FriendEmail == "" || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");
 
 
?>
<?php
if (!$FriendEmail) {
?>
<?php
}
else {
if ($FriendEmail) {
 
$subject = "Message from $SenderName from www.campushousing.com/acu";
//format the message as html and add the image here.  something like:
$message .= "<html>";
$message .= "<head>";
$message .= "<style type=\"text/css\" media=\"screen\">";
$message .= "a:link, a:visited {color:#A0C804; text-decoration:none;}";
$message .= "a:hover {color:#339933; text-decoration:none;}";
$message .= "body {background-color:#FFFFFF;";
$message .= "font-family:Arial, Helvetica, sans-serif;";
$message .= "color:#514f50;";
$message .= "font-size:10px;}";
$message .= "p {font-size: 11px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= "h1 {font-size: 14px;";
$message .= "line-height: normal;";
$message .= "font-weight:bold;";
$message .= "margin-bottom:-5pt;}";
$message .= ".page {background-color:#ecebeb;";
$message .= "padding:0px;";
$message .= "width:570px;}";
$message .= ".content {width:570px;";
$message .= "height:auto;";
$message .= "background-color:#FFFFFF;";
$message .= "padding:0px;";
$message .= "border:#ECEBEB;}";
$message .= ".unsubscribe {width:570px;";
$message .= "padding-left:0px;";
$message .= "padding-right:0px;}";
$message .= ".unsubscribe p{font-size:10px;}";
$message .= ".smallprint p{font-size: 9px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= ".style2 {font-size: 30px}";
$message .= ".style5 {color: #A0C804;";
$message .= "font-size: 30px;";
$message .= "font-weight: bold;}";
$message .= "</style>";
$message .= "<body>";
$message .= "<div class=\"page\">";
$message .= "<div class=\"content\">";
$message .= "<table width=\"570\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#ECEBEB\">";
$message .= "<tr>";
$message .= "<td><img src=\"http://www.campushousing.com/postcards/CLVNoWorriesApartmentHunting05.jpg\" alt=\"no worries!\" width=\"570\" height=\"847\" /></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td><table width=\"570\" cellpadding=\"0\" cellspacing=\"0\">";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td width=\"536\" cellpadding=\"20\" align=\"left\"><br>";
$message .= "<p>$FriendName,</p><br>";
$message .= "<p>".$emailtext."</p><br>";
$message .= "<p>-$SenderName<br><br></p>  ";
$message .= "</td>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "</table></td>";
$message .= "</tr>";
$message .= "</table>";
$message .= "</div>";
$message .= "</div>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $SenderEmail\n" .
    "Reply-To: $SenderEmail\n" .
    'Content-type: text/html; charset=iso-8859-1' . "\n" .
    'X-Mailer: PHP/' . phpversion() . "\n";
    'MIME-Version: 1.0' . "\n" .
 
 
mail($FriendEmail, $subject, $message, $headers);
 
 
  }
 
 
echo "<center><br><br><font face=Arial size=3 color=black>Thank you <font color=green>$SenderName</font>. The email has been sent to <font color=green>$FriendName</font>. <br><br></font><p>";
}
?>

Open in new window

0
 
natsullyAuthor Commented:
says "Warning: array_push(): First argument should be an array in /usr/local/plesk/apache/vhosts/campushousing.com/httpdocs/acu/html/thanks_test.php on line 11

Warning: array_push(): First argument should be an array in /usr/local/plesk/apache/vhosts/campushousing.com/httpdocs/acu/html/thanks_test.php on line 11

Warning: implode(): Bad arguments. in /usr/local/plesk/apache/vhosts/campushousing.com/httpdocs/acu/html/thanks_test.php on line 18
Please enter a valid email for your or your friend."
0
 
khaledfCommented:
change the lin
$tempArray;

to
$tempArray= Array();
0
 
natsullyAuthor Commented:
amazing. That worked!!!
The only thing is all of the addresses I enter show up in the "To" line.
Is there anyway to send them and them not be able to see who else it was sent to?
0
 
khaledfCommented:
oh, sure ,  you will need to loop through the mail function. and the could need to be changed to somthing similar to


<?php
 
function isEmail($email) {
        return (preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email))? true : false;
}
$tempArray= Array();
$FriendsEmails=split(",",$_POST['FriendEmail']);
foreach($FriendsEmails as $email){
        $email=trim($email);
        if(!empty($email) && isEmail($email)){
         array_push($tempArray,$email);
        }else{
            echo "email: " . $email . " is not valid email.";
            exit;
        }
}
 
$FriendEmail= $tempArray; //implode(', ',$tempArray);
$FriendName = $_POST['FriendName'];
$SenderName = $_POST['SenderName'];
$SenderEmail = (isEmail($_POST['SenderEmail']))? $_POST['SenderEmail'] : "info@campushousing.com";
$emailtext = $_POST['emailtext'];
$string = $_SERVER['QUERY_STRING'];
 
if (sizeOf($FriendEmail > 0) || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");
 
 
?>
<?php
if (!sizeOf($FriendEmail) > 0) {
?>
<?php
}
else {
if (sizeOf($FriendEmail)>0) {
 
$subject = "Message from $SenderName from www.campushousing.com/acu";
//format the message as html and add the image here.  something like:
$message .= "<html>";
$message .= "<head>";
$message .= "<style type=\"text/css\" media=\"screen\">";
$message .= "a:link, a:visited {color:#A0C804; text-decoration:none;}";
$message .= "a:hover {color:#339933; text-decoration:none;}";
$message .= "body {background-color:#FFFFFF;";
$message .= "font-family:Arial, Helvetica, sans-serif;";
$message .= "color:#514f50;";
$message .= "font-size:10px;}";
$message .= "p {font-size: 11px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= "h1 {font-size: 14px;";
$message .= "line-height: normal;";
$message .= "font-weight:bold;";
$message .= "margin-bottom:-5pt;}";
$message .= ".page {background-color:#ecebeb;";
$message .= "padding:0px;";
$message .= "width:570px;}";
$message .= ".content {width:570px;";
$message .= "height:auto;";
$message .= "background-color:#FFFFFF;";
$message .= "padding:0px;";
$message .= "border:#ECEBEB;}";
$message .= ".unsubscribe {width:570px;";
$message .= "padding-left:0px;";
$message .= "padding-right:0px;}";
$message .= ".unsubscribe p{font-size:10px;}";
$message .= ".smallprint p{font-size: 9px;";
$message .= "line-height: 16px;";
$message .= "font-weight: normal;";
$message .= "padding-right:20px;";
$message .= "padding-left:17px;";
$message .= "margin-bottom:-5pt;}";
$message .= ".style2 {font-size: 30px}";
$message .= ".style5 {color: #A0C804;";
$message .= "font-size: 30px;";
$message .= "font-weight: bold;}";
$message .= "</style>";
$message .= "<body>";
$message .= "<div class=\"page\">";
$message .= "<div class=\"content\">";
$message .= "<table width=\"570\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#ECEBEB\">";
$message .= "<tr>";
$message .= "<td><img src=\"http://www.campushousing.com/postcards/CLVNoWorriesApartmentHunting05.jpg\" alt=\"no worries!\" width=\"570\" height=\"847\" /></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td><table width=\"570\" cellpadding=\"0\" cellspacing=\"0\">";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td width=\"536\" cellpadding=\"20\" align=\"left\"><br>";
$message .= "<p>Friends,</p><br>";
$message .= "<p>".$emailtext."</p><br>";
$message .= "<p>-$SenderName<br><br></p>  ";
$message .= "</td>";
$message .= "<td width=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "<td height=\"17\" align=\"left\" bgcolor=\"#ECEBEB\"> </td>";
$message .= "</tr>";
$message .= "</table></td>";
$message .= "</tr>";
$message .= "</table>";
$message .= "</div>";
$message .= "</div>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $SenderEmail\n" .
    "Reply-To: $SenderEmail\n" .
    'Content-type: text/html; charset=iso-8859-1' . "\n" .
    'X-Mailer: PHP/' . phpversion() . "\n";
    'MIME-Version: 1.0' . "\n" .
 
foreach($FriendEmail as $friendEmail){ 
   mail($friendEmail, $subject, $message, $headers);
}
 
 
  }
 
 
echo "<center><br><br><font face=Arial size=3 color=black>Thank you <font color=green>$SenderName</font>. The email has been sent to <font color=green>your friends</font>. <br><br></font><p>";
}
?>

Open in new window

0
 
natsullyAuthor Commented:
It gives a parse error on the

foreach($FriendEmail as $friendEmail){

line.
0
 
khaledfCommented:
these 2 lines
 'X-Mailer: PHP/' . phpversion() . "\n";
    'MIME-Version: 1.0' . "\n" .

should be

 'X-Mailer: PHP/' . phpversion() . "\n".
    'MIME-Version: 1.0' . "\n" ;
0
 
natsullyAuthor Commented:
That helped.
It was giving me the "Please enter a valid email" error message though.

I took out the "if (sizeOf($FriendEmail > 0) || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");" line and it worked?
0
 
khaledfCommented:
Ooops

sizeOf($FriendEmail > 0

should be
sizeOf($FriendEmail <= 0
0
 
khaledfCommented:
Ooops again. this is wrong. LOL

ignore my last post
0
 
khaledfCommented:
you mean this line?
if (sizeOf($FriendEmail > 0) || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");

yes indeed it should be

if (sizeOf($FriendEmail <= 0) || $SenderEmail == "") die ("Please enter a valid email for your or your friend.");
0
 
natsullyAuthor Commented:
Thanks for your help!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now