trevor4370 Posted June 26, 2009 Share Posted June 26, 2009 I have picked up a "Tell a Friend" script which is very basic. A simple form where you enter Your name Your email Friend's name 1 Friend's email 1 etc for 5 friends Like most scripts a few things need to be changed. This script works fine, and i have got it to a place I am happy with except for two things a bit out of my league at this moment. When the friend receives the email the "To" field has a list of the friends email addresses, as does normal emails do when sending "To". I would like the name to be displayed with the email address. I have spent hours on this trying to work it out, Yes I have read the PHP manual. "User <[email protected]>, Another User <[email protected]>" and $headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; I have $friend_name1 (name of the friend) and $friend_email1 (email of the friend) etc . I have the following code function form_process() { $from_name = $_POST["your_name"]; $from_email = $_POST["your_email"]; $to = $_POST["your_email"].",".$_POST["friend_email2"].",".$_POST["friend_email3"].",".$_POST["friend_email4"].",".$_POST["friend_email5"]; $message = $_POST["message"]; $error_count = validate_form_items(); if($error_count == 0) { if(email($from_email, $from_name, $to, $message)) header("Location: thankyou.html"); else { global $errors; $errors[] = "Email coudn't be send at this time. <br>Please report the webmaster of this error."; } } } I am having a problem with $to = $_POST["your_email"].",".$_POST["friend_email2"].",".$_POST["friend_email3"].",".$_POST["friend_email4"].",".$_POST["friend_email5"]; How do i add $_POST["friend_name1"] etc in the line ..... I have tried everything but the right thing so far. Thanks for any feedback Link to comment https://forums.phpfreaks.com/topic/163735-string-to-problem-in-form/ Share on other sites More sharing options...
dzelenika Posted June 26, 2009 Share Posted June 26, 2009 What error do you get?. What is the name of your field "friend_name1"? Is your script passing validation function validate_form_items()? Link to comment https://forums.phpfreaks.com/topic/163735-string-to-problem-in-form/#findComment-863979 Share on other sites More sharing options...
trevor4370 Posted June 28, 2009 Author Share Posted June 28, 2009 I don't get any errors - the email just does not send from what I have tried. Here is the script in full. I have added the names of the friends to the origional script as well as friends emails 2-5 (As I need five friends in total). I have removed a Message field from the form, so the script referrs to this but it is no longer there. This script is working as I have not tried to add the names to the $to. Actually i would prefer if the friends received the email as $bcc. //minimum characters allowed in the message box $msg_min_chars = "0"; //maximum characters allowed in the message box $msg_max_chars = "0"; $errors = array(); function validate_form_items() { global $msg_min_chars, $msg_max_chars; $msg_chars = "{".$msg_min_chars.",".$msg_max_chars."}"; $form_items = array( "name" => array( "regex" => "/^([a-zA-Z '-]+)$/", "error" => "name appears to be in improper format", ), "email" => array( "regex" => "/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/", "error" => "email address is invalid", ), "message" => array( "regex" => "/.*/", "error" => "Your message is either too short or exceeds $msg_max_chars characters", ), ); global $errors; if(!preg_match($form_items["name"]["regex"], $_POST["your_name"])) $errors[] = " Your ".$form_items["name"]["error"]; if(!preg_match($form_items["email"]["regex"], $_POST["your_email"])) $errors[] = "your ".$form_items["email"]["error"]; if(!preg_match($form_items["name"]["regex"], $_POST["friend_name1"])) $errors[] = " Friend 1 ".$form_items["name"]["error"]; if(trim($_POST["friend_name2"]) != "") { if(!preg_match($form_items["name"]["regex"], $_POST["friend_name2"])) $errors[] = " Friend 2 ".$form_items["name"]["error"]; } if(trim($_POST["friend_name3"]) != "") { if(!preg_match($form_items["name"]["regex"], $_POST["friend_name3"])) $errors[] = " Friend 3 ".$form_items["name"]["error"]; } if(trim($_POST["friend_name4"]) != "") { if(!preg_match($form_items["name"]["regex"], $_POST["friend_name4"])) $errors[] = " Friend 4 ".$form_items["name"]["error"]; } if(trim($_POST["friend_name5"]) != "") { if(!preg_match($form_items["name"]["regex"], $_POST["friend_name5"])) $errors[] = " Friend 5 ".$form_items["name"]["error"]; } if(!preg_match($form_items["email"]["regex"], $_POST["friend_email1"])) $errors[] = "Friend 1 ".$form_items["email"]["error"]; if(strlen(trim($_POST["message"])) < $msg_min_chars || strlen(trim($_POST["message"])) > $msg_max_chars ) $errors[] = $form_items["message"]["error"]; if(trim($_POST["friend_email2"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email2"])) $errors[] = "Friend 2 ".$form_items["email"]["error"]; } if(trim($_POST["friend_email3"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email3"])) $errors[] = "Friend 3 ".$form_items["email"]["error"]; } if(trim($_POST["friend_email2"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email4"])) $errors[] = "Friend 4 ".$form_items["email"]["error"]; } if(trim($_POST["friend_email5"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email5"])) $errors[] = "Friend 5 ".$form_items["email"]["error"]; } return count($errors); } function email($from, $from_name, $to, $message) { //header("Location: thankyou.html");return; $headers .= "From: ".$from."\r\n"; $headers .= "Content-type: text/plain; charset=ISO-8859-1"; $your_domian_name = "Brass Monkey Season®"; //edit what you want your vistors to see in their email here $subject = $from_name." sent you an invitation to $your_domian_name"; $your_message = "Hi!\r\r\n"; $your_message.= ucfirst($from_name); $your_message.= " has just booked a holiday online at www.domain name .com.au and has gone in the draw to win one of two $1,000 getaways to WHEREEVER.\r\r They thought you might be interested in finding out more about this trip, so go to www.Domain name.com.au to find out what all the fuss is about.\r\r See you soon,\r From sender\r\n"; $message=$your_message.stripslashes($message); if (mail($to,$subject,$message,$headers) ) { return true; } else { return false; } } function print_error($errors) { foreach($errors as $error) { $err.=$error."<br/>"; } echo "<div style=\"border:1px red solid; font-size:14px; font-weight:normal; color:red; margin:10px; padding:10px;\"> $err <div>"; } function form_process() { $from_name = $_POST["your_name"]; $from_email = $_POST["your_email"]; $to = $_POST["your_email"].",<". $_POST["friend_email2"].">,".$_POST["friend_email3"].",".$_POST["friend_email4"].",".$_POST["friend_email5"]; $message = $_POST["message"]; $error_count = validate_form_items(); if($error_count == 0) { if(email($from_email, $from_name, $to, $message)) header("Location: thankyou.html"); else { global $errors; $errors[] = "Email coudn't be send at this time. <br>Please report the webmaster of this error."; } } } if(isset($_POST["submit"])) form_process(); ?> <html> <title>Tell a friend Brass Monkey</title> <head> </head> <body> <form id="test" method="post" action="<?php echo $PHP_SELF?>" > <table border="0" align="center"> <tr> <td colspan="3" bgcolor="#FFFFFF" style="border-bottom:1px solid black;"> <font size="+2"><b>Tell A Friend</b></font> </td> </tr> <tr> <td colspan="3" bgcolor="#FFFFFF"> <?php global $errors; if(count($errors) != 0){ print_error($errors); } ?> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> <b> Name:*</b> </td> <td bgcolor="#FFFFFF"> <b>Email:*</b> </td> </tr> <tr> <td bgcolor="#FFFFFF">Your </td> <td bgcolor="#FFFFFF"> <input type="text" name="your_name" id="name" size="20" maxlength="25" value="<?php echo $_POST["your_name"]?>"> </td> <td bgcolor="#FFFFFF"> <input type="text" name="your_email" id="email" size="40" maxlength="80" value="<?php echo $_POST["your_email"]?>"> </td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF">Friend 1</td> <td valign="top" bgcolor="#FFFFFF"> <input type="text" name="friend_name1" id="name" size="20" maxlength="25" value="<?php echo $_POST["friend_name1"]?>"> </td> <td valign="top" bgcolor="#FFFFFF"><input type="text" name="friend_email1" id="name7" size="40" maxlength="80" value="<?php echo $_POST["friend_email1"]?>"></td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF">Friend 2</td> <td valign="top" bgcolor="#FFFFFF"> <input type="text" name="friend_name2" id="name" size="20" maxlength="25" value="<?php echo $_POST["friend_name2"]?>"> </td> <td valign="top" bgcolor="#FFFFFF"><input type="text" name="friend_email2" id="name6" size="40" maxlength="80" value="<?php echo $_POST["friend_email2"]?>"></td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF">Friend 3</td> <td valign="top" bgcolor="#FFFFFF"> <input type="text" name="friend_name3" id="name" size="20" maxlength="25" value="<?php echo $_POST["friend_name3"]?>"> </p> </td> <td valign="top" bgcolor="#FFFFFF"><input type="text" name="friend_email3" id="name5" size="40" maxlength="80" value="<?php echo $_POST["friend_email3"]?>"></td> </tr> <tr> <td bgcolor="#FFFFFF">Friend 4</td> <td bgcolor="#FFFFFF"><p><b> <input type="text" name="name" id="name2" size="20" maxlength="25" value="<?php echo $_POST["friend_name4"]?>"> </b></p> </td> <td valign="top" bgcolor="#FFFFFF"><input type="text" name="friend_email4"" id="name4" size="40" maxlength="80" value="<?php echo $_POST["friend_email4"]?>"></td> </tr> <tr> <td bgcolor="#FFFFFF">Friend 5</td> <td valign="top" bgcolor="#FFFFFF"><input type="text" name="friend_name4" id="name" size="20" maxlength="25" value="<?php echo $_POST["friend_name4"]?>"></td> <td bgcolor="#FFFFFF"><input type="text" name="friend_email5" id="name3" size="40" maxlength="80" value="<?php echo $_POST["friend_email5"]?>"></td> </tr> <tr> <td colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td colspan="3" bgcolor="#FFFFFF"><input type="submit" value="submit" name="submit" ></td> </tr> <tr> <td colspan="3" align="right"> </td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/163735-string-to-problem-in-form/#findComment-865221 Share on other sites More sharing options...
dzelenika Posted June 28, 2009 Share Posted June 28, 2009 Is your site on windows? This is citation from PHP's help: Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine). Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP. As such, the to parameter should not be an address in the form of "Something <[email protected]>". The mail command may not parse this properly while talking with the MTA. Link to comment https://forums.phpfreaks.com/topic/163735-string-to-problem-in-form/#findComment-865250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.