PHP Script freezes after Mail() - php

PHP script freezes after the mail() command when run through a webpage, but runs fine on SSH.
Page just keeps loading forever, nothing executes after the mail() command, however, the mail() command does work and sends an email.
What could the problem be?

This should be a comment, but its a bit long.
You've not provided nearly enough information here.
It has been suggested that we need to see the code - if your description is accurate then we don't, however given the quality of information supplied the predicate is very dubious. What happens when you run something like:
<?php
print "started<br />\n";
while (ob_get_level()) ob_flush();
flush();
$result=mail('youruser#localhost', 'test', 'test');
print "result=" . var_export($result, true);
exit;
What do the access and error logs show for the script?
What does your MTA logs show?
What is your MTA?
What OS is this running on?
but runs fine on SSH
Does your CLI SAPI use the same php.ini files as the web server?
If not what are the respective configurations?
Are you running the CLI as the webserver uid?
(hint - that's 8 questions you need to answer before anyone can make an informed guess as to what's happenning here)

Related

Where are shell_exec 'echos' saved in?

I'm running a web server frontend that has numerous php console scripts (run via shell_exec) embedded throughout the backend.
These are triggered from shell_exec(php $scripthere);
These scripts have useful information output from them, such as echo "You should read this"; and die("You should read this too"); but as they are never echo'd to the frontend, they are never displayed.
Running my Ubuntu server, where can I read the outputs from shell_exec? Yes I know I could go through every line of code and fputs it somewhere, but as this is run via the shell_exec command is there not some STDOUT file or something I could read all outputs from?

php mail() returning false, but not in command line

I've been looking around on stackoverflow and many other websites for ideas, but nothing has worked so far and I need more direct help.
I have a dedicated Ubuntu Server (9.04 jaunty) and I'm running several PHP websites there, with apache2. My problem is that the mail() function has stopped working. Since a while back I've been using PEAR Mail Queue for most emails, so I'm not sure exactly when mail() stopped working, but I still need it for an OpenCart setup, among a few other things, but mail() keeps return false no matter what I do. Even this simple code won't work:
<?php
mail('mail#example.com','test','test');
?>
So, I've been looking at the Sendmail-settings in php.ini which all check out, and when I run this in the command line, it works fine and sends me an email:
# sendmail
mail#example.com
test message
So I thought it might be php itself that has a problem, so I try the following command:
# php -r "mail('mail#example.com','test','test');"
And it also sends the email without any problems! So what could it be? I started thinking it might be some sort of permission problem, that maybe www-data had some issues with running the script. Still not sure about this, because I can't even run this script:
<?php
echo exec('whoami');
?>
It also returns false! I've run the visudo command and added the following line just in case:
www-data ALL=(ALL) ALL
And I've changed the user and group owners of the files from root:root to www-data:www-data but still nothing!
What else can I try? There must be someting! Can anyone help me?
Commandline and webserver don't share the same php.ini! Check which php.ini is used on commandline and in the server module.
I had the same issue a few weeks ago. I tried:
PHP mail() works from command line but not apache
PHP "mail()" function sends mail from php5 cli but not when the script is run by a browser
And a few others.
I had a entry in my error log saying Segmentation fault. After 2 days a found the problem. I used Exim4 on my debian system, the combination with apache2 gave a segmentation fault. I replaced Exim4 with Postfix and it worked!
However, in your case you cannot run a exec like whoami and you should pursuit that path.
Because the mail function eventually comes to the os, a exec command should work!
You could send email using phpMailer throw SMTP with out using you mail() function.
The code is here(http://phpmailer.worxware.com/)
Just a idea
possibly can use this extation for opencart to use phpmailer to replace mail function. I have not tested this just so it and thought it might give someone some ideas.
In my case it was php-fpm used in Magento docker image I was playing with. It kept php preloaded (with configuration).
After restarting it picked up new settings: how to restart or just sudo killall php-fpm.

PHP Mail function doesn't work from command line but works when called from www

I have a script hosted at a site using CPanel. It stopped working a few days ago, not sure if it's because of an update but I can't find anything.
The script basically calls mail($email, "Testing", "This is a test"). This code works when I invoke it from the browser (i.e. I get the email) but not from the command line. It doesn't show any errors and I am not getting any email as well.
Can someone help?
Thanks
Command line PHP and web server PHP normally have different configuration directives, with different privileges enabled or disabled.
Take a look at the php.ini file for more information, there the different settings can be ...set.
And do as mario suggest too, ask phpinfo() (in web server) and php -i (in CLI);

Run PHP cronjob on hosting system

I wish to run a lengthy PHP script on a hosted Linux system (for example Godaddy or OVH). The script is supposed to be able to run for 1 or 2 hours. I suppose that would be a cronjob.
I have tried to make a Linux command call my PHP with following:
print `echo /usr/local/bin/php -q myScript.php | at now`;
But this does not return any feedback, and the script is not processed.
Using 'which PHP' I could check that the path to PHP is correct.
Also, other simple commands like ls -l give me proper output.
I am not sure how to further troubleshoot this, or if it is even possible.
How would you recommend to proceed to run my PHP script?
Update:
I have tried putting the whole path to the script and it made no difference.
Since I do not know how to edit cron on the hosting account, I am calling my PHP script by calling another PHP file. I have:
triggerScript.php:
<?php
print `/usr/local/bin/php /path/to/myScript.php`;
?>
and myScript.php:
<?php
print `ls -l`;
?>
Now I run triggerScript.php on my browser, and the page seem to load for a while but I get a browser error "Internet Explorer cannot display the webpage".
Did you say GoDaddy?
Check this out:
http://help.godaddy.com/topic/67/article/3547?locale=en
All the linux shared hosting I use/used has option to setup cron from control panel. You do not have to setup by running a command from php...
Godaddy supports cron, even with the lowest priced account. I use this features on some of my sites hosted on Godaddy.
You probably don't have shell access and cannot get real cron jobs.
Use one of the free or commercial "web cron" providers that regularly call one of your web php scripts. Combine that with a set_time_limit call in your php script so it does not get killed for runnning too long.

Why has my PHP system() command stopped returning output to my scripts ... i used to work!

I have a PHP script that calls a .bat file using system(). The output is written to the screen and I derive some values from parsing this output. This is running on windows 2003 IIS server. PHP v5.2.0
Specifically I am using this script to launch an Amazon EC2 instance and assign an IP address to it. It has worked great for me so far but recently the problem started.
Here is the code
$resultBatTemp = system("cmd /C C:\Inetpub\ec2\my_batch_file_to_launch_instance.bat");
$resultBat = (string)$resultBatTemp;
$instanceId = substr($resultBat, 9, 10);
...
Once I have this instace Id I can run another batch file that calls associates an ip address with this instance. It would appear that the instance does get launched but I never get the output on the screen.
For some reason this has all stopped working, the page freezes and never refreshes. I also need to completely exit safari or mozilla otherwise all pages from the website fail to load. Only when I relaunch the browser can i view the website again. I've connected to the webserver that hosts these scripts and checked PHP error log but nothing shows there. I've opened a DOS prompt and entered the code from the bat file that way and it connects to amazon and launches the instance fine. Ive isolated this bit of code and removed the system command and the rest of the script runs fine, so it appears that the hold up is with outputting the results of the bat file.
Recently I have purchased a new domain name for the site so this script is running from this domain. Might this cause the problem?
thanks
------------------------------------------------UPDATE-----------------------------------------------
Well hope this helps someone, I didnt find out what was wrong but created a new PHP file with a simple system command that called a .bat file, and a non-existent .bat file expecting to get an error back but nothing - just the usual hang for ages. So I restarted IIS and this fixed the problem. Dont know what was wrong but that did the trick.
Maybe first check what the system() call returns. According to documentation it will return FALSE in case of failure. Also, including your my_batch_file_to_launch_instance.bat in the question might help in solving it.
Try using the passthru function
Also make sure that all your commands are safe use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Categories