lukep11a Posted July 8, 2011 Share Posted July 8, 2011 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"; }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/ Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 what do you mean replicate? if you have 5 recepients then you also have 5 mail() functions? You can make your form fields as an array then from there you can just loop the value of the email addresses... Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240343 Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 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++; } }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240381 Share on other sites More sharing options...
lukep11a Posted July 9, 2011 Author Share Posted July 9, 2011 Thanks for your response, I've tried that and it doesn't work, the confirmation message is displayed correctly but no emails are actually sent, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240453 Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 Sory dude i miss out something pls. Change the first part of the mail function from mail($recepients_friend to mail($emails[$n] Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240464 Share on other sites More sharing options...
lukep11a Posted July 9, 2011 Author Share Posted July 9, 2011 Thanks, that works now, is there any way to only display the thankyou message for the number of friends emailed? It currently shows 5 thankyou messages no matter how many email addresses you enter? Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240473 Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 Yep. Currently it will display 5 thank you message. If you only want to display the message once remove it inside the mail function and have it echo outside the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240480 Share on other sites More sharing options...
lukep11a Posted July 9, 2011 Author Share Posted July 9, 2011 Thanks for your help, it's working perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240561 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241449-sending-email-to-multiple-recipients/#findComment-1240564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.