Presto-X Posted October 13, 2009 Share Posted October 13, 2009 Hello everyone, I am working on a tell a friend script for my website, and I am running in to a bit of a problem, I want to send a message to each friend the visiting user adds (I’m using jQuery to add more than one friend field) my field names are fname1 femail1 the 1’s will increase with each set the visiting user adds. If I use this code it display the content and loops just fine: foreach($_POST as $key => $value){ if(substr($key, 0, 5) == "fname"): echo "Name: $value<br />"; endif; if(substr($key, 0, 6) == "femail"): echo "Email: $value<br /><hr />"; endif; } But if I replace this with my basic PHP email code the name and email no longer work. foreach($_POST as $key => $value){ if(substr($key, 0, 5) == "fname"): $friends_name = $value; endif; if(substr($key, 0, 6) == "femail"): $email = $value; endif; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; mail($email, $subject, $emailbody, $headers); } I was thinking this was going to very simple loop each name and email address and send an email, but it turns out I am not doing something right. Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/ Share on other sites More sharing options...
chaiwei Posted October 13, 2009 Share Posted October 13, 2009 before this mail($email, $subject, $emailbody, $headers); insert this echo $email,' - ',$subject,' - ',$emailbody,' - ', $headers; Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-935965 Share on other sites More sharing options...
pastcow Posted October 13, 2009 Share Posted October 13, 2009 A much easier way would be to do this.. <input name="email[]"/> <input name="email[]"/> <input name="email[]"/> And then you can use $_POST['email'][0], $_POST['email'][1] etc... from within your PHP script Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-935967 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 A much easier way would be to do this.. <input name="email[]"/> <input name="email[]"/> <input name="email[]"/> And then you can use $_POST['email'][0], $_POST['email'][1] etc... from within your PHP script This is my code: $i = 0; foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; Thanks pastcow, this seems to be mostly working now, let say I enter in 3 friends names (Joe, Mike, and Emily) it submits 3 emails just fine but it sends all 3 of them to Joe with Joe's name in each one? I was thinking that I may need to clean the variable before the next loop so I try to use unset('my_variable') with out any luck??? This is my code: $i = 0; foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; mail($friends_email, $subject, $emailbody, $headers); $i++; endforeach; unset($friends_name); unset($friends_email); Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936001 Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Does something like this not do the job? foreach($_POST['femail'] as $key => $value): $friends_name = $_POST['fname'][$key]; $friends_email = $_POST['femail'][$key]; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; mail($friends_email, $subject, $emailbody, $headers); endforeach; Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936004 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Thanks for the reply cags, I got the same out come, it sends all 3 emails but they are all the same email. Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936007 Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 What does your HTML look like? Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936009 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 <form action="" method="post" name="form1"> <input type="hidden" id="id" value="1"> <input type="hidden" name="form" value="get_involved" /> <input name="submit_email" type="hidden" value="1" /> Your Full Name:<br /> <input type="text" name="full_name" id="full_name" class="input" /> Your Email Address:<br /> <input type="text" name="email" id="email" class="input" /> <table width="100%" border="0" cellspacing="4" cellpadding="0"> <tbody id="divTxt"> </tbody> <tfoot> <tr> <td><a href="return false;" onClick="addFormField(); return false;"><img src="components/com_tell_a_friend/SpryAssets/add.png" alt="" border="0" /></a></td> </tr> </tfoot> </table> <input type="submit" name="button" id="button" value="Submit" class="button<?PHP if($_POST['submit_email']=="1"){ echo '_disabled"';} ?>" <?PHP if($_POST['submit_email']=="1"){ echo 'disabled="disabled"';} ?> /> </form> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> <script type="text/javascript" src="components/com_tell_a_friend/SpryAssets/add-remove.js"></script> Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936014 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Here is my jquery code: // ADD A FIELD function addFormField(){ var id = document.getElementById("id").value; $("#divTxt").append('<tr id="row' + id + '" class="field"><td><input type="text" name="fname[]" id="fname' + id + '" class="input" value="Full Name" onfocus="if(this.value==\'Full Name\'){this.value=\'\'};" onblur="if(this.value==\'\'){this.value=\'Full Name\'};" /> <input type="text" name="femail[]" id="femail' + id + '" class="input" value="Email Address" onfocus="if(this.value==\'Email Address\'){this.value=\'\'};" onblur="if(this.value==\'\'){this.value=\'Email Address\'};" /> <a href=\'return false;\' onClick=\'removeFormField("#row' + id + '"); return false;\' class="remove"><img src="../components/com_tell_a_friend/SpryAssets/remove.png" alt="" border="0" /></a></td></tr>'); $('#row' + id).highlightFade({ color:'green_yellow', speed:500 }); id = (id - 1) + 1; document.getElementById("id").value = id; } // REMOVE A FIELD function removeFormField(id){ $(id).remove(); } Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936017 Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 I know nothing about JavaScript/jQuery, but from what I can tell it seems to be ok... Here is a simple example of the process working... <?php if(isset($_POST['submit'])) { foreach($_POST['name'] as $k=>$v) { echo "Sending email to " . $v . " at " . $_POST['email'][$k] . '<br/>'; } } ?> <br/> <form action="" method="post"> <input type="text" name="name[]" value="name 1" /><input type="text" name="email[]" value="email 1" /><br /> <input type="text" name="name[]" value="name 2" /><input type="text" name="email[]" value="email 2" /><br /> <input type="text" name="name[]" value="name 3" /><input type="text" name="email[]" value="email 3" /><br /> <input type="submit" name="submit" /> </form> Also try putting this at the top of your page so you can see what the contents of the $_POST array looks like, the answer may jump out at you. echo '<pre>'; print_r($_POST); echo '</pre>'; Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936020 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Ya it looks very simple and I can not see why this is not working grrrr.... So I have added this bit of code in my foreach to see what is getting submitted: foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; echo $friends_name.' '.$friends_email; and it is displaying this: Joe [email protected] [email protected] [email protected] Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936023 Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Which is exactly what it should read. You can see that on each iteration of the loop the email address is different, since your mail call is... mail($friends_email, $subject, $emailbody, $headers); It should send 1 to each of [email protected], [email protected] and [email protected]. It should be practically impossible for any other outcome as you can quite clearly see that $friends_email value is changing. Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936026 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 ok I think it has something to do with this bit of code: $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); $message = str_replace("{date}",date('l, F jS, Y'),$message); $message = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message); Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936042 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 $i = 0; foreach($_POST as $key => $value): echo $_POST['fname'][$i].' '.$_POST['femail'][$i]; $subject = $params->get('subject'); $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); $message = str_replace("{date}",date('l, F jS, Y'),$message); $message = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message); // message $emailbody = 'Hello '.$_POST['fname'][$i].',<br />'; $emailbody .= $message; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; // Mail it mail($_POST['femail'][$i], $subject, $emailbody, $headers); $i++; endforeach; display the following in the email body: Hello Emily, Hello Joe, Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936044 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 so can str_replace be used only once per loop? Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936049 Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 at last I got it working with the following: $message = $params->get('message'); $i = 0; foreach($_POST as $key => $value): echo $_POST['fname'][$i].' '.$_POST['femail'][$i]; $subject = $params->get('subject'); $message2 = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message2 = str_replace("{friends_email}",$_POST['femail'][$i],$message2); $message2 = str_replace("{your_name}",$_POST['full_name'],$message2); $message2 = str_replace("{your_email}",$_POST['email'],$message2); $message2 = str_replace("{date}",date('l, F jS, Y'),$message2); $message2 = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message2); // message $emailbody = 'Hello '.$_POST['fname'][$i].',<br />'; $emailbody .= $message2; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; // Mail it mail($_POST['femail'][$i], $subject, $emailbody, $headers); $i++; unset($message2); endforeach; Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936053 Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Look at it from a logical point of view. Assumably to start with you have somewhere in your code (outside the loop) that says something along the lines of. $message = "Hello {friends_name}, your friend {your_name} ({your_email}) has asked us to send this link... blah..."; Now lets say you enter 3 emails (john ([email protected]), jim ([email protected]) and jane ([email protected]). The first time you run the loop you str_replace the values in that string using... $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); On the second loop of the code your $message variable now reads... $message = "Hello jim, your friend pete ([email protected]) has asked us to send this link... blah..."; So your str_replace has no effect on the string. The fix is, outside of the line to have... $message_template = "Hello {friends_name}, your friend {your_name} ({your_email}) has asked us to send this link... blah..."; $values = array("{friends_name}", "{friends_email}", "{your_name}", "{your_name}"); Then inside the loop use... $replacements = array($_POST['fname'][$key], $_POST['femail'][$key], $_POST['full_name'], $_POST['email']); $message = str_replace($values, $replacements, $message_template); EDIT: Ok you fixed it, but you can probably neaten it up using the arrays. Link to comment https://forums.phpfreaks.com/topic/177514-solved-tell-a-friend-with-muliple-to-addresses/#findComment-936054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.