简体   繁体   中英

php mail 'from' header not behaving

I have an odd problem.

I have a 'standard' Email include I use that wraps the php mail() function. This has been in use for a few years and in two places in my site is quite happily sending mail every day. However, on re-using the same include in a different part of the same site on the same host, I am finding I cannot send mail including a 'From' header. The only difference is this new sending location on the site is a basic auth-protected directory.

So before people leap in and tell me this or that, remember, this is the exact same code as it working at the same time on the same host and so the exact-same include works in two locations and not the third.

So I set, among other headers (as this carries multipart HTML Email):

$headers  = "From: $fromName <$fromEmail>\n";
$headers .= "Reply-To: \"$fromName\" <$fromEmail>\n";
$headers .= "Sender: \"$fromName\" <$fromEmail>\n";

This works fine in two locations on the site, but the inclusion of the 'From' line causes a mail send failure in a third location on the same host. The code also works fine from my dev machine, it's only the Live hosted box that won't send mail from this third location. Now...

  1. Yes I know I'm using "\\n" and not "\\r\\n", but my experience has been "\\r\\n" is necessary on Windows hosts but causes problems on *ix hosts - and yes I know the RFC says CRLF - and so this is 'fixed' later in the wrapper if it's on a Windows host but left as "\\n" on *ix
  2. No, setting "\\r\\n" does not make any difference!
  3. I have experiemented with the mail() fifth paramter and using -f, but that only gives me mail sent on behalf of
  4. The 'Sender' field actually has the effect that on opening the message the recipient sees; From Pretty_name [email_address], however, in Outlook at least, in the mail list the sender is shown as email_address, not 'Pretty_name'
  5. I've experiented with ini-set, but that only sets an Email address and the message is already going with the desired From Email address, it's the 'Pretty_name' that won't work

So remember - the exact same code is runing elsewhere on the same site without any problems, but the issue here seems to be (for a reason I've yet to fathom) that the same code can only be used without the 'From' header from this location.

Anyone any ideas on what seems such inconsistent behaviour?

Thx

First, if mail() returns false, then make sure you output the last error message:

print_r(error_get_last());

If the error message doesn't really indicate much about the email itself, then it's likely something wrong in the php.ini file (eg configuration mistake during an upgrade or something). You can test that by isolating the problem code to a separate file, then temporarily swap out the php.ini file for the default one, restart your web server to let the new php.ini take effect, and run your test again (I'd recommend dumping phpinfo() along with your script so you can confirm that the new php.ini is in effect).

Second, use \\r\\n. I know it has no functional difference in your test here, but it IS the right line ending and using just \\n WILL cause problems in some mail servers that actually follow the rules. When you use \\n, you are relying on the mail server to have additional code that handles automatic correction of errors.

Using \\r\\n might cause problems in some poorly-coded servers that DON'T follow the rules, but that's not really your problem to solve. Always follow the RFC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM