ben03 Posted April 6, 2009 Share Posted April 6, 2009 I am new to php and trying to get my head around sending an html form to an email address where it is sent via a sendmail.php page. The code I have done so far is below but am not sure if its correct/what needs correcting! Thanks. <?php $ToEmail = '[email protected]'; $EmailSubject = 'Site contact form '; $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; $MESSAGE_BODY .= "Phone: ".$_POST["phone"]."<br>"; $MESSAGE_BODY .= "Company: ".$_POST["company"]."<br>"; $MESSAGE_BODY .= "Jobtitle: ".$_POST["jobtitle"]."<br>"; $MESSAGE_BODY .= "Besttime2call: ".$_POST["besttime2call"]."<br>"; $MESSAGE_BODY .= "Howdidyouhear: ".$_POST["howdidyouhear"]."<br>"; $MESSAGE_BODY .= "options: ".$_POST["boxes[]"]."<br>"; $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY) or die ("Failure"); ?> Link to comment https://forums.phpfreaks.com/topic/152797-form-to-email-help/ Share on other sites More sharing options...
JD* Posted April 6, 2009 Share Posted April 6, 2009 What happens when you run it? Link to comment https://forums.phpfreaks.com/topic/152797-form-to-email-help/#findComment-802366 Share on other sites More sharing options...
gevans Posted April 6, 2009 Share Posted April 6, 2009 Because this is being sent as am email not outputted to a browser you should use inplace of <br>. Though <br> can work with some extra headers, your not strcitly sending a html email, so change your code to look like this... <?php $ToEmail = '[email protected]'; $EmailSubject = 'Site contact form '; $MESSAGE_BODY = "Name: ".$_POST["name"]." "; $MESSAGE_BODY .= "Email: ".$_POST["email"]." "; $MESSAGE_BODY .= "Phone: ".$_POST["phone"]." "; $MESSAGE_BODY .= "Company: ".$_POST["company"]." "; $MESSAGE_BODY .= "Jobtitle: ".$_POST["jobtitle"]." "; $MESSAGE_BODY .= "Besttime2call: ".$_POST["besttime2call"]." "; $MESSAGE_BODY .= "Howdidyouhear: ".$_POST["howdidyouhear"]." "; $MESSAGE_BODY .= "options: ".$_POST["boxes[]"]." "; $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])." "; mail($ToEmail, $EmailSubject, $MESSAGE_BODY) or die ("Failure"); ?> Link to comment https://forums.phpfreaks.com/topic/152797-form-to-email-help/#findComment-802368 Share on other sites More sharing options...
ben03 Posted April 7, 2009 Author Share Posted April 7, 2009 That's great thanks for the help. With a checkbox list I have used this one line of code although there are 5 options. Is this correct? $MESSAGE_BODY .= "options: ".$_POST["boxes[]"]." "; "boxes[]" has been used for each of the 'name' attributes. Would the values then be displayed in the email for each of these selected? Link to comment https://forums.phpfreaks.com/topic/152797-form-to-email-help/#findComment-803343 Share on other sites More sharing options...
Axeia Posted April 7, 2009 Share Posted April 7, 2009 If you used names ending in [] it will be an array instead of a string so you can't simply add it. You will probably want to loop trough the array and add the values to a string, like <?php $boxes = ''; foreach( $_POST['boxes'] as $str ) { $boxes .= $str; } //$boxes is what you can use in the mail. ?> You could use the implode function if you want to go from an array to a string directly, but I doubt it would ever give a result you'd want... so I posted you the foreach loop as an example. Since that allows some manipulation within the loop. I recommend having a read at the PHP forms section of w3schools Link to comment https://forums.phpfreaks.com/topic/152797-form-to-email-help/#findComment-803361 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.