5

I'm writing an e-mail message to be sent on submission of a form. For some reason, the \n character displays correctly on all lines but one. I can't see any difference between this line and the others, so can't work out why it isn't working. The code is below, together with the output that I'm getting as well as a list of the variables used.

As you can see from the email, the \n on the end of the 'Work Address' line is not working, so "Company Name" just follows straight on, rather than being on the next line. Then the \n after company name works fine, so home address is on the next line.

Here's the code for the message:

$xmsg = "First Name: $firstname\n" .
        "Last Name: $lastname\n" .
    "Work Tel: $worktel\n" .
    "Home Tel: $hometel\n" .
    "Mobile Tel: $mobtel\n" .
    "E-mail: $email\n" .
    "Work Address: $workad\n" .
    "Company Name: $company\n" .
    "Home Address: $homead\n" .

Here's the output:

First Name: Joe
Last Name: Bloggs
Work Tel: 0123456789
Home Tel: 0987654321
Mobile Tel: 0789456123
E-mail: joe.bloggs@jb.co.uk
Work Address: Bloggoffice, Joe Street, London, N1 J0E Company Name: 
Home Address: 9 Blogg Street, Borough of Joe, London, SE4 J03

And in case it's useful, here's the list of variables used:

$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$worktel = $_SESSION['worktel'];
$hometel = $_SESSION['privtel'];
$mobtel = $_SESSION['mobtel'];
$email = $_SESSION['email'];
$company = $_SESSION['companyname'];
$workad = $_SESSION['workad1'] . ', ' . $_SESSION['workad2'] . ', ' .     $_SESSION['workad3'] . ', ' . $_SESSION['workpostcode'];
$homead = $_SESSION['homead1'] . ', ' . $_SESSION['homead2'] . ', ' . $_SESSION['homead3'] . ', ' . $_SESSION['homepostcode'];

I'd be grateful for any help, I hope it's not just a stupid mistake that I've overlooked!

hakre
  • 178,314
  • 47
  • 389
  • 754
Chris
  • 1,763
  • 5
  • 27
  • 41
  • 6
    What does `var_dump($workad)` say? – Adam Wright Jan 16 '12 at 16:09
  • 3
    Please [don't add signatures or taglines to your posts](http://stackoverflow.com/faq#signatures). – meager Jan 16 '12 at 16:09
  • on a side note, it's not really a good practise to clutter up your session namespace with all those variables – jere Jan 16 '12 at 16:17
  • Chris, don't worry too much about the signature thing (as in, don't do it again, but don't worry that you didn't notice that bit of the FAQ ;) ). Yours was pretty innocuous. Questions are supposed to be more like wikipedia articles than a forum discussion or email exchange. They're saved for all posterity. Let us know what `var_dump($workad)` spits out, just edit your question with the results. – Daniel Bingham Jan 16 '12 at 16:18
  • Hi all, sorry about the signature thing, my mistake. var_dump($workad) simply came up with 'NULL' – Chris Jan 16 '12 at 16:26
  • The plot thickens. If I add a '.' to the end of the string that makes up the 'workad' variable, it seems to work. Anyone have any ideas? This method will do as a workaround for now but ideally I'd rather not have to rely on it. The variable is now: `$workad = $_SESSION['workad1'] . ', ' . $_SESSION['workad2'] . ', ' . $_SESSION['workad3'] . ', ' . $_SESSION['workpostcode'] . '.';` – Chris Jan 16 '12 at 17:00
  • You presumably have some kind of control character on the end of `$workad` which is cancelling the effect of the new line - although I have no idea what that would be. This means that it is actually on the end of `$_SESSION['workpostcode']` - how did you assign a value to this? If `var_dump($workad)` printed `NULL` then you didn't do it right - it must contain *something* otherwise you wouldn't see the address. – DaveRandom Jan 16 '12 at 17:13
  • It doesn't look like it's an issue with your code, but something to verify. If you use single quotes around \n, it won't output properly. There's an outside chance that the use of concatenation with ', ' is somehow affecting this. Have you tried hardcoding the value to a particular string, using double quotes? – JamesB41 Jan 16 '12 at 20:51
  • 1
    Where is the output? In the email or are you actually echoing the string for debug purposes? – hakre Jan 17 '12 at 10:05
  • ok, I've sorted the `var_dump($workad)` and it's showing this: `string(39) "Bloggoffice, Joe Street, London, N1 J0E"`, which is what I would expect - am I wrong? – Chris Jan 17 '12 at 10:13
  • Hakre, the output is in the e-mail – Chris Jan 17 '12 at 18:03

1 Answers1

1

Using PHP_EOL constant seems more correct than using \n. Because PHP_EOL is the correct end of line character for every platform.

cnkt
  • 2,863
  • 5
  • 28
  • 42
  • It really does not matter though. Never had problems with plain unix line endings, except when you try to use an older windows program to open them (but who does that?) – Evert Jan 17 '12 at 16:25