I need to add a contact form to a website I’m making, and found a useful bit of code someone had kindly shared for the likes of me who have no PHP experience.
After pasting and editing the code into the page and uploading it to a staging site, I tested it using my gmail address and it works fine. The only thing is that the email text has no line breaks in it.
Here’s the code:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'myemailadress@gmail.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="contactus.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"> </textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
This is what I receive when it arrives in my inbox:
Name: Mr J BloggsEmail: email@address.comComment: Comments text.
What I’d like is for it to instead look like this:
Name: Mr J Bloggs
Email: email@address.com
Comment: Comments text.
Thanks in advance for any help.
Just add line break after each row: