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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Seymour

    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@examp le.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
  • Michael Austin

    #2
    Re: MS-Windows mail servers and PHP's mail() function

    Jim Seymour wrote:[color=blue]
    > 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@examp le.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[/color]

    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
    :)

    Comment

    • Jim Seymour

      #3
      Re: MS-Windows mail servers and PHP's mail() function

      In article <cuvMc.5218$MS2 .2457@newssvr22 .news.prodigy.c om>,
      Michael Austin <maustin@firstd basource.com> writes:
      [snip][color=blue]
      >
      > it is not just "MS-Windows" mail servers, the CR/LF is a part of the
      > SMTP RFC
      > for email headers.[/color]

      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.
      [color=blue]
      > 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,[/color]

      I searched my current news spool for "[Ww]indows.*[Mm]ail" and
      "[Mm]ail.*[Ww]indows", w/o success.
      [color=blue]
      > I would recommend a search for WNT, PHP, mail[/color]

      Okay. I'll try those search terms in Google Groups. Thanks.
      [color=blue]
      > and take a
      > look at
      > the docs.[/color]

      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@LinxNe t.com | http://jimsun.LinxNet.com

      Comment

      • Manuel Lemos

        #4
        Re: MS-Windows mail servers and PHP's mail() function

        Hello,

        On 07/24/2004 01:13 PM, Jim Seymour wrote:[color=blue]
        > 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.[/color]

        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.




        --

        Regards,
        Manuel Lemos

        PHP Classes - Free ready to use OOP components written in PHP
        Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


        PHP Reviews - Reviews of PHP books and other products


        Metastorage - Data object relational mapping layer generator

        Comment

        • Manuel Lemos

          #5
          Re: MS-Windows mail servers and PHP's mail() function

          Hello,

          On 07/24/2004 01:13 PM, Jim Seymour wrote:[color=blue]
          > 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.[/color]

          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.




          --

          Regards,
          Manuel Lemos

          PHP Classes - Free ready to use OOP components written in PHP
          Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


          PHP Reviews - Reviews of PHP books and other products


          Metastorage - Data object relational mapping layer generator

          Comment

          • Jim Seymour

            #6
            Re: MS-Windows mail servers and PHP's mail() function

            In article <4102F3DB.40500 09@acm.org>,
            Manuel Lemos <mlemos@acm.org > writes:[color=blue]
            > Hello,
            >
            > On 07/24/2004 01:13 PM, Jim Seymour wrote:[color=green]
            >> 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.[/color]
            >
            > Depending on the PHP version that you use, your deliveries may be
            > affected by bugs of the PHP mail() function.[/color]

            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.
            [color=blue]
            >
            > You may want to try this class that provides some workarounds to the PHP
            > mail function bugs.[/color]
            [snip][color=blue]
            >[/color]

            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@LinxNe t.com | http://jimsun.LinxNet.com

            Comment

            • Jim Seymour

              #7
              Re: MS-Windows mail servers and PHP's mail() function

              In article <cuvMc.5218$MS2 .2457@newssvr22 .news.prodigy.c om>,
              Michael Austin <maustin@firstd basource.com> writes:[color=blue]
              > Jim Seymour wrote:[color=green]
              >> 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.[/color][/color]
              [snip][color=blue]
              >
              > 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.[/color]

              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



              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_head ers
              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_head ers 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('addlHea ders') 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@LinxNe t.com | http://jimsun.LinxNet.com

              Comment

              • StinkFinger

                #8
                Re: MS-Windows mail servers and PHP's mail() function

                simply put, use PHPMailer. ever since replacing all of PHPs crappy mail()
                with PHPMailer, i have never had a single problem.

                "Jim Seymour" <jseymour@LinxN et.com> wrote in message
                news:10g4vbfnvb vt92d@corp.supe rnews.com...[color=blue]
                >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@examp le.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[/color]


                Comment

                Working...