Can't get newline char to work in emails

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
mgilsbach
Forum Newbie
Posts: 4
Joined: Thu Mar 02, 2006 12:53 pm

Can't get newline char to work in emails

Post 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
webxan
Forum Newbie
Posts: 3
Joined: Tue Sep 02, 2008 3:32 pm

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

Post 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";
mgilsbach
Forum Newbie
Posts: 4
Joined: Thu Mar 02, 2006 12:53 pm

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

Post by mgilsbach »

That fixed it. Brilliant!

Thank you.

-Mike
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post 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
Post Reply