Page 1 of 1

mail() allows names on Linux, not on Windows!?

Posted: Wed May 12, 2010 3:32 am
by Jace
When using PHP's mail() function, you can use just 'basic' email addresses like 'your@address.com', but you can also include names, e.g. like 'John Doe <your@address.com>'.

Now, if I try the following php on a Linux webserver, it works fine:

Code: Select all

<?php
$to = 'Somebody <you@domain.com>';
$subject = 'Hello';
$message = 'Hi there, just testing';
$from = 'Me <me@domain.com>';
$fromAddress = 'me@domain.com';

$headers = "From: $from\r\n";
$param = "-f$fromAddress";

mail($to,$subject,$body,$headers,$param);
?>
However when I run the exact same script on a Windows server (using Xampp 1.7.1), it says:
Warning: mail() [function.mail]: SMTP server response: 501 <Me <me@domain.com>>: "@" or "." expected after "Me" in C:\httpdocs\test.php on line 11

And when I reduce the $from to just the address only (i.e. like $fromAddress), the same problem occurs with the To address:
Warning: mail() [function.mail]: SMTP server response: 501 <Somebody <you@domain.com>>: "@" or "." expected after "Somebody" in C:\httpdocs\test.php on line 11

When I reduce that as well to just 'you@domain.com', it works fine again.
I tried several SMTP servers, but it made no difference. And besides, in this case the Linux and Windows machines were using the same SMTP server anyway.

Is this a bug in php_smtp.dll or ... ? :?:

(FYI: I'm running PHP Version 5.2.9 on Apache/2.2.11, phpinfo says "SMTP support: enabled, version 0.2.0-dev")

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 6:41 am
by Benjamin
I'm not sure why it would do that. I know there are some issues with using the mail function on Windows boxes. I recommend that you look into using Swiftmailer.

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 11:56 am
by Weirdan
Jace wrote:And besides, in this case the Linux and Windows machines were using the same SMTP server anyway.
Are you sure? By default mail() on unix pipes mail to sendmail binary (meaning it talks to local mailserver installed on the server where you invoke mail()) that passes the message further, while on windows php connects to mail server via smtp. This is the most likely reason for differences in behavior you observe.

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 12:44 pm
by flying_circus
I've been fighting that issue for a long time myself.

As Benjamin said, SwiftMailer will allow you to do what you want.

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 2:38 pm
by zeus1
1. On the windows machine: Are you sure you set up what the SMTP server is and what SMTP port is in the php.ini file? Try

Code: Select all

<? phpinfo(); ?>
for details. You have to do this for windows machines. Check out http://www.w3schools.com/PHP/php_ref_mail.asp for more details.
2. If this doesn't work, try using the PEAR extension. Make sure you specify all the smtp parameters, don't worry about authentication if you don't have it. Check out http://email.about.com/od/emailprogramm ... cation.htm

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 3:01 pm
by mikosiko
No
Jace wrote:When using PHP's mail() function, you can use just 'basic' email addresses like 'your@address.com', but you can also include names, e.g. like 'John Doe <your@address.com>'.

However when I run the exact same script on a Windows server (using Xampp 1.7.1), it says:
Warning: mail() [function.mail]: SMTP server response: 501 <Me <me@domain.com>>: "@" or "." expected after "Me" in C:\httpdocs\test.php on line 11

And when I reduce the $from to just the address only (i.e. like $fromAddress), the same problem occurs with the To address:
Warning: mail() [function.mail]: SMTP server response: 501 <Somebody <you@domain.com>>: "@" or "." expected after "Somebody" in C:\httpdocs\test.php on line 11
...

this note in http://php.net/manual/en/function.mail.php caught my attention :

Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.
As such, the to parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA.

Re: mail() allows names on Linux, not on Windows!?

Posted: Wed May 19, 2010 3:03 pm
by mikosiko
zeus1 wrote:1. On the windows machine: Are you sure you set up what the SMTP server is and what SMTP port is in the php.ini file? Try

Code: Select all

<? phpinfo(); ?>
for details. You have to do this for windows machines. Check out http://www.w3schools.com/PHP/php_ref_mail.asp for more details.
2. If this doesn't work, try using the PEAR extension. Make sure you specify all the smtp parameters, don't worry about authentication if you don't have it. Check out http://email.about.com/od/emailprogramm ... cation.htm
he is not saying that the SMTP is not working... :wink:

Re: mail() allows names on Linux, not on Windows!?

Posted: Fri May 21, 2010 9:01 am
by Jace
Thanks for your suggestions. Will try to sort out if the linux server does indeed use a different SMTP server after all.

On the windows machine, phpinfo says smtp = <my ISP's SMTP server>, port = 25, and smtp support enabled, version "0.2.0-dev".
I also use this smtp server myself in my local mail client, works OK.

Regarding Swiftmail or Pear, are they available for PHP4 too? The mail part of the stuff I'm developing unfortunately also needs to run on some crappy legacy servers with PHP 4 :( (I know, people hosting such servers should rather be left to die and rot, but alas...)

Re: mail() allows names on Linux, not on Windows!?

Posted: Mon May 31, 2010 6:17 am
by Weirdan
Jace wrote:Regarding Swiftmail or Pear, are they available for PHP4 too?
Swiftmailer has certainly abandoned PHP4 compatibility long time ago, but pear component could still be compatible.