Jump to content

Sending email to multiple recipients


lukep11a

Recommended Posts

Hi, I have a tell a friend form on my site that can currently email 1 email address with the specified message, I am trying to modify it to email upto 5 email addresses, I have changed the form but am not sure how to adapt the processing code, I can do it by replicating the processing code 5 times but not sure this is the most efficient way. If anyone can help it would be greatly appreciated:

 

Form code:

 

<form action="tellafriend.php" method="POST">
        <fieldset>
        <input name="privateleaguename" type="hidden" value="<?php echo $row['privateleaguename']; ?>">
        <input name="privateleaguepasscode" type="hidden" value="<?php echo $row['privateleaguepasscode']; ?>">
        <legend>Invite a friend</legend><br />
        Your name<br />
        <input type="text" name="your_name" value="Your name" /><br /><br />
        Friends email 1<br />
        <input type="text" name="friend_email_1" value="Your friends email" /><br /><br />
        Friends email 2<br />
        <input type="text" name="friend_email_2" value="Your friends email" /><br /><br />
        Friends email 3<br />
        <input type="text" name="friend_email_3" value="Your friends email" /><br /><br />
        Friends email 4<br />
        <input type="text" name="friend_email_4" value="Your friends email" /><br /><br />
        Friends email 5<br />
        <input type="text" name="friend_email_5" value="Your friends email" /><br /><br />
        <input type="Submit" value="Tell a friend" name="Submit" />
        </fieldset>
        </form>

 

Processing code:

 

<?php
	if (isset($_POST['Submit'])) {
	// This will check to see if the form has been submitted
	$senders_name = $_POST['your_name'];
	// The person who is submitting the form
	$recipient_friend = $_POST['friend_email_1'];
	$privateleaguename = $_POST['privateleaguename'];
	$privateleaguepasscode = $_POST['privateleaguepasscode'];
	// The forms recipient
	mail($recipient_friend,"Invitation to join Private League from $senders_name", "Dear $recipient_friend,\n\nYour friend $senders_name has created a private league and thought you would be interested in joining.\n\nPlease follow the link to register for FREE. Then enter the details below in the 'Join a Private League' section of your account page.\n\nPrivate League Name: $privateleaguename\nPrivate League Passcode: $privateleaguepasscode\n\nThank You

	if (isset($_POST['your_name'])) {
	echo "<br>Your friend at $recipient_friend has been contacted <br><br>Thank you $senders_name";
	}}
	?>

Link to comment
https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/
Share on other sites

You can do it like this:

 

<form action="tellafriend.php" method="POST">
        <fieldset>
        <input name="privateleaguename" type="hidden" value="<?php echo $row['privateleaguename']; ?>">
        <input name="privateleaguepasscode" type="hidden" value="<?php echo $row['privateleaguepasscode']; ?>">
        <legend>Invite a friend</legend><br />
        Your name<br />
        <input type="text" name="your_name" value="Your name" /><br /><br />
        Friends email 1<br />
        <input type="text" name="friend_email[]" value="Your friends email" /><br /><br />
        Friends email 2<br />
        <input type="text" name="friend_email[]" value="Your friends email" /><br /><br />
        Friends email 3<br />
        <input type="text" name="friend_email[]" value="Your friends email" /><br /><br />
        Friends email 4<br />
        <input type="text" name="friend_email[]" value="Your friends email" /><br /><br />
        Friends email 5<br />
        <input type="text" name="friend_email[]" value="Your friends email" /><br /><br />
        <input type="Submit" value="Tell a friend" name="Submit" />
        </fieldset>
</form>

 

<?php
	if (isset($_POST['Submit'])) {
	// This will check to see if the form has been submitted
	$senders_name = $_POST['your_name'];
	// The person who is submitting the form
	$privateleaguename = $_POST['privateleaguename'];
	$privateleaguepasscode = $_POST['privateleaguepasscode'];
	// The forms recipient

	$n = 0;
	$emails = $_POST['friend_email'];
	while($n < count($emails)) { 
		mail($recipient_friend,"Invitation to join Private League from $senders_name", "Dear $emails[$n],\n\nYour friend $senders_name has created a private league and thought you would be interested in joining.\n\nPlease follow the link to register for FREE. Then enter the details below in the 'Join a Private League' section of your account page.\n\nPrivate League Name: $privateleaguename\nPrivate League Passcode: $privateleaguepasscode\n\nThank You");

		$n++;
	}


	if (isset($_POST['your_name'])) {
		$n = 0; 
		while($n < count($emails)) {	
			echo "<br>Your friend at $emails[$n] has been contacted <br><br>Thank you $senders_name";
			$n++;
		}
	}}

?><?php
	if (isset($_POST['Submit'])) {
	// This will check to see if the form has been submitted
	$senders_name = $_POST['your_name'];
	// The person who is submitting the form
	$privateleaguename = $_POST['privateleaguename'];
	$privateleaguepasscode = $_POST['privateleaguepasscode'];
	// The forms recipient

	$n = 0;
	$emails = $_POST['friend_email'];
	while($n < count($emails)) { 
		mail($recipient_friend,"Invitation to join Private League from $senders_name", "Dear $emails[$n],\n\nYour friend $senders_name has created a private league and thought you would be interested in joining.\n\nPlease follow the link to register for FREE. Then enter the details below in the 'Join a Private League' section of your account page.\n\nPrivate League Name: $privateleaguename\nPrivate League Passcode: $privateleaguepasscode\n\nThank You");

		$n++;
	}


	if (isset($_POST['your_name'])) {
		$n = 0; 
		while($n < count($emails)) {	
			echo "<br>Your friend at $emails[$n] has been contacted <br><br>Thank you $senders_name";
			$n++;
		}
	}}

?>

If you're willing to not customize the message with the recipient's name, ie "Dear, (recipient's name)" you can achieve the same thing but without calling the mail function multiple times. If you use the Pear Mail function it's much more efficient at sending multiple messages. The only real difference in the code would be to include an array of email addresses as an argument as opposed to just one like you do now.

 

If you're interested, I just posted an example of how to do this in another thread similar to this one:

 

http://www.phpfreaks.com/forums/index.php?topic=338197.msg1593938#msg1593938

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.