Mailform help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crooms
Forum Newbie
Posts: 3
Joined: Thu Jul 23, 2009 9:22 am

Mailform help

Post by crooms »

For some reason the line breaks are not processed in the email.

Code: Select all

<?php
ini_set("sendmail_from", "example@btinternet.com");
$mailfrom = "example@btinternet.com";
$mailto = "example@btinternet.com";
$thanks = "thanks.php";
 
    foreach ($_POST as $key=>$value)
    {
    $mailtext .= '<b>' .$key. ':</b>  ' .$value.'<br> ';
    }
    $headers = 'From: '.$mailfrom.'
    Reply-to: '.$mailfrom.'
    Return-path: '.$mailfrom.'
    Content-type: text/html
 
';
mail($mailto, " Site enquiry", $mailtext, $headers);
header("location: $thanks");
 
 
?>
This outputs in the email as

Code: Select all

<b>name:</b>  test<br> <b>address:</b>  test<br> <b>email:</b>  est<br> <b>telephone:</b>  dsates<br> <b>spamcheck:</b>  ok<br> <b>enquiry:</b>  test<br>
I am taking over a web technicians role, and am a relative newbie.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Mailform help

Post by jayshields »

For line breaks in non-HTML email you'll want to use the newline character, "\n". Make sure you include that character using double-quotes.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Mailform help

Post by akuji36 »

Hello

I do not see any line breaks in your code.

try adding

Code: Select all

br/
when you wish to add line break.

Use the forward slash after your br so it looks like: <br/>.
crooms
Forum Newbie
Posts: 3
Joined: Thu Jul 23, 2009 9:22 am

Re: Mailform help

Post by crooms »

Thanks for the help.

I've tried <br/> and /n, it still just displays <br/> or /n instead of doing a line break. It is crazy, it seems correct but just spits it all out on one line.

Is there anything apart from the code that may be causing this, e.g. server settings, or the form which it is getting info from?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Mailform help

Post by jayshields »

It's backslash-n, not forwardslash-n. And remember it needs to be in double quotes, not single quotes. Here's an example:

Code: Select all

$body = "This is my email message body.\n\nThere's a line break above this line.";
$body = 'This is my email message body.' . "\n\n" . 'There is a line break above this line.'; //alternative
crooms
Forum Newbie
Posts: 3
Joined: Thu Jul 23, 2009 9:22 am

Re: Mailform help

Post by crooms »

Thankyou very much for helping a newbie. \n worked.
Post Reply