Jump to content

Form to email help


ben03

Recommended Posts

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

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

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

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

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.