Question

I have a script which submits information which is being entered in a form to my email.

However, currently when I submit information, I receive it in my email like this:

FirstNameHere

First Name:FirstNameHereLast Name:LastNameHere

But I want it to be in my email like this:

First Name: FirstNameHere

Last Name: LastNameHere

Can anyone help me fix this? This is the code that I currently use.

<?php
$to = "myemailhere@domain.com";
$subject = "Form Information";
$fname = $_POST['fname'] ;
$lname = $_POST['lname'] ;
$message = 'First Name:' . $fname . 'Last Name:' . $lname;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $fname, $lname) ;
?>
Was it helpful?

Solution

You should add new line character between the inputs. PHP - how to create a newline character?

<?php
$to = "myemailhere@domain.com";
$subject = "Form Information";
$fname = $_POST['fname'] ;
$lname = $_POST['lname'] ;
$message = 'First Name:' . $fname ."\r\n". 'Last Name:' . $lname;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
?>

OTHER TIPS

I Agree with ecabuk's answer. But You can use HTML Formatting (with css) too.

<?php
$to = "mail@example.com";
$subject = "Demo";
$message="
<html>
<body>
<table>
<tr><td>First Name : </td><td>".$_POST['fname']."</td></tr>
<tr><td>Last Name : </td><td>".$_POST['lname']."</td></tr>
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <$email>' . "\r\n";
mail($to,$subject,$message,$headers) or die('Error Occurred');
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top