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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jace
Forum Newbie
Posts: 12
Joined: Fri Jan 15, 2010 5:33 pm

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

Post 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")
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

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

Post 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.
User avatar
zeus1
Forum Newbie
Posts: 22
Joined: Tue May 18, 2010 10:45 pm
Location: /Under/The/AppleTree.png

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

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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:
User avatar
Jace
Forum Newbie
Posts: 12
Joined: Fri Jan 15, 2010 5:33 pm

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

Post 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...)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
Post Reply