f



MS-Windows mail servers and PHP's mail() function

I have a contact form that uses PHP's mail() function.  It recently
came to my attention that (some?) MS-Windows mail servers may not
properly process data given to the mail() function that's only
newline-terminated.

The mail() function takes up to five parameters:

    mail(recipient, subject, message, [headers [,parameters]])

The ones of interest here are the first four.  The questions are
these:

    1. Since recipient and subject are not multi-line, and, from the
       web form, don't even contain newlines, is it safe to assume
       they need neither newlines nor carriage-returns?

    2. The message, from the web form, contains newlines.  I assume
       all newlines need carriage-returns inserted in front of them?

    3. The headers are created, in the web form processing, by
       appending newline-terminated strings to a variable (e.g.:
       headers .= "blah: yadda\n";).  I presume that this string
       likewise needs carriage-returns inserted before the newlines?

    4. I've seen mention of problems with "hanging carriage-returns."
       I presume that simply means there should be no line, blank or
       otherwise, that contains a carriage-return w/o an accompanying
       newline?

Assuming all the above to be true, my four parameters would look like:

    recipient: "somebody@example.com"
      subject: "Some subject"
      message: "Some message\r\ntext\r\ngoes here\r\n"
      headers: "Header1: foo\r\nHeader2: bar\r\nHeader3: baz\r\n"

Is the above what (some?) MS-Windows mail servers want to see?

TIA,
Jim
-- 
Jim Seymour                    | PGP Public Key available at:
                               | http://www.uk.pgp.net/pgpnet/pks-commands.html
                               |
                               | http://jimsun.LinxNet.com
0
jseymour (27)
7/24/2004 3:17:03 PM
comp.lang.php 32646 articles. 0 followers. Post Follow

7 Replies
1935 Views

Similar Articles

[PageSpeed] 43

Jim Seymour wrote:
> I have a contact form that uses PHP's mail() function.  It recently
> came to my attention that (some?) MS-Windows mail servers may not
> properly process data given to the mail() function that's only
> newline-terminated.
> 
> The mail() function takes up to five parameters:
> 
>     mail(recipient, subject, message, [headers [,parameters]])
> 
> The ones of interest here are the first four.  The questions are
> these:
> 
>     1. Since recipient and subject are not multi-line, and, from the
>        web form, don't even contain newlines, is it safe to assume
>        they need neither newlines nor carriage-returns?
> 
>     2. The message, from the web form, contains newlines.  I assume
>        all newlines need carriage-returns inserted in front of them?
> 
>     3. The headers are created, in the web form processing, by
>        appending newline-terminated strings to a variable (e.g.:
>        headers .= "blah: yadda\n";).  I presume that this string
>        likewise needs carriage-returns inserted before the newlines?
> 
>     4. I've seen mention of problems with "hanging carriage-returns."
>        I presume that simply means there should be no line, blank or
>        otherwise, that contains a carriage-return w/o an accompanying
>        newline?
> 
> Assuming all the above to be true, my four parameters would look like:
> 
>     recipient: "somebody@example.com"
>       subject: "Some subject"
>       message: "Some message\r\ntext\r\ngoes here\r\n"
>       headers: "Header1: foo\r\nHeader2: bar\r\nHeader3: baz\r\n"
> 
> Is the above what (some?) MS-Windows mail servers want to see?
> 
> TIA,
> Jim

it is not just "MS-Windows" mail servers, the CR/LF is a part of the SMTP RFC 
for email headers. There have been several discussions in this group about this 
issue in the past, rather trying to remember all of the nuances of the 
discussions, I would recommend a search for WNT, PHP, mail and take a look at 
the docs.

-- 
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
0
maustin (1437)
7/24/2004 3:45:12 PM
In article <cuvMc.5218$MS2.2457@newssvr22.news.prodigy.com>,
	Michael Austin <maustin@firstdbasource.com> writes:
[snip]
> 
> it is not just "MS-Windows" mail servers, the CR/LF is a part of the
> SMTP RFC 
> for email headers.

I'm aware of that.  But my mail servers, at least, seem to just
"handle it" when I use the mail() function.  (Which injects email
into the local MTA's queue via the "sendmail" command and therefor
isn't using SMTP, btw.)  I don't know that *all* 'nix-based MTAs do
that, but mine does.  Furthermore: From my discussion with my friend
and colleague: Apparently so do some (most?) MS-Win-based MTAs.  He
has a .NET-based contact form that was working just fine, until along
came somebody for whom it did not work (mail simply went into a black
hole - no error return or anything) until he inserted
carriage-returns.

>                    There have been several discussions in this group
> about this 
> issue in the past, rather trying to remember all of the nuances of the 
> discussions,

I searched my current news spool for "[Ww]indows.*[Mm]ail" and
"[Mm]ail.*[Ww]indows", w/o success.

>              I would recommend a search for WNT, PHP, mail

Okay.  I'll try those search terms in Google Groups.  Thanks.

>                                                            and take a
> look at 
> the docs.

I looked at the docs.  My questions were based on what I gleaned from
the docs and the associated follow-up comments.  I'm just trying to
make sure, as best as possible, I have it as right, since I have no
MS-Win server to test it on and no idea what MS-Win-based MTA(s) need
the carriage-returns, even if I did.

Thanks for the follow-up.

-- 
Jim Seymour                    | PGP Public Key available at:
WARNING: The "From:" address   | http://www.uk.pgp.net/pgpnet/pks-commands.html
is a spam trap.  DON'T USE IT! |
Use: jseymour@LinxNet.com      | http://jimsun.LinxNet.com
0
jseymour (27)
7/24/2004 4:13:05 PM
Hello,

On 07/24/2004 01:13 PM, Jim Seymour wrote:
> I looked at the docs.  My questions were based on what I gleaned from
> the docs and the associated follow-up comments.  I'm just trying to
> make sure, as best as possible, I have it as right, since I have no
> MS-Win server to test it on and no idea what MS-Win-based MTA(s) need
> the carriage-returns, even if I did.

Depending on the PHP version that you use, your deliveries may be 
affected by bugs of the PHP mail() function.

You may want to try this class that provides some workarounds to the PHP 
mail function bugs. It comes with several sub-classes that implement 
alternative delivery methods to using the mail function. It also comes 
with wrapper functions named smtp_mail() and sendmail_mail() functions 
that emulate the mail() function except that they deliver the messages 
via the sendmail program or via SMTP.

http://www.phpclasses.org/mimemail


-- 

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
0
mlemos (662)
7/24/2004 11:42:19 PM
Hello,

On 07/24/2004 01:13 PM, Jim Seymour wrote:
> I looked at the docs.  My questions were based on what I gleaned from
> the docs and the associated follow-up comments.  I'm just trying to
> make sure, as best as possible, I have it as right, since I have no
> MS-Win server to test it on and no idea what MS-Win-based MTA(s) need
> the carriage-returns, even if I did.

Depending on the PHP version that you use, your deliveries may be 
affected by bugs of the PHP mail() function.

You may want to try this class that provides some workarounds to the PHP 
mail function bugs. It comes with several sub-classes that implement 
alternative delivery methods to using the mail function. It also comes 
with wrapper functions named smtp_mail() and sendmail_mail() functions 
that emulate the mail() function except that they deliver the messages 
via the sendmail program or via SMTP.

http://www.phpclasses.org/mimemessage


-- 

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
0
mlemos (662)
7/24/2004 11:42:35 PM
In article <4102F3DB.4050009@acm.org>,
	Manuel Lemos <mlemos@acm.org> writes:
> Hello,
> 
> On 07/24/2004 01:13 PM, Jim Seymour wrote:
>> I looked at the docs.  My questions were based on what I gleaned from
>> the docs and the associated follow-up comments.  I'm just trying to
>> make sure, as best as possible, I have it as right, since I have no
>> MS-Win server to test it on and no idea what MS-Win-based MTA(s) need
>> the carriage-returns, even if I did.
> 
> Depending on the PHP version that you use, your deliveries may be 
> affected by bugs of the PHP mail() function.

I've used the code I wrote under everything from PHP 4.2.3 to 4.3.8,
with Apache from at least 1.3.27 through 1.3.31, on various releases
of Linux, Sparc Solaris and FreeBSD, with sendmail, qmail, and various
revisions of Postfix for MTAs.

> 
> You may want to try this class that provides some workarounds to the PHP 
> mail function bugs.
[snip]
> 

Thanks, Manuel, but good ol' mail() is working fine for me, on the
platforms I choose to use.  If mail() was so b0rk3d as to fail to
work correctly and reliably on my platforms-of-choice, I wouldn't be
using PHP.  Period.  If I can do *minor* code-changes to make my work
(more) usable by my MS-Win-using brethren: I'm more than happy to do
so.  But *minor* is as far as I'm willing to go.

Thanks for the follow-up.

-- 
Jim Seymour                    | PGP Public Key available at:
WARNING: The "From:" address   | http://www.uk.pgp.net/pgpnet/pks-commands.html
is a spam trap.  DON'T USE IT! |
Use: jseymour@LinxNet.com      | http://jimsun.LinxNet.com
0
jseymour (27)
7/25/2004 4:56:21 AM
In article <cuvMc.5218$MS2.2457@newssvr22.news.prodigy.com>,
	Michael Austin <maustin@firstdbasource.com> writes:
> Jim Seymour wrote:
>> I have a contact form that uses PHP's mail() function.  It recently
>> came to my attention that (some?) MS-Windows mail servers may not
>> properly process data given to the mail() function that's only
>> newline-terminated.
[snip]
> 
> it is not just "MS-Windows" mail servers, the CR/LF is a part of the
> SMTP RFC 
> for email headers. There have been several discussions in this group
> about this 
> issue in the past, rather trying to remember all of the nuances of the 
> discussions, I would recommend a search for WNT, PHP, mail and take a
> look at 
> the docs.

Unfortunately, I wasn't able to turn up much in the way of useful
information by searching Google Groups with strings such as "WNT
mail," "WINNT mail," or "WIN32 mail."  But this URL

    http://us2.php.net/manual/en/function.mail.php

had, on a second reading, all that I needed.  (I think.)  From that,
it looks like all I have to do is make sure the additional_headers
have cr/lf at the end of each header line, save the last header line,
which must have neither a cr nor a lf.

My additional_headers are added here-and-there, throughout the code,
to a variable named $addlHeaders.  Each header line is added with a
trailing newline.  So, just before I call mail, I have this bit of
code:

    // MS-Win mail servers want crlf and *don't* want a trailing pair
    if(PHP_OS == "WIN32" || PHP_OS == "WINNT") {
	// It seems we only need do this with the "additional headers,"
	// but we're set up here to easily add other mail() variables.
	foreach (array('addlHeaders') as $foo) {
	    $$foo = preg_replace("/\n$/", "", $$foo);
	    $$foo = preg_replace("/\n/", "\r\n", $$foo);
	}
    }

Thanks to all for the follow-ups.

-- 
Jim Seymour                    | PGP Public Key available at:
WARNING: The "From:" address   | http://www.uk.pgp.net/pgpnet/pks-commands.html
is a spam trap.  DON'T USE IT! |
Use: jseymour@LinxNet.com      | http://jimsun.LinxNet.com
0
jseymour (27)
7/25/2004 8:00:14 PM
simply put, use PHPMailer. ever since replacing all of PHPs crappy mail() 
with PHPMailer, i have never had a single problem.

"Jim Seymour" <jseymour@LinxNet.com> wrote in message 
news:10g4vbfnvbvt92d@corp.supernews.com...
>I have a contact form that uses PHP's mail() function.  It recently
> came to my attention that (some?) MS-Windows mail servers may not
> properly process data given to the mail() function that's only
> newline-terminated.
>
> The mail() function takes up to five parameters:
>
>    mail(recipient, subject, message, [headers [,parameters]])
>
> The ones of interest here are the first four.  The questions are
> these:
>
>    1. Since recipient and subject are not multi-line, and, from the
>       web form, don't even contain newlines, is it safe to assume
>       they need neither newlines nor carriage-returns?
>
>    2. The message, from the web form, contains newlines.  I assume
>       all newlines need carriage-returns inserted in front of them?
>
>    3. The headers are created, in the web form processing, by
>       appending newline-terminated strings to a variable (e.g.:
>       headers .= "blah: yadda\n";).  I presume that this string
>       likewise needs carriage-returns inserted before the newlines?
>
>    4. I've seen mention of problems with "hanging carriage-returns."
>       I presume that simply means there should be no line, blank or
>       otherwise, that contains a carriage-return w/o an accompanying
>       newline?
>
> Assuming all the above to be true, my four parameters would look like:
>
>    recipient: "somebody@example.com"
>      subject: "Some subject"
>      message: "Some message\r\ntext\r\ngoes here\r\n"
>      headers: "Header1: foo\r\nHeader2: bar\r\nHeader3: baz\r\n"
>
> Is the above what (some?) MS-Windows mail servers want to see?
>
> TIA,
> Jim
> -- 
> Jim Seymour                    | PGP Public Key available at:
>                               | 
> http://www.uk.pgp.net/pgpnet/pks-commands.html
>                               |
>                               | http://jimsun.LinxNet.com 


0
stinky (50)
7/25/2004 10:15:11 PM
Reply: