0

I've been refactoring an old php project that's fairly large and I've run into a problem that has totally stumped me. There are about 150 php files in total. I'm the original author and admittedly much of the code in my earlier projects is ...well really messy.

I've boiled it down to a few essential files and now I'm having a problem with the mail function on only one script. It's running on a shared virtual host under php 5.4. There are numerous places in the program that send mail to users, and it's all been working well until now.

Basically after evaluating the query string, index.php includes one or more of a few other scripts, then builds and outputs the result.

I have a user registration form that is included when called for. The user registration script displays the form and processes it's input upon submission. This all works fine except the mail command that notifies the admin of the new user. It returns true, so I don't see a problem with the code. And similar mail commands in other parts of the program within the same includes directory work fine.

In fact just to test it, I added a very basic mail command to the top of the user_registration script and duplicated the same mail command (except for the minor difference in subject line) and placed it into the index.php file right before the include statement. Something like this,

index.php

<?php

set_include_path('includes/');

mail('recipient@abc.com','subject - index','body text');
include('user_registration.php');

?>

user_registration.php

<?php

mail('recipient@abc.com','subject - user','body text');

// Do Other Stuff

?>

The mail command in index.php works fine. All of the code in user_registration.php works fine except the mail commands which returns true if wrapped in an if condition, yet the mail is never delivered. And, mail commands (some more complex) throughout the rest of the program work fine as well.

Of course this is an oversimplification of the actual code for clarity sake, but it's just about that simple.

What possible code could I have neglected to demonstrate in this example that might account for this behavior? What other factors could influence the mail command like this?

The hosting provider support tech I spoke with told me they do not log sendmail errors (seems strange), and couldn't help me otherwise.

I'm pulling my hair out trying to understand what's going on here. Surely the enormous brains here at SO have some helpful advice.

EDIT: I read other similar questions related to the php mail command failing to send mail, but none of those addressed the issue within the same context. My question is different because I have numerous instances of the mail command within my scripts that work as expected, and one script where none of the mail commands work. The mail command doesn't work anywhere in this one script, even at the very top. An exact copy of the same line of code is pasted into two scripts, one works, the other doesn't. Why? What could cause a single php script to behave this way? All other questions I've been able to find only discuss a single instance of the mail command not working. I'm newly registered at SO, but I've been a long-time reader. If I've missed this question elsewhere I apologize. Please point me to it.

J.Raney
  • 11
  • 3
  • 1
    my #1 tip for debugging mail sending: whats the mail server log say? –  Mar 22 '16 at 00:01
  • 1
    @Dagon I put it to you, that the #1 tip for any debugging is *Read the logs* –  Mar 22 '16 at 00:06
  • @PeterTòmasScott I prefer to echo all that out on screen. but its the same principle - the issue with mail is that its a black box to php, once it hands it off to the email server it has no idea what happens, the mail server never reports back, so you have to go to its logs directly. You cnat turn on php's error display and expect mail server errors (ditto db's) –  Mar 22 '16 at 00:10
  • I completely agree about debugging with error logs. Trouble is, I'm on a shared virtual host and my hosting provider tells me they do not keep logs for sendmail. Yeah, I know, sounds strange to me too, but that's what I'm working with. – J.Raney Mar 22 '16 at 05:26

0 Answers0