Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of Julian Matz
Julian Matz🇮🇪

PHP mail() and Mozilla Thunderbird
Hello!

I  use Mozilla Thunderbird 1.5 as e-mail client.
When I receive some plain-text e-mails sent by the PHP mail() function the e-mail message is displayed with double or quadruple line-breaks.

In other words, this issue only arises, as far as I know, with plain-text e-mails sent using PHP mail() function.

I have done some testing on this:
Outlook Express --> text/plain --> Thunderbird (OK)
php mail() --> text/plain --> Outlook Express (OK)
php mail() --> text/plain --> Thunderbird (outlined below)

I tested this using the following as the body of the php mail:

<?php
 
 $email_message = "
 $greeting\n
 You have received a message from $Name <$EMailAddr>.\n
 Timestamp: $timestamp
 IP Address: $ip
 Name: $Name\n
 $MessageTitle:
 ---------------------
 $Message
 ---------------------\n
####################################################
Sample text sample text sample text sample text
 sample text sample text sample text sample text sample text.
####################################################\n
";

?>

Everything was formatted ok except for the [$Message] part which came from a <textarea> inside a HTML form.
Every line break was doubled. I've pasted the e-mail as received by Thunderbird below - 1 line break (hard return) changed to 2 and 2 changed to 3 ...
Does anyone have any ideas? Is this a Thunderbird related issue or a php mail() issue?

Thanks for your input!

======================================================
 Good morning!

 You have received a message from Julian <[EDITED]>.

 Timestamp: 2006-01-30T07:47:56+01:00
 IP Address: 159.134.184.134
 Name: Julian

 Test:
 ---------------------
 Just testing...



Testing.

Testing...



Testing

More testing
 ---------------------

####################################################
Sample text sample text sample text sample text
 sample text sample text sample text sample text sample text.
####################################################

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of JB04JB04

Remove all the \n characters, their is no need as when you have something like:-


Hello
welcome
goodbye


PHP adds a newline character for each line break like above so using \n aswell is why you get that problem.


James

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Hi James,

I know what you mean with the \n characters, but there are none in $Message.
The problem occurs only inside the two lines: (whether I leave out the "\n" in the rest or not)

 ---------------------
 Just testing...



Testing.

Testing...



Testing

More testing
 ---------------------

ive never seen this before, try the following code as a test

fill in the blanks :)

<?php

$to = '';
$from = '';
$subject = 'just a test!';
$message = "

Hello

Goodbye

";

mail($to, $subject, $message, $from);

?>


Please post the email body you get back into here, tis should help diagnose a little easier if its related to the form or not.


BTW, what operating system?

also, are you using sendmail or SMTP or what?

James

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


SOLUTION
Avatar of JB04JB04

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

>> Please post the email body you get back into here

I received the e-mail in both Outlook Express and Thunderbird...
The results were slightly different:
Thunderbird removed the first line and added 2 or three at the end of message.
Outlook Express added 2 at the beginning and 1 or 2 at the end.

=OUTLOOK EXPRESS=========
>
>
>
> Hello
>
> Goodbye
>
>
>
=======================

=THUNDERBIRD=============
> Hello
> 
> Goodbye
> 
> 
> 
> 
=======================

>> what operating system?
Debian Sarge /GNU Linux, PHP 5.x, Apache 1.3

>> also, are you using sendmail or SMTP or what?
Pretty sure it's sendmail

>> $message = str_replace(array("\r\n","\r"), array("\n","\n"), $message);
I will test this and get back... Thanks!

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Hi James,

Your str_replace() method seems to work...
The issue only seems to occur with text received from a textarea, so this could be applied to that...

Have you any idea if there is also a server-side configuration that can solve this issue?
I can apply your method no problem when I'm developing on PHP but there are a lot of e-mails that are sent automatically by software on my server - for example the control panel and the billing system. It would be a lot easier to configure server-side... Maybe php.ini?

Thanks,
Julian

not that im aware of, on php.net it states that the message body should always contain a \n rather than \r\n or \r, perhaps you could drop a note to those applications, strange thing is ive never ever heard of this before on the php mailing list or anywhere else so must be specific to your setup or something!


James

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Yes, that's what I thought... I've done a lot of digging and searching on the net but didn't find anything...
Some people may not notice the extra line breaks because most e-mail clients compensate for extra line breaks by removing them from the message. My guess is that that's why the messages looked normal in Outlook Express.

If you have access to a server with PHP do you think maybe you could try uploading a test page that will submit the text from a textarea to my email address?
You don't have to do this... Only if you have time and wish to do so... If you can't just let me know and I will close the question...

I have written the php and uploaded in txt format to my server just in case. It includes my e-mail address.
http://www.netlink.ie/testing/phpmail.php.txt

Thanks,
- Julian

just sent it !

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Thanks, got it...

You're right, the e-mail you sent me was formatted without extra line breaks:

==============
> first line
> 
> third line
> 
> fifth line
> 
> 
>
==============

wherea mine looks like:

==============
> first line
> 
> 
> 
> third line
> 
> 
> 
> fifth line
> 
> 
>
==============

So it must be a server issue...
Very strange... Or would it be a PHP version issue?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Hi James,

Haven't really resolved this issue yet, but will keep trying.
If all fails I will just use the str_replace method you suggested...

I appreciate all your help!! Thanks!!

- Julian.

its certainly a strange one, let us know how you get on!



James

Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Hi James,

I don't know if you're still subscribed to this question...but I've made a very interesting discovery :-) ...

I've examined the e-mails as they come into Thunderbird and then compared them to the way they *should* be formatted...

This is what I found:

Every line in the e-mail that ends with a colon ":" is proceeded by 1 or 2 extra line break...
(It only happens when the ":" is at the end of a line, not if it's proceeded by text like
example: example)

Example: if this is the text for a plain-text e-mail -
============================================
Dear %%FULLNAME%%,

Your new hosting account has been created!
You should receive login details for your new account shortly.

If you are using an existing domain with your new account
you may need to change the nameserver information.
Our nameservers are:
NS1.EXAMPLE.COM
NS2.EXAMPLE.COM

If you wish to use your own nameservers you must setup
A-records and MX-records (for mail) to this IP address:
%%IPADDRESS%%

Your billing information can be accessed using following details:
%%USERLOGINURL%%
Username: %%EMAIL%%
Password: %%CLIENT_REAL_PASS%%
________________________________________________
Please contact us if you have any questions about your account.
============================================


Then this would be the e-mail when it arrives in the inbox:
============================================
Dear Name,

Your new hosting account has been created!
You should receive login details for your new account shortly.

If you are using an existing domain with your new account
you may need to change the nameserver information.
Our nameservers are:


NS1.EXAMPLE.COM
NS2.EXAMPLE.COM

If you
wish to use your own nameservers you must setup
A-records and MX-records (for mail) to this IP address:


195.137.213.66

Your billing information can be accessed using following details:


https://url
Username: username
Password: passwd
________________________________________________

Please contact us if you have any questions about your account.
============================================

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


thats very strange, are your using thunderbird on windows and using the latest version direct from mozilla.com ?




Avatar of Julian MatzJulian Matz🇮🇪

ASKER

Yes, Windows XP and Thunderbird version 1.5 (20051201)...
But I don't think it's an issue with Thunderbird because I had a copy of the same message sent to Outlook Express and I looked at the message's source code there and it's the exact same... My guess is that Outlook Express just removes the extra line breaks automatically when displayed...

Also, this only happens with plain-text, HTML mail seems fine...
PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.