Page 1 of 1

Can't get newline char to work in emails

Posted: Tue Sep 09, 2008 3:40 pm
by mgilsbach
Hi,

I am using mail() to send emails and I can't get newline characters (\n or \r\n) to work in the formatting of the message body.

Here is my code:

Code: Select all

    $from = 'From: ' . $_POST['customer_name'] . '<' . $_POST['customer_email'] . '>';
    if ($_POST['form_num'] == '1')
    {
        $to = ($_POST['short_target'] == 'Sales') ? 'mike@gilsbachdesigns.com' : 'mike.gilsbach@quest.com';
        $subject = 'Customer Contact, Short Form';
        $body = 'This is a short form email from garmanrouting.com directed to ' . $_POST['short_target'] . '\n';
        $body = $body . 'Name: ' . $_POST['customer_name'] . '\n' ;
        $body = $body . 'Company: ' . $_POST['customer_company'] . '\r\n';
        $body = $body . 'Phone: ' . $_POST['customer_phone'] . '\r\n' ;
        $body = $body . 'Email: ' . $_POST['customer_email'] . '\n\n' ;
        $body = $body . 'Message: \n' . $_POST['customer_needs']  ;
    }else{
        $to = 'mike@gilsbachdesigns.com';
        $subject = 'Customer Contact, Detailed Form';
        $body = 'This is a detailed form email.';
    }
    mail($to,$subject,$body,$from);
Everything else, works but in the email, the newlines just show as "/n", not new lines.

Any suggestions?

TIA,
Mike

Re: Can't get newline char to work in emails

Posted: Tue Sep 09, 2008 3:45 pm
by webxan
try using double quote instead of single quote in the $body like this

Code: Select all

$body = $body . "Name: " . $_POST["customer_name"] . "\n";

Re: Can't get newline char to work in emails

Posted: Tue Sep 09, 2008 3:49 pm
by mgilsbach
That fixed it. Brilliant!

Thank you.

-Mike

Re: Can't get newline char to work in emails

Posted: Tue Sep 09, 2008 10:34 pm
by josh
Your script allows me to inject headers and use your server as a spam bot
http://www.php-security.org/MOPB/MOPB-34-2007.html